util.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:JustCopy 作者: exe1023 项目源码 文件源码
def sort_sentences(sentences, words, sim_func = get_similarity, pagerank_config = {'alpha': 0.85,}):
    """???????????????

    Keyword arguments:
    sentences         --  ????????
    words             --  ?????????sentences???????????????
    sim_func          --  ????????????????????????
    pagerank_config   --  pagerank???
    """
    sorted_sentences = []
    _source = words
    sentences_num = len(_source)        
    graph = np.zeros((sentences_num, sentences_num))

    for x in xrange(sentences_num):
        for y in xrange(x, sentences_num):
            similarity = sim_func( _source[x], _source[y] )
            graph[x, y] = similarity
            graph[y, x] = similarity

    nx_graph = nx.from_numpy_matrix(graph)
    scores = nx.pagerank(nx_graph, **pagerank_config)              # this is a dict
    sorted_scores = sorted(scores.items(), key = lambda item: item[1], reverse=True)

    for index, score in sorted_scores:
        item = AttrDict(index=index, sentence=sentences[index], weight=score)
        sorted_sentences.append(item)

    return sorted_sentences
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号