字串符英文标点转空格(03)

学校作业 专栏收录该内容
6 篇文章 1 订阅

Given a hotel review as

  • "The location is great. Took advantage of the downtown location to walk to dinner, check out a couple galleries, and have drinks. It was great. I highly recommend this hotel for anyone visiting downtown. However, the condition of the rooms were bad. Bed sheets, linens were dirty. Toilet was horrible."

and a list of positive words as

  • ['good', 'nice', 'friendly','great', 'clean', 'comfortable', 'amazing', 'enjoyable', 'wonderful', 'great'].

and a list of negative words as

  • ['poor', 'bad', 'unfriendly','horrible', 'dirty', 'uncomfortable' ].

Please caculate the percentage of positive words, negative words, and neutral words in this review.

import string
txt="The location is great . Took advantage of the downtown location \
to walk to dinner, check out a couple galleries ,\
and have drinks . It was great . I highly recommend this\
hotel for anyone visiting downtown .\
However , the condition of the rooms were bad . Bed sheets ,\
linens were dirty . Toilet was horrible ."

for i in txt:
    if i in string.punctuation:  #如果字符是标点符号的话就将其替换为空格
        txt = txt.replace(i," ")
txt=list(txt.split())
txt_lens=len(txt)

positive_list=['good', 'nice', 'friendly','great', 'clean', 'comfortable', 'amazing', 'enjoyable', 'wonderful', 'great']
positive_dict={}
negative_list=['poor', 'bad', 'unfriendly','horrible', 'dirty', 'uncomfortable' ]
negative_dict={}

for i in positive_list:
    if i in txt:
        positive_dict.setdefault(i,0)
        positive_dict[i]+=1
print(positive_dict)
pstv=sum(positive_dict.values())

for i in negative_list:
    if i in txt:
        negative_dict.setdefault(i,0)
        negative_dict[i]+=1
print(negative_dict)
ngtv=sum(negative_dict.values())

print('the percentage of positive words is :',pstv/txt_lens)
print('the percentage of negative words is :',ngtv/txt_lens)

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值