【问题标题】:How to set sentences to variables NLTK如何将句子设置为变量 NLTK
【发布时间】:2017-02-12 14:37:11
【问题描述】:

我对使用 nltk 还是很陌生,但遇到了困难。我想将一个文本文件拆分成单独的句子,并将每个句子设置为一个变量以供以后使用。我已经处理了第一部分:

import nltk
from nltk.tokenize import sent_tokenize

text1 = open('/Users/joshuablew/Documents/myCorpus/version1.txt').read()

sent_tokenize(text1)

这会打印出每个分开的句子:

['Who was the 44th president of the United States?', 'Where does he live?', 'This is just a plain sentence.', 'As well as this one, just to break up the questions.', 'How many houses make up the United States Congress?', 'What are they called?', 'Again, another question breakpoint here.', 'Who is our current President?', 'Can he run for re-election?', 'Why or why not?']

从这里我不知道该怎么做才能将这些句子自动保存到变量中。

或者,是否可以有索引text1[0] = 'Who was the 44th president of the United States?',和text1[1] = 'Where does he live?' 等等?其中文本文件的每个索引是每个单独的句子

感谢您的帮助。

【问题讨论】:

  • sent_tokenize 将段落标记为句子列表。 textList = sent_tokenize(text1) 应该会给你这个可以通过索引引用的列表。

标签: python nlp nltk


【解决方案1】:
import nltk
from nltk.tokenize import sent_tokenize

with open('1.txt', 'r') as myfile:
    sentences=myfile.read()

number_of_sentences = sent_tokenize(sentences)

print(len(number_of_sentences))

textList = sent_tokenize(sentences)

print(textList)

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2012-06-29
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    相关资源
    最近更新 更多