0

我有一个字符串,字典形式为:

('The puppy likes flowers',
 {'laughter': (8.5, 0.9313),
  'flowers': (7.88, 1.1718),
  'the': (4.98, 0.9145),
  'puppy': (7.58, 1.4581),
  'died': (1.56, 1.198),
  'laugh': (9.5, 0.1),
  'flow': (2.3, 0.51),
  'likes':(5.9, 0.032),
  'like':(6.5, 0.021)    
   }
  )

每个括号是对应于(分数,标准差)的元组。我只取每个元组中第一个整数的平均值。我试过这个:

def score(string, d):
    if len(string) == 0:
        return 0
    string = string.lower()
    included = [d[word][0]for word in d if word in string]
    return sum(included) / len(included)

当我运行时:

print score ('The puppy likes flower', {'laughter': (8.5, 0.9313), 'flower': 
(7.88, 1.1718), 'the':(4.98, 0.9145), 'puppy':(7.58, 1.4581), 
'died':(1.56, 1.198),'laugh': (9.5, 0.1),'flow': (2.3, 0.51)})

我应该只得到'the','puppy', 'likes''flowers':的平均值,4.98 + 7.88 + 5.9 + 7.58 / 4但是这个运行函数还包括'like''flow': 4.98 + 7.88 + 5.9 + + 7.58 + 6.5 + 2.3 / 6

4

5 回答 5

2

首先使用变量字符串不是一个好主意...但是在这里可以...您的逻辑有缺陷...以下有效

def avg(l):
    if l:
        return sum(l)/len(l)
    return 0

def score(s, d):
    return avg([d.get(x,[0])[0] for x in s.lower().split()])

这将为s不在其中的字符串添加 0 d... 如果您想忽略它们,请改用以下内容

def score(s, d):
    return avg([d[x][0] for x in s.lower().split() if x in d])
于 2012-10-23T05:12:04.970 回答
0

您应该首先拆分字符串:

splited_string = string.split()
included = [d[word][0]for word in d if word in splited_string]
于 2012-10-23T04:55:51.207 回答
0

你可以在下面的函数中得到这部分,但我决定稍微清理一下你的元组:

tuple = ('The puppy likes flowers',
 {'laughter': (8.5, 0.9313),
  'flowers': (7.88, 1.1718),
  'the': (4.98, 0.9145),
  'puppy': (7.58, 1.4581),
  'died': (1.56, 1.198),
  'laugh': (9.5, 0.1),
  'flow': (2.3, 0.51),
  'likes':(5.9, 0.032),
  'like':(6.5, 0.021)    
   }
  )

string = tuple[0]
dict = tuple[1]

现在定义我们的函数:

def score(string, dict):
    s = 0
    n = 0
    for each in string.lower().split(' '):
       if each in dict.keys():
          s += dict[each][0]
          n += 1
    average = s/n
    return average

在你的情况下:

In [43]: string
Out[43]: 'The puppy likes flowers'

In [44]: dict
Out[44]: 
{'died': (1.56, 1.198),
 'flow': (2.3, 0.51),
 'flowers': (7.88, 1.1718),
 'laugh': (9.5, 0.1),
 'laughter': (8.5, 0.9313),
 'like': (6.5, 0.021),
 'likes': (5.9, 0.032),
 'puppy': (7.58, 1.4581),
 'the': (4.98, 0.9145)}

评估功能:

In [45]: score(string, dict)
Out[45]: 6.585
于 2012-10-23T05:05:53.260 回答
0

而不是使用python的'in'操作尝试使用==,即编辑:

string = string.split(' ') #Returns a list of word

included = [d[word][0]for word in d if word == string]
于 2012-10-23T05:15:31.313 回答
0

与到目前为止的其他答案一样,此答案在字典中查找从输入字符串中拆分出来的单词的分数,这与您的示例代码所做的不同,即查找字典单词作为输入字符串的一部分并将其相加分数。此外,此答案的逻辑与其他一些答案的逻辑相似,但通过使用 python 的内置filter函数可以更紧凑地表达。下面显示的程序的输出是6.585, 6.15333333333, None,6.032四行。

w={'puppy': (7.58, 1.4581), 'likes': (5.9, 0.032), 'laugh': (9.5, 0.1), 'flow': (2.3, 0.51), 'the': (4.98, 0.9145), 'flowers': (7.88, 1.1718), 'laughter': (8.5, 0.9313), 'died': (1.56, 1.198), 'like': (6.5, 0.021)}

def score(s, d):
    v = [d[a][0] for a in filter(lambda x: x in d, s.lower().split())]
    return sum(v)/len(v) if len(v) else None

print score('the puppy likes flowers', w)
print score('the puppy likes flower', w)
print score('short stuff', w)
print score('the flowers flow like laughter', w)
于 2012-10-23T06:29:42.533 回答