我有以下代码打开目录中的文件,在它们上运行 spaCy NLP,并将输出依赖项解析信息放入新目录中的文件中。
import spacy, os
nlp = spacy.load('en')
path1 = 'C:/Path/to/my/input'
path2 = '../output'
for file in os.listdir(path1):
with open(file, encoding='utf-8') as text:
txt = text.read()
doc = nlp(txt)
for sent in doc.sents:
f = open(path2 + '/' + file, 'a+')
for token in sent:
f.write(file + '\t' + str(token.dep_) + '\t' + str(token.head) + '\t' + str(token.right_edge) + '\n')
f.close()
问题是这不会保留输出文件中依赖项的顺序。我似乎在 API 文档中找不到任何对字符位置的引用。