Ho una breve funzione per verificare se una parola è una parola reale confrontandola con il corpus WordNet dal Natural Language Toolkit. Sto chiamando questa funzione da un thread che convalida i file txt. Quando eseguo il mio codice, la prima volta che la funzione viene chiamata getta un AttributeError con il messaggioCosa potrebbe causare che WordNetCorpusReader non abbia alcun attributo LazyCorpusLoader?
"'WordNetCorpusReader' object has no attribute '_LazyCorpusLoader__args'"
Quando ho sospendere l'esecuzione, la stessa riga di codice non genera un errore, quindi immagino che il corpus è non ancora caricato sulla mia prima chiamata che ha causato l'errore.
Ho provato a utilizzare nltk.wordnet.ensure_loaded()
per forzare il caricamento del corpus, ma sto ancora ottenendo lo stesso errore.
Ecco la mia funzione:
from nltk.corpus import wordnet as wn
from nltk.corpus import stopwords
from nltk.corpus.reader.wordnet import WordNetError
import sys
cachedStopWords = stopwords.words("english")
def is_good_word(word):
word = word.strip()
if len(word) <= 2:
return 0
if word in cachedStopWords:
return 0
try:
wn.ensure_loaded()
if len(wn.lemmas(str(word), lang='en')) == 0:
return 0
except WordNetError as e:
print "WordNetError on concept {}".format(word)
except AttributeError as e:
print "Attribute error on concept {}: {}".format(word, e.message)
except:
print "Unexpected error on concept {}: {}".format(word, sys.exc_info()[0])
else:
return 1
return 1
print (is_good_word('dog')) #Does NOT throw error
Se ho un'istruzione di stampa nello stesso file in ambito globale, che non genera l'errore. Tuttavia, se lo chiamo dal mio thread, lo fa. Di seguito è riportato un esempio minimo per riprodurre l'errore. Ho provato sulla mia macchina e dà l'uscita
Attribute error on concept dog: 'WordNetCorpusReader' object has no attribute '_LazyCorpusLoader__args'
Attribute error on concept dog: 'WordNetCorpusReader' object has no attribute '_LazyCorpusLoader__args'
Attribute error on concept dog: 'WordNetCorpusReader' object has no attribute '_LazyCorpusLoader__args'
Attribute error on concept dog: 'WordNetCorpusReader' object has no attribute '_LazyCorpusLoader__args'
Attribute error on concept dog: 'WordNetCorpusReader' object has no attribute '_LazyCorpusLoader__args'
Attribute error on concept dog: 'WordNetCorpusReader' object has no attribute '_LazyCorpusLoader__args'
Attribute error on concept dog: 'WordNetCorpusReader' object has no attribute '_LazyCorpusLoader__args'
Attribute error on concept dog: 'WordNetCorpusReader' object has no attribute '_LazyCorpusLoader__args'
Attribute error on concept dog: 'WordNetCorpusReader' object has no attribute '_LazyCorpusLoader__args'
Esempio minimo:
import time
import threading
from filter_tag import is_good_word
class ProcessMetaThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
is_good_word('dog') #Throws error
def process_meta(numberOfThreads):
threadsList = []
for i in range(numberOfThreads):
t = ProcessMetaThread()
t.setDaemon(True)
t.start()
threadsList.append(t)
numComplete = 0
while numComplete < numberOfThreads:
# Iterate over the active processes
for processNum in range(0, numberOfThreads):
# If a process actually exists
if threadsList != None:
# If the process is finished
if not threadsList[processNum] == None:
if not threadsList[processNum].is_alive():
numComplete += 1
threadsList[processNum] = None
time.sleep(5)
print 'Processes Finished'
if __name__ == '__main__':
process_meta(10)
Ottima spiegazione! Ma puoi spiegare come 'wm' può essere in uno stato incoerente? I suoi dicts sono globali a livello di thread, quindi contiene gli attributi manglessi di 'WordnetCorpusReader'. Ma quali informazioni sul thread locale lo fanno apparire come un "LazyCorpusLoader' sul secondo thread? – alexis
@alexis ottima domanda! Vedrò se riesco a ridimensionarlo un po 'non appena ne avrò l'occasione. (È molto presto qui.) La linea di fondo è che wn è ancora un oggetto proxy quando il secondo thread lo accede per la prima volta nonostante abbia avuto la sua classe e sia cambiato, ma i dettagli sono una cosa importante da includere qui. –
Grazie, non vedo l'ora di leggerlo. Non ho idea di quale tipo di "oggetto proxy" possa rompere in modo così realistico. (Una condizione di competizione, con il thread 2 che vede l'oggetto a metà trasformazione, non dovrebbe dare errori così consistenti.) – alexis