问题
def getNer(text):
with open('chunker.pkl', 'rb') as pickle_file:
chunker = pickle.load(pickle_file)
return chunker.parse(pos_tag(word_tokenize(text)))
Running this function works fine But when I include this function in my Django Project I get the following error
chunker = pickle.load(pickle_file)
AttributeError: Can't get attribute 'NamedEntityChunker' on <module '__main__' from 'manage.py'>
The object being pickled is
class NamedEntityChunker(ChunkParserI):
def __init__(self, train_sents, **kwargs):
assert isinstance(train_sents, Iterable)
self.feature_detector = features
self.tagger = ClassifierBasedTagger(
train=train_sents,
feature_detector=features,
**kwargs)
def parse(self, tagged_sent):
chunks = self.tagger.tag(tagged_sent)
iob_triplets = [(w, t, c) for ((w, t), c) in chunks]
return conlltags2tree(iob_triplets)
Im using the latest version of Django and Python3
回答1:
I had the same error - turns out I wasn't importing the class before trying to open it. The GUI needs to know how to construct the object before being able to read it. Try:
from YourModuleName import NamedEntityChunker
before calling your opening function.
来源:https://stackoverflow.com/questions/45483349/attributeerror-cant-get-attribute-on-module-main-from-manage-py