Passing variable to a module method
Question:
0
`vectorizer = TfidfVectorizer(analyzer=’word’,norm=None, use_idf=True,smooth_idf=True) tfIdfMat = vectorizer.fit_transform(df[‘Description’]) feature_names = sorted(vectorizer.get_feature_names())
docList=[‘df.Description’] #skDocsTfIdfdf = pd.DataFrame(tfIdfMat.todense(),index=sorted(docList), columns=feature_names) #print(skDocsTfIdfdf)
for col in tfIdfMat.nonzero()[1]: print (feature_names[col], ‘-‘ , tfIdfMat[0, col])`
Answers:
As the quickstart says, an error of this kind is thrown if the function page
does not find the page. In this case is a bit strange because it is wikipedia suggestion. Anyway, surround at least that line in a try-except
block to handle the error:
import wikipedia
results = wikipedia.search("KFC")
for x in results:
print(x)
try:
getlink = wikipedia.page(x).url
except wikipedia.exceptions.PageError as e:
print(f'{x} not found.')
print(getlink)
Also, you could try replacing search
method by suggest
method
0
`vectorizer = TfidfVectorizer(analyzer=’word’,norm=None, use_idf=True,smooth_idf=True) tfIdfMat = vectorizer.fit_transform(df[‘Description’]) feature_names = sorted(vectorizer.get_feature_names())
docList=[‘df.Description’] #skDocsTfIdfdf = pd.DataFrame(tfIdfMat.todense(),index=sorted(docList), columns=feature_names) #print(skDocsTfIdfdf)
for col in tfIdfMat.nonzero()[1]: print (feature_names[col], ‘-‘ , tfIdfMat[0, col])`
As the quickstart says, an error of this kind is thrown if the function page
does not find the page. In this case is a bit strange because it is wikipedia suggestion. Anyway, surround at least that line in a try-except
block to handle the error:
import wikipedia
results = wikipedia.search("KFC")
for x in results:
print(x)
try:
getlink = wikipedia.page(x).url
except wikipedia.exceptions.PageError as e:
print(f'{x} not found.')
print(getlink)
Also, you could try replacing search
method by suggest
method