【发布时间】:2021-09-02 14:40:42
【问题描述】:
我有一个包含常见拼写错误及其更正的文本文件。
同一个单词的所有拼写错误都应该在同一行。
我确实做了一些这样的工作,但不是针对同一个单词的所有拼写错误。
misspellings_corpus.txt (sn-p):
I'de->I'd
aple->apple
appl->apple
I'ed, I'ld, Id->I'd
期望:
I'de, I'ed, I'ld, Id->I'd
aple, appl->apple
模板:wrong1, wrong2, wrongN->correct
尝试:
lines = []
with open('/content/drive/MyDrive/Colab Notebooks/misspellings_corpus.txt', 'r') as fin:
lines = fin.readlines()
for this_idx, this_line in enumerate(lines):
for comparison_idx, comparison_line in enumerate(lines):
if this_idx != comparison_idx:
if this_line.split('->')[1].strip() == comparison_line.split('->')[1].strip():
#...
correct_words = [l.split('->')[1].strip() for l in lines]
correct_words
【问题讨论】:
-
将
collections.defaultdict(list)与您的正确拼写键一起使用,并将每个错误拼写附加为一个值。然后一旦完成,您可以根据需要写出 values() 和 key -
我对所需的文字感到困惑。第一行不应该是:
I'd, I'd, I'd, I'd,第二行也不应该是:apple, apple? -
@jrd1 目的是用逗号
,分隔拼写错误,然后->正确拼写。我会将所需的模板附加到帖子中。 -
@JonSG 我现在在帖子中附加了
correct_words的列表。我会调查collections。