0
我有以下代碼打開目錄中的文件,對它們運行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文檔中找到對字符位置的任何引用。
謝謝syllogism_!這很好。我結束了以下內容:'爲孩子送: \t \t \t \t頭= child.head \t \t \t \t head_pos = child.head.tag_ \t \t \t \t常量=小孩 \t \t \t \t const_pos = child.tag_ \t \t \t \t f.write(file +'\ t'+ str(child.idx)+'\ t'+ str(child.dep_)+'\ t'+ str(head)+'\ t'+ str(head_pos)+'\ t'+ str(const)+'\ t'+ str(const_pos) +'\ n')' – Shane