【问题标题】:How to use autocorrect in Pandas column of sentences如何在 Pandas 句子列中使用自动更正
【发布时间】:2018-08-28 02:43:14
【问题描述】:

我有一列句子,我是这样拆分的

df['ColTest'] = df['ColTest'].str.lower().str.split()

我要做的是遍历每个句子中的每个单词并应用 autocorrect.spell()

for i in df['ColTest']:
for j in i:
    df['ColTest'][i][j].replace(at.spell(j))

这是报错

AttributeError: 'float' 对象没有属性 'replace'

自动拼写autospell

DataFrame 的样子

ColTest
This is some test string
that might contain a finger
but this string might contain a toe
and this hass a spel error

我的专栏中没有数字...请问有什么想法吗?

【问题讨论】:

  • 您能分享几行数据框 df 吗?

标签: python string python-3.x pandas autocorrect


【解决方案1】:

使用autocorrect library,您需要遍历数据框的行,然后遍历给定行中的单词以应用spell 方法。这是一个工作示例:

from autocorrect import spell 
import pandas as pd 

df = pd.DataFrame(["and this hass a spel error"], columns=["colTest"])
df.colTest.apply(lambda x: " ".join([spell(i) for i in x.split()]))

也正如@jpp 在下面的评论中所建议的,我们可以避免使用lambda,如下所示:

df["colTest"] = [' '.join([spell(i) for i in x.split()]) for x in df['colTest']]

下面是输入的样子:

                      colTest
0  and this hass a spel error

输出:

0    and this has a spell error
Name: colTest, dtype: object

【讨论】:

  • 或者,避免 lambda 使用类似:df['colTest'] = [' '.join([spell(i) for i in x.split()]) for x in df['colTest']]
  • @jpp 我添加了你的建议。谢谢。
  • 好帖子!请注意 autocorrect.spell 现在已弃用。但同样适用于 autocorrect.Speller。 from autocorrect import Speller, 'spell = Speller(lang='en')` 其余部分按原样工作。
猜你喜欢
  • 2023-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 2023-02-16
相关资源
最近更新 更多