使用regex执行情感分析

pgvzfuti  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(111)

我有一个客户评论列表,我必须使用正则表达式(regex)将它们分类为正面或负面。
这是一个客户评论的例子,一个积极关键词和消极关键词的列表。

review="I absolutely loved this product! Loving it!"

positive_keyword= ['loved','outstanding', 'exceeded']

negative_keyword= ['hated','not good', 'bad']

上面的示例评论将被分类为积极的,因为出现了“loved”,它存在于positive_keyword列表中。我希望定义一个函数,它将根据列表中任何关键字的出现,使用正则表达式将评论分类为正面或负面。

def sentiment(review, positive_keyword, negative_keyword):

我该怎么做?

mfpqipee

mfpqipee1#

你可以试试这样的东西:

import re

positive_keywords = ['loved', 'outstanding', 'exceeded']
negative_keywords = ['hated', 'not good', 'bad']
reviews = ["I absolutely loved this product! Loving it!", "I hated that situation"]

def classify_sentiment(review, positive_keywords, negative_keywords):
    # Create regular expressions for positive and negative keywords
    positive_pattern = '|'.join(positive_keywords)
    negative_pattern = '|'.join(negative_keywords)
    # Search for positive and negative keywords in the review using regex
    positive_match = re.search(positive_pattern, review, re.IGNORECASE)
    negative_match = re.search(negative_pattern, review, re.IGNORECASE)
    if positive_match and not negative_match:
        return "Positive"
    elif negative_match and not positive_match:
        return "Negative"
    else:
        return "Neutral"
        

for review in reviews:
    sentiment_result = classify_sentiment(review, positive_keywords, negative_keywords)
    print(f"Sentiment: {sentiment_result} ---> {review}")
ia2d9nvy

ia2d9nvy2#

review="I absolutely loved this product! Loving it!"

positive_keyword= ['loved','outstanding', 'exceeded',]

negative_keyword= ['hated','not good', 'bad']

cnt_positive = 0
cnt_negative = 0

for st_pos, st_neg in zip(positive_keyword, negative_keyword):
    match_pos = re.search(st_pos, review)
    match_neg = re.search(st_neg, review)
    if match_pos:
        cnt_positive += len(match_pos.group(0))
    elif match_neg:
        cnt_negative += len(match_neg.group(0))

print(f'match with positive keywods is {cnt_positive} consecutive chars')
print(f'match with negative keywods is {cnt_negative} consecutive chars')

match with positive keywods is 5 consecutive chars
match with negative keywods is 0 consecutive chars

相关问题