我学到了很多关于自然语言处理的nltk,可以做很多事情,但我无法找到从包中阅读文本的方法。我尝试过这样的方法:
from nltk.book import *
text6 #Brings the title of the text
open(text6).read()
#or
nltk.book.text6.read()
但它似乎不起作用,因为它没有丝状。以前似乎没有人问过这个问题,所以我想答案应该很简单。你知道如何阅读这些文本或如何将它们转换成字符串吗?提前感谢
发布于 2018-03-15 09:48:59
让我们深入研究一下代码=)
首先,nltk.book
代码驻留在https://github.com/nltk/nltk/blob/develop/nltk/book.py上。
如果我们仔细查看,文本将作为一个nltk.Text
对象加载,例如,对于来自https://github.com/nltk/nltk/blob/develop/nltk/book.py#L36的text6
:
text6 = Text(webtext.words('grail.txt'), name="Monty Python and the Holy Grail")
Text
对象来自https://github.com/nltk/nltk/blob/develop/nltk/text.py#L286,您可以阅读更多关于如何从http://www.nltk.org/book/ch02.html中使用它的信息。
webtext
是nltk.corpus
的一个语料库,因此要获取nltk.book.text6
的原始文本,可以直接加载get文本。
>>> from nltk.corpus import webtext
>>> webtext.raw('grail.txt')
只有在加载PlaintextCorpusReader
对象时才会出现PlaintextCorpusReader
,而不是从Text
对象(处理对象)加载:
>>> type(webtext)
<class 'nltk.corpus.reader.plaintext.PlaintextCorpusReader'>
>>> for filename in webtext.fileids():
... print(filename)
...
firefox.txt
grail.txt
overheard.txt
pirates.txt
singles.txt
wine.txt
发布于 2018-03-14 18:19:49
看来他们已经为你把它拆开了。
from nltk.book import text6
text6.tokens
发布于 2021-02-21 13:17:13
#生成排序令牌
print(sorted(set(text6))
https://stackoverflow.com/questions/49283774
复制相似问题