harvesttext package

Submodules

harvesttext.harvesttext module

class harvesttext.harvesttext.HarvestText(standard_name=False)[源代码]

基类:object

add_entities(entity_mention_dict, entity_type_dict=None)[源代码]
add_new_entity(entity0, mention0=None, type0='添加词')[源代码]
add_new_mentions(entity_mention_dict)[源代码]
add_new_words(new_words)[源代码]
add_typed_words(type_word_dict)[源代码]
analyse_sent(sent)[源代码]
build_entity_ego_graph(docs, word, min_freq=0, other_min_freq=-1, inv_index={}, used_types=[])[源代码]

Entity only version of build_word_ego_graph()

build_entity_graph(docs, min_freq=0, inv_index={}, used_types=[])[源代码]
build_index(docs, with_entity=True, with_type=True)[源代码]
build_sent_dict(sents, method='PMI', min_times=5, ft_size=100, ft_epochs=15, ft_window=5, pos_seeds=[], neg_seeds=[])[源代码]
build_trie(new_word, entity, entity_type)[源代码]
build_word_ego_graph(docs, word, standard_name=True, min_freq=0, other_min_freq=-1, stopwords=None)[源代码]

根据文本和指定限定词,获得以限定词为中心的各词语的关系。 限定词可以是一个特定的方面(衣食住行这类文档),这样就可以从词语中心图中获得关于这个方面的简要信息 :param docs: 文本的列表 :param word: 限定词 :param standard_name: 把所有实体的指称化为标准实体名 :param stopwords: 需要过滤的停用词 :param min_freq: 作为边加入到图中的与中心词最小共现次数,用于筛掉可能过多的边 :param other_min_freq: 中心词以外词语关系的最小共现次数 :return: G(networxX中的Graph)

check_prepared()[源代码]
choose_from(surface0, entity_types)[源代码]
choose_from_multi_mentions(mention_cands, sent='')[源代码]
clear()[源代码]
cut_sentences(para, drop_empty_line=True)[源代码]
decoref(sent, entities_info)[源代码]
deprepare()[源代码]
dig_trie(sent, l)[源代码]
entity_linking(sent, pinyin_recheck=False, char_recheck=False)[源代码]
find_entity_with_rule(text, rulesets=[], add_to_dict=True, type0='添加词')[源代码]

利用规则从分词结果中的词语找到实体,并可以赋予相应的类型再加入实体库 :param text: string, 一段文本 :param rulesets: list of (tuple of rules or single rule) from match_patterns, list中包含多个规则,满足其中一种规则的词就认为属于这个type 而每种规则由tuple或单个条件(pattern)表示,一个词必须满足其中的一个或多个条件。 :param add_to_dict: 是否把找到的结果直接加入词典 :param type0: 赋予满足条件的词语的实体类型, 仅当add_to_dict时才有意义 :return: found_entities

get_entity_counts(docs, inv_index, used_type=[])[源代码]
get_linking_mention_candidates(sent, pinyin_recheck=False, char_recheck=False)[源代码]
get_pinyin_correct_candidates(word)[源代码]
get_summary(docs, topK=5, stopwords=None, with_importance=False, standard_name=True)[源代码]
mention2entity(mention)[源代码]

找到单个指称对应的实体 :param mention: 指称 :return: 如果存在对应实体,则返回(实体,类型),否则返回None, None

posseg(sent, standard_name=False, stopwords=None)[源代码]
prepare()[源代码]
remove_entity(entity)[源代码]
remove_mention(mention)[源代码]
search_entity(query, docs, inv_index)[源代码]
search_word_trie(word, tolerance=1)[源代码]
参数:
  • word
  • tolerance
返回:

seg(sent, standard_name=False, stopwords=None, return_sent=False)[源代码]
set_linking_strategy(strategy, lastest_mention=None, entity_freq=None, type_freq=None)[源代码]

为实体链接设定一些简单策略,目前可选的有: ‘None’,’freq’,’latest’,’latest&freq’ ‘None’: 默认选择候选实体字典序第一个 ‘freq’: 对于单个字面值,选择其候选实体中之前出现最频繁的一个。

对于多个重叠字面值,选择其中候选实体出现最频繁的一个进行连接【每个字面值已经确定唯一映射】。

‘latest’: 对于单个字面值,如果在最近有可以确定的映射,就使用最近的映射。 ‘latest’- 对于职称等作为代称的情况可能会比较有用。 比如”经理”可能代指很多人,但是第一次提到的时候应该会包括姓氏。 我们就可以记忆这次信息,在后面用来消歧。 ‘freq’ - 单字面值例:’市长’+{‘A市长’:5,’B市长’:3} -> ‘A市长’ 重叠字面值例,’xx市长江yy’+{‘xx市长’:5,’长江yy’:3}+{‘市长’:’xx市长’}+{‘长江’:’长江yy’} -> ‘xx市长’ :param strategy: 可选 ‘None’,’freq’,’latest’,’latest&freq’ 中的一个 :param lastest_mention: dict,用于’latest’,预设 :param entity_freq: dict,用于’freq’,预设某实体的优先级(词频) :param type_freq: dict,用于’freq’,预设类别所有实体的优先级(词频) :return None

