HarvestText Documentation

repository·master·Indexed 25 days ago

https://github.com/blmoistawinde/harvesttext

A Python toolkit for text mining and preprocessing specializing in unsupervised and weakly supervised methods. It provides capabilities for entity linking, Named Entity Recognition (NER), sentiment analysis, relationship network modeling, and text summarization. Key features include fine-grained segmentation, dependency parsing, new word discovery, and built-in domain-specific dictionaries for IT, Finance, Food, and Law.

Tokens
5.6K
Snippets
18
Records
32
Agent score
83%

What's inside HarvestText

  1. Overview of HarvestText capabilities

    master

    HarvestText is a toolkit for text mining and preprocessing designed with a focus on unsupervised (or weak supervision) methods. It allows developers to integrate domain knowledge (such as types and aliases) to process and analyze specific domain texts efficiently. It is suitable for tasks like novel analysis, web text processing, and professional literature exploration.

    Key functional areas include:

    • Basic Processing: Fine-grained segmentation, text cleaning (URLs, emails, Weibo formats), entity linking, Named Entity Recognition (NER), dependency parsing, and new word discovery.
    • High-level Applications: Sentiment analysis, relationship network construction, text summarization (TextRank), keyword extraction, fact extraction (triplets), and simple Question Answering (QA) systems.
    • Built-in Resources: Includes general stop words, sentiment lexicons, and domain-specific dictionaries (IT, Finance, Food, Law, etc.).
  2. Perform information retrieval with build_index and search_entity

    master

    Quickly search for documents containing specific entities (or their aliases) using an inverted index.

    1. Build Index: Use ht.build_index(docs) to create the index.
    2. Count Entities: Use ht.get_entity_counts(docs, inv_index) to get the frequency of all entities in the documents.
    3. Search:
      • Single entity: ht.search_entity("EntityName", docs, inv_index)
      • Multiple entities (co-occurrence): ht.search_entity("EntityA EntityB", docs, inv_index)
      • Mixed entity and type: ht.search_entity("#Type# EntityName", docs, inv_index)
    4. Filter by Type: Use ht.get_entity_counts(docs, inv_index, used_type=["Type"]) to restrict counts to specific entity types.
  3. Install HarvestText

    master

    You can install HarvestText using pip or by running the setup.py file from the repository directory.

    Note that some features require additional libraries. If installation fails or features are missing, you may need to install them manually:

    • For English language features: pip install pattern
    • For Named Entity Recognition (NER) and Dependency Parsing: pip install pyhanlp (requires Python <= 3.8).
    pip install --upgrade harvesttext
  4. Enable English support in HarvestText

    master

    While primarily designed for Chinese data mining, HarvestText supports limited English features like sentiment analysis, sentence segmentation, and word segmentation. To use these, you must instantiate HarvestText with language="en".

    ht_eng = HarvestText(language="en")
  5. Filter existing words using a Jieba dictionary

    master

    You can prevent the discovery of known words by providing an existing dictionary (e.g., from Jieba) to the word_discover method using the excluding_words parameter. Use exclude_number=True to also exclude numeric values from discovery.

    from harvesttext.resources import get_jieba_dict
    jieba_dict = get_jieba_dict(min_freq=100)
    text = "1979-1998-2020的喜宝们 我现在记忆不太好..."
    new_words_info = ht.word_discover(text, 
                                        excluding_words=set(jieba_dict),       # Exclude words already in dictionary
                                        exclude_number=True)                   # Exclude numbers (default True)     
    new_words = new_words_info.index.tolist()
    print(new_words)                                                         # ['喜宝']
  6. Use NaiveKGQA for simple Question Answering

    master

    The NaiveKGQA class provides a mechanism for answering questions based on provided SVOs (Subject-Verb-Object triples) and an entity type dictionary. It can answer questions about historical figures, relationships, and events.

    QA = NaiveKGQA(SVOs, entity_type_dict=entity_type_dict)
    questions = ["你好","孙中山干了什么事?","谁发动了什么?","清政府签订了哪些条约?",
    		 "英国与鸦片战争的关系是什么?","谁复辟了帝制?"]
    for question0 in questions:
    	print("问:"+question0)
    	print("答:"+QA.answer(question0))
  7. Discover new words with word_discover

    master

    Identify new words from large amounts of text using statistical indicators. You can provide seed words to guide the quality of discovery.

    Use ht.word_discover(text, threshold_seeds=["SeedWord"]). The function returns a pandas.DataFrame containing quality information about the discovered words. You can extract the list of words using .index.tolist().

    para = "上港的武磊和恒大的郜林..."
    new_words_info = ht.word_discover(para, threshold_seeds=["武磊"])
    new_words = new_words_info.index.tolist()
  8. Build entity relationship networks with build_entity_graph

    master

    Create a graph structure (returning a networkx.Graph object) based on word co-occurrence to model relationships between entities, such as social networks.

    • Add Entities: Use ht.add_new_entity(name, mention, type) to expand the entity library.
    • Build Graph: Use ht.build_entity_graph(docs, used_types=["Type"]) to generate the graph. You can optionally restrict the graph to specific entity types.
    ht.add_new_entity("颜骏凌", "颜骏凌", "球员")
    docs = ["武磊和颜骏凌是队友", "武磊和郜林都是国内顶尖前锋"]
    G = ht.build_entity_graph(docs, used_types=["球员"])
  9. Save and Load HarvestText models

    master

    You can persist a HarvestText object to disk using saveHT and reload it later using loadHT. This allows you to preserve trained states or specific configurations.

    from harvesttext import loadHT,saveHT
    para = "上港的武磊和恒大的郜林,谁是中国最好的前锋?那当然是武磊武球王了,他是射手榜第一,原来是弱点的单刀也有了进步"
    saveHT(ht,"ht_model1")
    ht2 = loadHT("ht_model1")
    
    # Clear records in the loaded model
    ht2.clear()
    print("cut with cleared model")
    print(ht2.seg(para))
  10. Register new words and entities for segmentation

    master

    To ensure that discovered keywords are prioritized during subsequent segmentation, you can register them as new words or specific entities.

    • Use add_new_words(list_of_words) to register words as general 'new words'.
    • Use add_new_entity(word, mention0=..., type0=...) to register a word as a specific type of entity (e.g., a technical term).
    def new_word_register():
        new_words = ["落叶球","666"]
        ht.add_new_words(new_words)   # Register as general "new words"
        ht.add_new_entity("落叶球", mention0="落叶球", type0="术语")  # Register as a specific type
        print(ht.seg("这个落叶球踢得真是666", return_sent=True))
        for word, flag in ht.posseg("这个落叶球踢得真是666"):
            print("%s:%s" % (word, flag), end=" ")