'module' object has no attribute 'word_tokenize' - python

It is first time to play with python, I am trying to copy and run some codes available online to understand how they work, but some of them are not working. This is an example of a problem that I faced when I try to copy a program that does the part of speech in python using NLTK.
import nltk
import re
import time
exampleArray = ['The incredibly intimidating NLP scares people who are sissies.']
def processLanguage():
try:
for item in exampleArray:
tokenized = nltk.word_tokenized(item)
tagged = nltk.pos_tag(tokenized)
except Exception, e:
print str(e)
processLanguage()
The problem is when I run this code, I have this message:
'module' object has no attribute 'word_tokenize'
'module' object has no attribute 'word_tokenized'
Can you please tell me how to solve this.

Related

Python getting object has no attribute error but attribute exists in package documentation

I am trying to get the lat/lngs from a successful location search using the SearchControl class in ipyleaflet. Code is from the ipyleaflet documentation link. When I run the below example I get the error AttributeError: 'SearchControl' object has no attribute 'on_location_found'. I know the code as is wont get me what I need, but it was a starting point. Can anyone advise how I might be able to get the lat/lngs? New to this.
from ipyleaflet import Map, SearchControl
m = Map(center=[47, 2], zoom=5)
search = SearchControl(
position="topleft",
url='https://cartoradon.irsn.fr/commune.py/communes/search/FR/{s}?',
zoom=5
)
m.add_control(search)
def on_found(**kwargs):
# Print the result of the search (text, location etc)
print(kwargs)
search.on_location_found(on_found)
m

TDA AttributeError

I am new to TDA tools in python and I was implementing a simple code to get familiar with these, but I got a strange error
import tadasets
from ripser import ripser
import persim
circle = tadasets.dsphere(d=1)
dgms = ripser(circle)
persim.plot_diagrams(dgms)
AttributeError: 'dict' object has no attribute 'astype'.
Actually I am working on Jupyter notebook with python version 3.8.3

Gensim Attribute Error when trying to use pre_scan on a doc2vec object

I am following the tutorial here:
https://github.com/RaRe-Technologies/gensim/blob/develop/docs/notebooks/doc2vec-wikipedia.ipynb
But when I get to this part:
pre = Doc2Vec(min_count=0)
pre.scan_vocab(documents)
I get the following error on scan_vocab:
AttributeError: 'Doc2Vec' object has no attribute 'scan_vocab'
Does anyone know how to fix this? Thanks.
That's a known problem after a 2018 refactoring of the Doc2Vec code:
https://github.com/RaRe-Technologies/gensim/issues/2085
You can just skip that cell to proceed with the rest of that demo notebook. (If you really needed to adjust the min_count using the info from a full-scan, you might be able to call some internal classes/methods mentioned in the above issue.)

Problem loading an tfidf object with pickle

I have an issue with pickle:
from a previous job, I created an sklearn tfidfvectorizer object and I saved it thanks to pickle.
Her is the code I used to do it :
def lemma_tokenizer(text):
lemmatizer=WordNetLemmatizer()
return [lemmatizer.lemmatize(token) for token in
word_tokenize(text.replace("'"," "))]
punctuation=list(string.punctuation)
stop_words=set(stopwords.words("english")+punctuation+['``',"''"]+['doe','ha','wa'])
tfidf = TfidfVectorizr(input='content',tokenizer=lemma_tokenizer,stop_words=stop_words)
pickle.dump(tfidf, open(filename_tfidf, 'wb'))
I saw that if i wan't to load the this tfidf object thanks to pickle, I need to define the function "lemma_tokenizer" before.
So I create the following python script named 'loadtfidf.py' to load the tfidf object :
import pickle
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
def lemma_tokenizer(text):
lemmatizer=WordNetLemmatizer()
return [lemmatizer.lemmatize(token) for token in word_tokenize(text.replace("'"," "))]
tfidf = pickle.load(open('tfidf.sav', 'rb'))
if I run this script the object is well loaded and everything goes well!
BUT then, I create another python script named 'test.py' in the same directory of 'loadtfidf.py' where I simply try to import loadtfidf:
import loadtfidf
And when I try to run this one line I have the following error :
"""Can't get attribute 'lemma_tokenizer' on module 'main' from '.../test.py'"""
I really don't understand why... I don't even know what to try to fix this error... can you help me to fix it?
Thank you in advance for your help !

ntlk python frenchStemmer

I an trying to initialize FrenchStemmer:
stemmer = nltk.stem.FrenchStemmer('french')
and the error is:
AttributeError: 'module' object has no attribute 'FrenchStemmer'
Has anyone seen this error before?
That's because nltk.stem has no module called as FrenchStemmer.
The French stemmer available is in SnowballStemmer() and you can access it by
import nltk
stemmer=nltk.stem.SnowballStemmer('french')
or by
import nltk
stemmer=nltk.stem.snowball.FrenchStemmer()

Categories

Resources