python - 如何将文本行转换为有意义的单词

标签 python regex nlp nltk

<分区>

我有一行字符串:

"specificationsinaccordancewithqualityaccreditedstandards"

需要拆分成标记化的词,例如:

"specifications in accordance with quality accredited standards"

我已经尝试过nltkword_tokenize但是它无法转换,

上下文:我正在将 PDF 文档解析为文本文件,这是我从 pdf 转换器返回的文本,用于将 pdf 转换为文本我在 中使用 PDFminer Python

最佳答案

你可以使用递归来解决这个问题。首先,您需要下载一个字典 txt 文件,您可以在此处获取:https://github.com/Ajax12345/My-Python-Projects/blob/master/the_file.txt

dictionary = [i.strip('\n') for i in open('the_file.txt')]
def get_options(scrambled, flag, totals, last):
   if flag:
       return totals

   else:
       new_list = [i for i in dictionary if scrambled.startswith(i)]
       if new_list:

           possible_word = new_list[-1]
           new_totals = totals
           new_totals.append(possible_word)
           new_scrambled = scrambled[len(possible_word):]
           return get_options(new_scrambled, False, new_totals, possible_word)

        else:
            return get_options("", True, totals, '')


s = "specificationsinaccordancewithqualityaccreditedstandards"
print(' '.join(get_options(s, False, [], '')))

输出:

'specifications in accordance with quality accredited standards'

关于python - 如何将文本行转换为有意义的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46055753/

相关文章:

python - 我可以使用逻辑索引或索引列表对张量进行切片吗?

python - 正则表达式搜索和子

c# - 从右到左语言中的正则表达式模式匹配

python - 了解 scikit CountVectorizer 中的 min_df 和 max_df

python-3.x - Sklearn - NLTK 问题预测

python - 捕获单词并重写

Python 在压缩列表的列表后消除元组

regex - 我如何在Python中使用F字符串和正则表达式

python - 如何使用正则表达式将\\转换为\in python或替换?

r - 仅保留语料库中包含特定关键词的句子(R 中)