使用python nltk库运行打包的命名实体识别代码时遇到运行时异常。 Pyinstaller已用于制作.exe文件。带有Anaconda的Python 3.5安装在机器中。
ImportError : No module named 'nltk.chunk.named.entity'. Failed to execute script
以下代码已在NER_1.py文件中使用。
#Code modificaiton for standalone python run - PythonInstaller version
#importing libraries
import nltk
import sys
import pandas as pd
import numpy as np
import re
import os
from nltk.tokenize import PunktSentenceTokenizer
#Reading the text from console
print("Enter the text - ")
raw = sys.stdin.readline()
sentences = nltk.sent_tokenize(raw) #split in to sentances
tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences] #split in to words
tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences] #tag sentences with NN, NNP, etc
chunked_sentences = nltk.ne_chunk_sents(tagged_sentences, binary=True)
def extract_entity_names(t):
entity_names = []
if hasattr(t, 'label') and t.label:
if t.label() == 'NE':
entity_names.append(' '.join([child[0] for child in t]))
else:
for child in t:
entity_names.extend(extract_entity_names(child))
return entity_names
entity_names = []
for tree in chunked_sentences:
entity_names.extend(extract_entity_names(tree))
#Check if the output is null
if not entity_names:
print("List is empty")
entity_names.append("NullNER")
# Print unique entity names
print (set(entity_names))
input("Press any key to exit...")