git clone https://github.com/ServiceInnovationLab/bagel-TextBlob.git
cd bagel-TextBlobWe want to use the exact version of python this project recommends.
pyenv install < .python-versionTo check the current version after installation:
python --version # This should match the version in .python-version filepip install -r requirements.txtFirst 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.sentimentExample of negative:
from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
blob = TextBlob("My boss is horrible.", analyzer=NaiveBayesAnalyzer())
blob.sentiment