如何在斯坦福大学的Stanza(StanfordNLP)中加载文档而不是字符串?

问题描述 投票:0回答:1

我正在使用斯坦福的Stanza,但找不到有关如何加载文档而不是字符串的文档。

例如,对于一个字符串,它的工作方式如下:

nlp_pos = stanza.Pipeline('it',processors='tokenize,mwt,pos,lemma,depparse')
doc = nlp_pos("hello how are you")

对于文档,我认为应该是这样:

nlp_pos = stanza.Pipeline('it',processors='tokenize,mwt,pos,lemma,depparse')
filename = "example.txt"
with open(filename, 'r') as f:
doc = f.read()

但是它不起作用。什么是替代品?

python python-3.x nlp stanford-nlp stanford-stanza
1个回答
0
投票

这里是一个示例,从Stanza文档中略作改编。我创建了一个文件句柄,并传递给doc = nlp(....)。警告:我不是在写原始文件(Stanza_No_Tags.txt),而是在写新文件(Stanza_Tokenized.txt)。 YMMV。

nlp = stanza.Pipeline(lang='zh', processors='tokenize')
Stanza_doc_open = open('Stanza_No_Tags.txt', 'r').read()

doc = nlp(Stanza_doc_open)
for i, sentence in doc.sentences:
    print(f'====== Sentence {i+1} =======', file=open('Stanza_Tokenized.txt', 'a'))
© www.soinside.com 2019 - 2024. All rights reserved.