我有一份客户评论列表,我必须使用正则表达式 (regex) 将它们分类为正面或负面。
这是客户评论的示例,正面关键字和负面关键字的列表。
review="I absolutely loved this product! Loving it!"
positive_keyword= ['loved','outstanding', 'exceeded']
negative_keyword= ['hated','not good', 'bad']
上面的示例评论将由于“loved”的出现而被分类为正面评论,“loved”出现在 Positive_keyword 列表中。我希望定义一个函数,根据任一列表中任何关键字的出现情况,使用正则表达式将评论分类为正面或负面。
def sentiment(review, positive_keyword, negative_keyword):
我该怎么做?
你可以尝试这样的事情:
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}")