python - 在 Python 中的字符串系列中替换不正确单词的有效方法
问题描述
我正在处理手写的文本数据,所以它有很多拼写错误。我目前正在清理数据,并且当单词不存在时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.unknown
这将最大限度地减少对and的调用次数,spell.correction
并防止对字典内容进行不必要的更新。
uniquewords = set().union(*(sentence.split() for sentence in df.text))
corrections = {word: spell.correction(word) for word in spell.unknown(uniquewords)}
推荐阅读
- python - 如何为现有熊猫数据框中的每个条目添加标志
- closures - 在 for 循环中使用 var 和使用闭包会产生不同的结果
- google-cloud-platform - 无法将 Search Ads 360 中的历史数据导入 Google BigQuery
- php - 序列化表单验证 Laravel 8
- uml - 如何从用例图中识别正确的主要参与者?
- java - 无法从文本文件中删除记录
- javascript - ReactJS:更新父组件类名或在具有不同类名的子父级中重新创建它
- java - Liquibase:向所有表添加相同的列
- google-cloud-firestore - 关于如何使用 Material UI 组件创建博客的建议?
- jenkins - Jenkins 主动选择参数丢失
在 HTML 中