git clone https://github.com/ServiceInnovationLab/bagel-TextBlob.git
cd bagel-TextBlob
We want to use the exact version of python this project recommends.
pyenv install < .python-version
To check the current version after installation:
python --version # This should match the version in .python-version file
pip install -r requirements.txt
First we’ll create some training and test data:
Using/editing the dummmy data from train.json
& test.json
.
Example: Passing the opened file into the constructor
with open('train.json', 'r') as fp:
cl = NaiveBayesClassifier(fp, format="json")
from textblob import TextBlob
from textblob.classifiers import NaiveBayesClassifier
with open('train.json', 'r') as fp:
cl = NaiveBayesClassifier(fp, format="json")
Call the classify(text) method to use the classifier.
Example:
cl.classify("This is an amazing library!")
Or if you have more than 1 line of text:
blob = TextBlob("The beer is good. But the hangover is horrible.", classifier=cl)
for s in blob.sentences:
print(s)
print(s.classify())
Example of positive:
from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
blob = TextBlob("I love this library", analyzer=NaiveBayesAnalyzer())
blob.sentiment
Example of negative:
from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
blob = TextBlob("My boss is horrible.", analyzer=NaiveBayesAnalyzer())
blob.sentiment