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


Python Lexicon.add方法代码示例

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


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

示例1: _estimate_relations

# 需要导入模块: from lexicon import Lexicon [as 别名]
# 或者: from lexicon.Lexicon import add [as 别名]
def _estimate_relations(cache, docs=None):
    lexicon = Lexicon()

    questions = {}
    question_objects = Question.objects.all()
    n_questions = question_objects.count()
    for (i, question) in enumerate(question_objects):
        print >>sys.stderr, "Handling question %i/%i"%(i+1, n_questions)
        name = 'question_%i' % question.id

        if name not in cache:
            words = text_to_word_features(question.text)

            for option in question.option_set.all():
                words.extend(text_to_basewords(option.name))

            for answer in question.answer_set.all():
                if(answer.explanation is None): continue
                words.extend(text_to_basewords(answer.explanation))

            cache[name] = ' '.join(words)
        else:
            words = cache[name].split(' ')

        lexicon.add(*words)

        itemlexicon = Lexicon()
        itemlexicon.add(*words)
        questions[question] = itemlexicon

    sessions = {}
    handled = []
    session_objects = Session.objects.filter(subject__contains=u'Hyv')
    n_sessions = session_objects.count()
    for (i, session) in enumerate(session_objects):
        #if session.info in handled:
        #    continue
        #handled.append(session.info)
        print >>sys.stderr, "Handling session %i/%i"%(i+1, n_sessions)
        # TODO: Filter by actual period

        if session.plenary_session.date.year < 2007:
            continue

        words = get_session_word_features(session, cache, docs)
       
        if len(words) == 0:
            continue

        lexicon.add(*words)

        itemlexicon = Lexicon()
        itemlexicon.add(*words)
        sessions[session] = itemlexicon
        
    prod = itertools.product(questions.keys(), sessions.keys())
    minr = 1
    maxr = 0
    relevances = []
    for (question, session) in prod:
        relevance = lexicon_relevance(questions[question], sessions[session],
                                      lexicon)
        if relevance < minr:
            minr = relevance
        if relevance > maxr:
            maxr = relevance
        relevances.append((relevance, question, session))

    # TODO: This is not very nice, we should have a better
    # ....relevancy measure instead
    relevances = map(lambda (r, q, s): ((r - minr) / (maxr - minr), q, s),
                     relevances)

    for (relevance, question, session) in relevances:
        try:
            record = QuestionSessionRelevance.objects.get(
                            question=question,
                            session=session, user=None)
        except QuestionSessionRelevance.DoesNotExist:
            record = QuestionSessionRelevance(relevance=relevance,
                        question=question,
                        session=session, user=None)
        
        record.relevance = relevance
        record.save()
开发者ID:jampekka,项目名称:kamu,代码行数:87,代码来源:estimate_relations.py


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