当前位置: 首页>>代码示例>>Python>>正文


Python Histogram.iterkeys方法代码示例

本文整理汇总了Python中histogram.Histogram.iterkeys方法的典型用法代码示例。如果您正苦于以下问题:Python Histogram.iterkeys方法的具体用法?Python Histogram.iterkeys怎么用?Python Histogram.iterkeys使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在histogram.Histogram的用法示例。


在下文中一共展示了Histogram.iterkeys方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: classify

# 需要导入模块: from histogram import Histogram [as 别名]
# 或者: from histogram.Histogram import iterkeys [as 别名]
def classify(token_list):
    out = dict()
    tokens = copy(token_list)
    
    # from pprint import pprint
    # pprint(FIELD_DMV)
    
    # first, sanity-check the digified tokens --
    # if more than one can pass for a phone number, a color,
    # or a zip code (that is to say, the input is ambiguous),
    # we bail:
    for token in tokens:
        # check each term against all test funcs --
        # if more than one bucket is nonzero, it's a problem
        h = Histogram()
        for FieldType in FIELD_DMV:
            if FieldType.check(token):
                h.inc(FieldType.json_name)
        if len(h) > 1:
            # ERROR: couldn't distinguish one thing
            # from another... BAIL
            raise RDAmbiguousTerms("Token '%s' parsed ambiguously\n"
                                   "Passed multiple field checks: %s" % (
                                       token, SEP_WS.join(h.iterkeys())
                                   ))
    
    # update `out` with the classified tokens
    for idx, token in enumerate(copy(tokens)):
        for FieldType in FIELD_DMV:
            if FieldType.check(token):
                field = FieldType()
                out.update(field.value_for_token(token))
                tokens.remove(token)
            continue
    
    # raise appropriate errors when we don't find what we need
    for FieldType in FIELD_DMV:
        field = FieldType()
        if not out.has_key(field.name):
            field.unfound(token_list) # this may raise
    
    # what is left "should" be the pieces of the name,
    # e.g. ['Washington', 'Booker T.'], ['James Murphy'], &c
    first_field = FirstNameField()
    last_field = LastNameField()
    if len(tokens) > 2:
        # ERROR: wtf is going on
        pass
    elif len(tokens) == 2:
        out.update(first_field.value_for_token(tokens[-1]))
        out.update(last_field.value_for_token(tokens[0]))
    elif len(tokens) == 1:
        names = tokens[0].split()
        if len(names) > 1:
            out.update(first_field.value_for_token(names[0]))
            out.update(last_field.value_for_token(names[-1]))
        else:
            NameField().unfound(token_list)
    else:
        # WHY ARE WE HERE. No names... really??
        raise RDAmbiguousNames("No names present!"
                               "Reconstructed original line:\n"
                               "\t%s" % reconstruct(token_list))
    
    # pprint(out, indent=4)
    return out
开发者ID:fish2000,项目名称:rolodexer,代码行数:68,代码来源:parser.py


注:本文中的histogram.Histogram.iterkeys方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。