问题
I want to run some sentiment analysis using FastText
. However, I have always got errors during the declaration of libraries and no example and tutorial within the web seems to be able to fix this.
I have tried to follow the steps described here: https://github.com/facebookresearch/fastText/tree/master/python#installation
but since the beginning, i.e. since
import fasttext
from fasttext import train_unsupervised
I have been getting the following error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-10-193c2ffe3856> in <module>
1 import fasttext
----> 2 from fasttext import train_unsupervised
3
4 # Skipgram model :
5 model = fasttext.train_unsupervised('data.txt', model='skipgram')
ImportError: cannot import name 'train_unsupervised' from 'fasttext' (/anaconda3/lib/python3.7/site-packages/fasttext/__init__.py)
I am using Python 3.7 in Jupyter Notebook. I would need FastText to analyse the sentiment of some Italian texts. I went here: https://fasttext.cc/docs/en/supervised-models.html but I have not understood what I should download.
I really hope you can help me with this.
回答1:
Running your code on a clean Python 3.7 conda environment should work after installing fasttext with pip (pip install fasttext
).
If you do that, you should see in a Linux console with
pip list | grep fasttext
that your fasttext
version is 0.9.2 (the current one today).
In addition, upon installing the wget
package with pip, the code below should get you started for sentiment analysis using one of the trained models (Amazon reviews) in the page that you linked:
import wget
from fasttext import load_model
wget.download("https://dl.fbaipublicfiles.com/fasttext/supervised-models/amazon_review_polarity.bin", 'model.bin')
model = load_model("model.bin")
model.predict("This movie sucks") # see how output changes!
model.predict("This band is great")
model.predict("I just feel OK about this.")
If model size is an issue, try replacing the model with a compressed one:
wget.download("https://dl.fbaipublicfiles.com/fasttext/supervised-models/amazon_review_polarity.ftz", 'model.ftz')
model = load_model("model.ftz")
You can also refer to https://fasttext.cc/docs/en/supervised-tutorial.html to train a model on a custom dataset instead.
来源:https://stackoverflow.com/questions/61978549/sentiment-analysis-and-fasttext-import-error