classify函数的python代码和测试
时间: 2024-05-07 07:18:49 浏览: 104
以下是一个简单的 `classify` 函数的Python代码和测试:
```python
def classify(text):
positive_words = ['good', 'awesome', 'fantastic', 'amazing', 'love']
negative_words = ['bad', 'terrible', 'horrible', 'awful', 'hate']
# Count the number of positive and negative words in the text
num_pos_words = sum(1 for word in text.split() if word in positive_words)
num_neg_words = sum(1 for word in text.split() if word in negative_words)
# Classify the text based on the ratio of positive to negative words
if num_pos_words > num_neg_words:
return 'Positive'
elif num_pos_words < num_neg_words:
return 'Negative'
else:
return 'Neutral'
```
测试代码:
```python
assert classify("I love this product!") == 'Positive'
assert classify("This is terrible!") == 'Negative'
assert classify("I'm feeling neutral about this.") == 'Neutral'
```
阅读全文