weixin_48989635 2020-11-07 11:48 采纳率: 0%
浏览 41

请教各位大佬如何统计句子中出现相应类别词语的次数

评论数据,我已经把评论数据分词好了,然后也建立了不同类别的字典txt文件,如果评论里包含相应类别字典(有4个相应字典表)里的词语,
就在excel表创立相应类别里+1,这个请问可以怎么实现呢?
if count可以吗
  • 写回答

1条回答 默认 最新

  • 幻影123! 2023-10-15 20:39
    关注

    望采纳

    
    # 读取词典,每行一个词
    with open('dictionary.txt', 'r', encoding='utf-8') as f:
        dictionary = set(line.strip() for line in f)
    
    # 读取句子
    sentence = input('请输入句子:')
    
    # 统计词频
    word_count = {}
    for word in sentence:
        if word in dictionary:
            if word in word_count:
                word_count[word] += 1
            else:
                word_count[word] = 1
    
    # 输出结果
    for word, count in word_count.items():
        print(f'{word}: {count}')
    
    评论

报告相同问题?