2

拿这本词典:

{'local': {'count': 7,
    'dining-and-nightlife': {'count': 1,
        'bar-clubs': {'count': 1}
    },
    'activities-events': {'count': 6,
        'outdoor-adventures': {'count': 4},
        'life-skill-classes': {'count': 2}
    }
}}

我如何确定最相关的匹配(在 30% 的余地内)?例如,activity-events 的计数为 6,因此 6/7 = 85%,其子户外冒险的计数为 4 out 6 (66%)。因此,最相关的类别是户外探险。

在这个例子中:

{'local': {'count': 11,
    'dining-and-nightlife': {'count': 4,
        'bar-clubs': {'count': 4}
    },
    'activities-events': {'count': 6,
        'outdoor-adventures': {'count': 4},
        'life-skill-classes': {'count': 2}
    }
}}

将餐饮和夜生活 (33%) 与酒吧俱乐部 (100%) 结合起来,将活动活动 (54%) 与户外探险 (66%) 结合起来。

我希望百分比截止值由

cutoff = 0.3

这里的想法是确定哪个类别最相关,删除较小的结果(低于 30%)匹配。

@FJ 在下面回答了这个问题,但现在我希望更新树中的计数。

初始输出:

{'local': {'activities-events': {'count': 6,
                             'life-skill-classes': {'count': 2},
                             'outdoor-adventures': {'count': 4}},
       'count': 11,
       'dining-and-nightlife': {'bar-clubs': {'count': 4}, 'count': 4}}}

帖子输出:

{'local': {'activities-events': {'count': 6,
                             'life-skill-classes': {'count': 2},
                             'outdoor-adventures': {'count': 4}},
       'count': 10,
       'dining-and-nightlife': {'bar-clubs': {'count': 4}, 'count': 4}}}
4

2 回答 2

1

以下应该有效,请注意,这将修改您的输入字典:

def keep_most_relevant(d, cutoff=0.3):
    for k, v in list(d.items()):
        if k == 'count':
            continue
        if 'count' in d and v['count'] < d['count'] * cutoff:
            del d[k]
        else:
            keep_most_relevant(v)

例子:

>>> d1 = {'local': {'count': 7, 'dining-and-nightlife': {'count': 1, 'bar-clubs': {'count': 1}}, 'activities-events': {'count': 6, 'outdoor-adventures': {'count': 4}, 'life-skill-classes': {'count': 2}}}}
>>> keep_most_relevant(d1)
>>> pprint.pprint(d1)
{'local': {'activities-events': {'count': 6,
                                 'life-skill-classes': {'count': 2},
                                 'outdoor-adventures': {'count': 4}},
           'count': 7}}

>>> d2 = {'local': {'count': 11, 'dining-and-nightlife': {'count': 4, 'bar-clubs': {'count': 4}}, 'activities-events': {'count': 6, 'outdoor-adventures': {'count': 4}, 'life-skill-classes': {'count': 2}}}}
>>> keep_most_relevant(d2)
>>> pprint.pprint(d2)
{'local': {'activities-events': {'count': 6,
                                 'life-skill-classes': {'count': 2},
                                 'outdoor-adventures': {'count': 4}},
           'count': 11,
           'dining-and-nightlife': {'bar-clubs': {'count': 4}, 'count': 4}}}
于 2012-10-10T23:54:08.260 回答
0
def matches(match, cutoff):
    total = float(match['count'])

    for k in match:
        if k == 'count':
            continue

        score = match[k]['count'] / total

        if score >= cutoff:
            yield (k, score)

            m = list(matches(match[k], cutoff))
            if m: yield max(m, key=lambda (c, s): s)

def best_matches(d, cutoff):
    for k in d:
        for m in matches(d[k], cutoff):
            yield m

测试 1

>>> d = {'local': {'count': 7,
    'dining-and-nightlife': {'count': 1,
        'bar-clubs': {'count': 1}
    },
    'activities-events': {'count': 6,
        'outdoor-adventures': {'count': 4},
        'life-skill-classes': {'count': 2}
    }
}}
>>> print list(best_matches(d, 0.3))
[('activities-events', 0.8571428571428571), ('outdoor-adventures', 0.66666666666666663)]

测试 2

>>> d = {'local': {'count': 11,
    'dining-and-nightlife': {'count': 4,
        'bar-clubs': {'count': 4}
    },
    'activities-events': {'count': 6,
        'outdoor-adventures': {'count': 4},
        'life-skill-classes': {'count': 2}
    }
}}
>>> print list(best_matches(d, 0.3))
[('dining-and-nightlife', 0.36363636363636365), ('bar-clubs', 1.0), ('activities-events', 0.54545454545454541), ('outdoor-adventures', 0.66666666666666663)]
于 2012-10-11T00:03:55.463 回答