ScalaConsultants / Aspect-Based-Sentiment-Analysis

💭 Aspect-Based-Sentiment-Analysis: Transformer & Explainable ML (TensorFlow)
Apache License 2.0
539 stars 91 forks source link

Sentiment Analysis Example Code Output Improvement #45

Open chetanniradwar opened 3 years ago

chetanniradwar commented 3 years ago

I am running the Readme file example.

from transformers import BertTokenizer`
name = 'absa/classifier-lapt-0.2'`
model = absa.BertABSClassifier.from_pretrained(name)
tokenizer = BertTokenizer.from_pretrained(name)
professor = absa.Professor()     # Explained in detail later on.
text_splitter = absa.sentencizer()  # The English CNN model from SpaCy.
nlp = absa.Pipeline(model, tokenizer, professor, text_splitter)
task = nlp(text="the laptop has excellent design.But battery life is not as my per my expectation.", aspects=['design','Battery','processor'])
tokenized_examples = nlp.tokenize(task.examples)
input_batch = nlp.encode(tokenized_examples)
output_batch = nlp.predict(input_batch)
predictions = nlp.review(tokenized_examples, output_batch)
completed_task = nlp.postprocess(task, predictions)

When I run this: absa.summary(design) I am getting right output i.e. Sentiment.positive for "design" Scores (neutral/negative/positive): [0.028 0.057 0.915]

But when I run this absa.summary(processor) I am getting negative sentiment with a score above 0.9. Instead, I should get a neutral sentiment as the "processor" aspect is not included in the text. There is no sentence talking about the processor (performance). If I want to do it such that the aspects which are not included in the text should be assigned a neutral sentiment instead of positive or negative how to do it? Please If anyone has the idea or code for that add it here.

ghost commented 3 years ago

what worked for me was:


aspects = ['design', 'processor']
my_aspect = []
for word in review_text:
    for aspect in aspects:
        if(similarity_score(aspect, word) > threshold):
            my_aspect.append(aspect)

task = nlp(text=review_text, aspects=my_aspect)
aspect_sent = task.examples

for aspect in aspect_sent:
    absa.summary(aspect)
chetanniradwar commented 3 years ago

@Megham-Garg Where similarity_score() method is defined? which library I have to import for similarity_ score()?

ghost commented 3 years ago

I have written my own method using the concept of cosine similarity.   You can use that or you may like to use some other method like euclidean distance based on the dataset you are dealing with.