TypeError:stat:路径应为字符串,字节,os.PathLike或整数,而不是_io.TextIOWrapper

时间:2018-07-14 09:35:11

标签: python python-3.x stanford-nlp

我在python教程网站上找到了以下代码:

test_pic[pic1-1]
test_pic[pic1-2]
test_pic[pic1-3]
test_pic[pic2-14]
test_pic[pic2-15]
test_pic[pic3-100]
test_pic[pic3-200]
test_pic[pic3-300]

错误如下:

from nltk.tag import StanfordNERTagger
from nltk.tokenize import word_tokenize


stanford_classifier = open(r"C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\stanford-ner-2018-02-27\classifiers\english.all.3class.distsim.crf.ser.gz")
stanford_ner_path = open(r"C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\stanford-ner-2018-02-27\stanford-ner.jar")


st = StanfordNERTagger(stanford_classifier, stanford_ner_path)

text = 'While in France, Christine Lagarde discussed short-term stimulus efforts in a recent interview with the Wall Street Journal.'

tokenized_text = word_tokenize(text)
classified_text = st.tag(tokenized_text)

print (classified_text)

1 个答案:

答案 0 :(得分:0)

您的问题

在此documentation page中可以看到,StanfordNERTagger将文件路径作为参数:

StanfordNERTagger(path_to_model, path_to_jar)

您的代码崩溃是因为open()将为您提供文件对象,而这并不是StanfordNERTagger期望的参数。

解决方案

将路径直接作为StanfordNERTagger的参数,如下所示:

st = StanfordNERTagger("C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\stanford-ner-2018-02-27\classifiers\english.all.3class.distsim.crf.ser.gz", "C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\stanford-ner-2018-02-27\stanford-ner.jar")