【问题标题】:concatenate words in a text file连接文本文件中的单词
【发布时间】:2019-07-22 12:05:47
【问题描述】:

我已将 pdf 文件导出为 .txt,我观察到许多单词由于换行符而被分成两部分。所以,在这个程序中,我想加入文本中分离的单词,同时保持句子中的正确单词。最后,我想得到一个所有单词拼写正确的最终 .txt 文件(或至少一个标记列表)。谁能帮我?

我现在的文字是这样的:

我需要你的帮助,因为我不是一个好的程序员。

我需要的结果:

我需要你的帮助,因为我不是一个优秀的程序员。

from collections import defaultdict
import re
import string
import enchant

document_text=open('test-list.txt','r')
text_string=document_text.read().lower()
lst=[]
errors=[]

dic=enchant.Dict('en_UK')
d=defaultdict(int)
match_pattern = re.findall(r'\b[a-zA-Z0-9_]{1,15}\b', text_string)

for w in match_pattern:
lst.append(w)

for i in lst:
    if  dic.check(i) is True:
        continue
    else:
        a=list(map(''.join, zip(*([iter(lst)]*2))))
    if dic.check(a) is True:
        continue
    else:
        errors.append(a)
print (lst)

【问题讨论】:

  • 那么错误是什么?

标签: python concatenation overwrite word


【解决方案1】:

你有一个更大的问题——你的程序怎么知道:

be
cause

...应该被当作一个词来对待?

如果你真的想,你可以用空格替换换行符:

import re

document_text = """
i need your help be
cause i am not a good programmer
""".lower().replace("\n", '')

print([w for w in re.findall(r'\b[a-zA-Z0-9_]{1,15}\b', document_text)])

这将正确地检查because,但在以下情况下会失败:

Hello! My name is 
Foo.

...因为isFoo 不是一个词。

【讨论】:

    猜你喜欢
    • 2013-08-26
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    相关资源
    最近更新 更多