通过Python用段落对书进行标记

问题描述 投票:-2回答:1

我正在研究一个NLP项目,并试图按段落标记“大期望”,然后存储到列表中。我需要执行此操作才能执行一些无监督的学习主题模型。

#reading in great expectations
fp = open("dickens-great.txt")

great = fp.read()

print(great[0:100])

#processing

great_paras=[]

for paragraph in great:
    para=paragraph[0]
    #removing the double-dash from all words
    para=[re.sub(r'--','',word) for word in para]
    #Forming each paragraph into a string and adding it to the list of strings.
    great_paras.append(para)

print(great_paras[0:4])

我得到的回报是:

[['C'], ['h'], ['a'], ['p']]

您可以看到,打印great_paras变量时遇到了问题,因为它是按字母而不是段落来分割文本。我已经为此苦苦挣扎了一段时间,但将不胜感激!

python nlp tokenize topic-modeling
1个回答
0
投票

[for paragraph in great分别遍历字符串great中的所有字符。

您实际上需要将文本拆分为段落,例如,根据换行的格式,通过在换行符或两个连续的换行符处进行拆分。

© www.soinside.com 2019 - 2024. All rights reserved.