word_discover(doc, threshold_seeds=[], auto_param=True, excluding_types=[], excluding_words=[], max_word_len=5, min_freq=5e-05, min_entropy=1.4, min_aggregation=50, ent_threshold='both', mem_saving=0)[源代码]

harvesttext.match_patterns module

harvesttext.match_patterns.AllEnglish()[源代码]
harvesttext.match_patterns.AllEnglishOrNum()[源代码]
harvesttext.match_patterns.Contains(span)[源代码]
harvesttext.match_patterns.EndsWith(suffix)[源代码]
harvesttext.match_patterns.StartsWith(prefix)[源代码]
harvesttext.match_patterns.UpperFirst()[源代码]
harvesttext.match_patterns.WithLength(length)[源代码]

harvesttext.resources module

harvesttext.resources.get_baidu_stopwords()[源代码]

获得百度停用词列表 来源,网上流传的版本:https://wenku.baidu.com/view/98c46383e53a580216fcfed9.html 包含了中英文常见词及部分标点符号 :return: stopwords: set of string

harvesttext.resources.get_qh_sent_dict()[源代码]

获得参考褒贬义词典: 褒贬义词典 清华大学 李军

此资源被用于以下论文中: Jun Li and Maosong Sun, Experimental Study on Sentiment Classification of Chinese Review using Machine Learning Techniques, in Proceding of IEEE NLPKE 2007 李军 中文评论的褒贬义分类实验研究 硕士论文 清华大学 2008 :return: qh_sent_dict = {“pos”:[words],”neg”:[words]}

harvesttext.resources.get_qh_typed_words(used_types=['IT', '动物', '医药', '历史人名', '地名', '成语', '法律', '财经', '食物'])[源代码]

THUOCL:清华大学开放中文词库 http://thuocl.thunlp.org/ IT 财经 成语 地名 历史名人 诗词 医学 饮食 法律 汽车 动物 :param used_types: :return: typed_words: 字典,键为类型,值为该类的词语组成的set

harvesttext.resources.get_sanguo()[源代码]

获得三国演义原文 :return: [“章节1文本”,”章节2文本”,…]

harvesttext.resources.get_sanguo_entity_dict()[源代码]

获得三国演义中的人名、地名、势力名的知识库。 自行搭建的简单版,一定有遗漏和错误,仅供参考使用 :return: entity_mention_dict,entity_type_dict

harvesttext.sent_dict module

class harvesttext.sent_dict.SentDict(docs=[], method='PMI', min_times=5, ft_size=100, ft_epochs=15, ft_window=5, pos_seeds=[], neg_seeds=[])[源代码]

基类:object

FastTextSim(w1, w2)[源代码]
PMI(w1, w2)[源代码]
SO_FastText(words)[源代码]
SO_PMI(words)[源代码]
analyse_sent(words)[源代码]
build_sent_dict(docs=[], method='PMI', min_times=5, ft_size=100, ft_epochs=15, ft_window=5, pos_seeds=[], neg_seeds=[])[源代码]
get_word_stat(docs, co=True)[源代码]
set_neg_seed(neg_seeds)[源代码]
set_pos_seeds(pos_seeds)[源代码]

harvesttext.word_discoverer module

class harvesttext.word_discoverer.WordDiscoverer(doc, max_word_len=5, min_freq=5e-05, min_entropy=2.0, min_aggregation=50, ent_threshold='both', mem_saving=0)[源代码]

基类:object

genWords(doc)[源代码]

Generate all candidate words with their frequency/entropy/aggregation informations @param doc the document used for words generation

genWords2(doc)[源代码]

Generate all candidate words with their frequency/entropy/aggregation informations @param doc the document used for words generation

get_df_info(ex_mentions)[源代码]
class harvesttext.word_discoverer.WordInfo(text)[源代码]

基类:object

Store information of each word, including its freqency, left neighbors and right neighbors

compute(length)[源代码]

Compute frequency and entropy of this word @param length length of the document for training to get words

computeAggregation(words_dict)[源代码]

Compute aggregation of this word @param words_dict frequency dict of all candidate words

update(left, right)[源代码]

Increase frequency of this word, then append left/right neighbors @param left a single character on the left side of this word @param right as left is, but on the right side

harvesttext.word_discoverer.entropyOfList(cnt_dict)[源代码]
harvesttext.word_discoverer.genSubparts(string)[源代码]

Partition a string into all possible two parts, e.g. given “abcd”, generate [(“a”, “bcd”), (“ab”, “cd”), (“abc”, “d”)] For string of length 1, return empty list

harvesttext.word_discoverer.genSubstr(string, n)[源代码]

Generate all substrings of max length n for string

harvesttext.word_discoverer.indexOfSortedSuffix(doc, max_word_len)[源代码]

Treat a suffix as an index where the suffix begins. Then sort these indexes by the suffixes.

Module contents

harvesttext.loadHT(filename)[源代码]
harvesttext.saveHT(htModel, filename)[源代码]