Skip to content

Instantly share code, notes, and snippets.

@amrakm
Last active May 26, 2022 10:48
Show Gist options
  • Save amrakm/2c5f0d830157e762b408f47a130365fd to your computer and use it in GitHub Desktop.
Save amrakm/2c5f0d830157e762b408f47a130365fd to your computer and use it in GitHub Desktop.
aspect based sentiment analysis
# pip install git+https://github.com/ScalaConsultants/Aspect-Based-Sentiment-Analysis
import aspect_based_sentiment_analysis as absa
nlp = absa.load()
text = ("We are great fans of Slack, but we wish the subscriptions "
"were more accessible to small startups.")
slack, price = nlp(text, aspects=['slack', 'price'])
print(slack.sentiment, slack.sentiment.value)
print(price.sentiment, price.sentiment.value)
####
#Sentiment.positive 2
#Sentiment.negative 1
absa.summary(price)
#Sentiment.negative for "price"
#Scores (neutral/negative/positive): [0.012 0.958 0.0]
def generate_absa(text: str, foods: List[str]) -> List[Dict]:
absa_list = []
aspects = nlp(text, aspects=list(foods))
for food in foods:
absa_dict = {}
sentiment = aspects[food].sentiment
absa_dict['food'] = food
absa_dict['sentiment_name'] = sentiment.name
absa_dict['sentiment_value'] = sentiment.value
absa_list.append(absa_dict)
return absa_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment