我有一组文本文件。我正在使用斯坦福大学的coreNLP名称实体识别器来提取这些文件中提到患者姓名的行的详细信息。当我在单个句子上运行NER时,它可以正确打印结果,但是当我在一组文件上运行它时,它在打印结果以及错误的同时,由于以下原因,我无法在文本文件上写入结果这个:
500 Server Error: Internal Server Error for url: http://localhost:9000/?properties=%7B%22outputFormat%22%3A+%22json%22%2C+%22annotators%22%3A+%22tokenize%2Cssplit%2Cner%22%2C+%22ssplit.isOneSentence%22%3A+%22true%22%7D
这是我正在使用的代码:
import re
import os
from nltk.parse import CoreNLPParser
tagger = CoreNLPParser(url='http://localhost:9000', tagtype='ner')
def name_detail_extracter():
data_location="D:\Data" # folder containing all the data
for root, dirs, files in os.walk(data_location):
for filename in files:
with open(os.path.join(root, filename), encoding="utf8",mode="r") as f:
patient_name_check=re.compile(r".*\s+(patient name)\s*:*\s*(.*)",re.I)
for line_number, line in enumerate(f, 1):
patient_name_matches=patient_name_check.findall(line)
for match in patient_name_matches:
name_details=match[1]
tokens = name_details.split()
result=tagger.tag(tokens)
for m in result:
print(m)
name_detail_extracter()
答案 0 :(得分:1)
该问题已解决,因为有一些空令牌传递给NER,所以现在我对它们进行了检查。
for match in patient_name_matches:
name_details=match[1]
tokens = name_details.split()
if tokens: # this is the check which I put
result=tagger.tag(tokens)
for m in result:
print(m)