gpt4 book ai didi

python - 在 Python 中替换字符串系列中不正确单词的有效方法

转载 作者:行者123 更新时间:2023-12-05 07:23:47 25 4
gpt4 key购买 nike

我正在处理手写的文本数据,因此它有很多拼写错误。我目前正在使用 pyspellchecker 来清理数据,并且我正在使用 correct() 方法在单词不存在时查找最可能的单词。我的方法是创建一个字典,将所有写得不好的词作为键,最有可能的词作为值:

dic={}
for i in df.text:
misspelled = spell.unknown(i.split())
for word in misspelled:
dic[word]=spell.correction(word)

尽管这很有效,但速度非常慢。因此,我想知道是否有更快的选择来实现它。你有什么想法吗?

编辑:df.text 中有 10571 行,字符串通常有 5-15 个单词长。每个循环大约需要 3-5 秒,这使得运行整个循环总共需要大约 40000 秒。

最佳答案

如果您只想创建一个从您遇到的拼写错误的单词到他们的建议的映射,您可以通过删除重复的单词来减小数据集的大小。这将最大限度地减少对 spell.unknownspell.correction 的调用次数,并防止对字典内容进行不必要的更新。

uniquewords = set().union(*(sentence.split() for sentence in df.text))
corrections = {word: spell.correction(word) for word in spell.unknown(uniquewords)}

关于python - 在 Python 中替换字符串系列中不正确单词的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55800059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com