python中每个单词的出现次数是一组字符串?

时间:2016-01-07 20:37:43

标签: python python-2.7 python-3.x

我在文本中搜索了以下3个句子,并使用sentence将其添加到列表sentence.append()

例如

sentence[0]=" hello my name is John"
sentence[1]="good morning I am John"
sentence[2]= "hello I am Smith"

我想根据所有3个句子中每个单词的出现次数为每个句子和每个单词分配一个分数。

例如:

Hello score= 2 since it appeared twice **SOLVED**
sentence[0] score= hello score( which is 2) + my (1) + name (1) + is (1) + John(2) = 6 

所以我用它来计算句子中每个单词的出现次数(得分),我的问题是如何用它来计算句子的分数?

dict = {}
for sentenceC in sentence:
    for word in re.split('\s', sentenceC): # split with whitespace
        try:
            dict[word] += 1
        except KeyError:
            dict[word] = 1
print (dict)

2 个答案:

答案 0 :(得分:0)

将问题分解为子任务

def getWordScores(sentences):
   scores = {}
   for sentence in sentences:
      for word in sentence.strip().split():
          word = word.lower()
          scores[word] = scores.get(word,0) + 1
   return scores

def getSentenceScore(sentence, word_scores):
   return sum(word_scores.get(w.lower(), 0) for w in sentence.strip().split())

然后撰写任务以获得解决方案

word_scores = getWordScores(sentence)
print word_scores['hello']
print getSentenceScore(sentence[0], word_scores)

答案 1 :(得分:0)

你可以得到这样的分数:

import re

sentence = list()

sentence.append(" hello my name is John")
sentence.append("good morning I am John")
sentence.append("hello I am Smith")

value = dict()
for sentenceC in sentence:
    for word in sentenceC.strip().split(" "): # split with whitespace
        try:
            value[word.lower()] += 1
        except KeyError:
            value[word.lower()] = 1
print (value)

score = dict()
number = 1
for sentenceC in sentence:
    for word in sentenceC.strip().split(" "): # split with whitespace
        try:
            score[number] += value[word.lower()]
        except KeyError:
            score[number] = value[word.lower()]
    number += 1

print score

#output: {1: 7, 2: 8, 3: 7}