有没有一种方法可以识别和创建数据帧中所有缩写词的列表



我有一个数据帧,其中有一列包含许多缩写词。

我只想简单地(a(确定下一列每个单元格中的所有首字母缩写词,(b(列出所有发现的唯一首字母缩写(而不是重复的(。

我想简单地使用pyspellchecker来查找任何拼写错误的单词,并将其视为首字母缩写。

我知道这种方法也会产生非首字母缩略词,这些词只是拼写错误的单词,但我想不出任何其他方法来做到这一点(除非我们假设所有首字母缩写词也都是大写的,不幸的是,在我的数据集中不是这样(。

例如,

第1列
我曾在NBA工作
我在中央情报局工作
我看到一个pt
CIA和NBA都是很酷的工作场所

不确定这是否正是您想要的,但可能会有所帮助。我想你有一个这样的数据帧(不是一个系列(:

df =
Column 1
0                      I worked for the NBA
1                       I worked at the CIA
2                          I am seeing a pt
3  CIA and NBA are both cool places to work
4             I also worked at NSA catedslf

然后这个

from spellchecker import SpellChecker
spell = SpellChecker()
df["Column 2"] = df.assign(
misspelled=df["Column 1"].str.split().map(spell.unknown),
acronyms=df["Column 1"].str.findall(r"([A-Z]{2,})").map(set)
)[["misspelled", "acronyms"]].apply(lambda row: set.union(*row), axis=1)

中的结果

Column 1         Column 2
0                      I worked for the NBA            {NBA}
1                       I worked at the CIA            {CIA}
2                          I am seeing a pt             {pt}
3  CIA and NBA are both cool places to work       {NBA, CIA}
4             I also worked at NSA catedslf  {catedslf, NSA}

然后

result = set.union(*df["Column 2"])

产生

{'NSA', 'CIA', 'catedslf', 'NBA', 'pt'}

df["Column 2"] = df["Column 2"].map(", ".join)

最后

Column 1       Column 2
0                      I worked for the NBA            NBA
1                       I worked at the CIA            CIA
2                          I am seeing a pt             pt
3  CIA and NBA are both cool places to work       CIA, NBA
4             I also worked at NSA catedslf  NSA, catedslf

但未来可能还有其他问题。例如标点符号。也许你应该做一些类似的事情:

from string import punctuation
df["Column 1"] = df["Column 1"].str.translate(str.maketrans("", "", punctuation))

事先(可能有更好的方法(。

最新更新