首页 > 解决方案 > 将 CSV 输入到 SpaCy 中的自定义 NER 模型

问题描述

对 ML 和 Python 来说非常陌生,感谢您对此问题的任何帮助。我已经使用 Prodigy(基于 en_core_web_lg)训练了一个 NER 模型,并将模型保存到我的虚拟环境中:

我的模特

我在带有 CONDA/VSCODE、SpaCy 2.x 环境的 Windows 10 上,现在我正在尝试加载一个逗号分隔的 CSV 文件,如下所示:

在此处输入图像描述

nlp = spacy.load("en_core_web_lg", disable=["ner"]) #remove NER of base model
print(nlp.pipe_names) #check to see if removed
nlp_entity = spacy.load("tmp_model", vocab=nlp.vocab) #load my tmp model
nlp.add_pipe(nlp_entity.get_pipe("ner")) #add back NER
print(nlp.pipe_names) #check to see if it was added back
nlp.to_disk("./tmp_model2") #save combo as a new model name

nlp=spacy.load("tmp_model2") #load new model
doc=nlp("Paragraph Text Here") #test the model with this text to see if its working
print(doc.text)
for ent in doc.ents: #for all entities in doc
     print(ent.label_, ent.text) #get the label and text

从这里开始,这就是我卡住的地方。我对自己说,我可以像这样读取 CSV 文件:

input = pd.read_csv('myfile.csv') #read in CSV via Pandas
doc=nlp(input['Text']) #look for "Text" column in the CSV file and run the model for each row
for ent in doc.ents:
     print(ent.label_, ent.text)

TypeError:参数“字符串”的类型不正确(预期为 str,得到系列)

再次对 Python 非常陌生,但我认为我需要将 Pandas 数据框转换为字符串?如果是这样,我将如何去做?

标签: pythonmachine-learningspacy

解决方案


在 Andrey 的帖子的帮助下,我能够找出合适的语法来吐出所有行。

input = pd.read_csv('MyFile.csv')
row_nums = len(input.index)
print("Number of rows is: ", len(input.index))
for x in range (0,row_nums):
    print(x, " LOOP START")
    doc=nlp(input['Text'].values[x])
    print(doc.text)
    for ent in doc2.ents:
        print(ent.label_, ent.text)

下一步是让我弄清楚如何将其推送回 CSV 文件!


推荐阅读