### Install SnowNLP via pip Source: https://context7.com/isnowfy/snownlp/llms.txt The standard command to install the SnowNLP library into your Python environment. ```bash pip install snownlp ``` -------------------------------- ### TextRank for Custom Summarization using SnowNLP Source: https://context7.com/isnowfy/snownlp/llms.txt Provides direct access to TextRank classes (`TextRank`, `KeywordTextRank`) for fine-grained control over keyword and summary extraction. The example demonstrates preparing a document by segmenting and filtering stopwords, then using TextRank to identify top sentences for summarization. ```python from snownlp import normal, seg from snownlp.summary.textrank import TextRank, KeywordTextRank text = ''' 机器学习是人工智能的一个分支。 它使用算法来分析数据并学习模式。 深度学习是机器学习的一个子领域。 神经网络是深度学习的核心技术。 ''' # Prepare document: segment text and filter stopwords sentences = normal.get_sentences(text) doc = [] for sent in sentences: words = seg.seg(sent) words = normal.filter_stop(words) doc.append(words) # TextRank for sentence summarization rank = TextRank(doc) rank.solve() top_sentence_indices = rank.top_index(2) print("Top sentences:") for idx in top_sentence_indices: print(f" {sentences[idx]}") ``` -------------------------------- ### Perform Sentiment Analysis Source: https://context7.com/isnowfy/snownlp/llms.txt Provides examples of calculating sentiment scores for Chinese text and using the sentiment module for custom classification. ```python from snownlp import SnowNLP from snownlp import sentiment s1 = SnowNLP('这个产品非常好用,我很喜欢') print(s1.sentiments) score = sentiment.classify('服务态度很好,下次还会再来') print(score) ``` -------------------------------- ### Initialize SnowNLP and Access Features Source: https://context7.com/isnowfy/snownlp/llms.txt Demonstrates how to initialize the SnowNLP class with Chinese text and access various NLP properties like word segmentation, POS tagging, and sentiment analysis. ```python from snownlp import SnowNLP s = SnowNLP('这个东西真心很赞') print(s.words) print(s.tags) print(s.sentiments) print(s.pinyin) print(s.sentences) ``` -------------------------------- ### Perform Similarity Search with BM25 Source: https://context7.com/isnowfy/snownlp/llms.txt This snippet shows how to initialize the BM25 algorithm with a corpus of tokenized documents. It allows for calculating term frequency, IDF scores, and similarity rankings between a query and the document collection. ```python from snownlp.sim.bm25 import BM25 corpus = [['Python', '编程', '语言', '开发'], ['Java', '编程', '语言', '企业'], ['机器学习', 'Python', '数据', '科学'], ['Web', '开发', 'JavaScript', '前端']] bm25 = BM25(corpus) print(bm25.f) print(bm25.idf) query = ['Python', '开发'] scores = bm25.simall(query) print(scores) score = bm25.sim(query, 0) print(f"Similarity to doc 0: {score}") ``` -------------------------------- ### Execute Part-of-Speech Tagging Source: https://context7.com/isnowfy/snownlp/llms.txt Demonstrates how to retrieve POS tags for words using the SnowNLP class or the tag module directly. ```python from snownlp import SnowNLP from snownlp import tag s = SnowNLP('我今天去北京旅游') print(list(s.tags)) words = ['我', '今天', '去', '北京', '旅游'] print(list(tag.tag(words))) ``` -------------------------------- ### SnowNLP Initialization and Core Features Source: https://context7.com/isnowfy/snownlp/llms.txt The main SnowNLP class provides a unified interface to access NLP features by initializing with a Unicode string. ```APIDOC ## Initialization ### Description Initializes the SnowNLP object with a Chinese text string to enable various NLP analysis properties. ### Method Constructor ### Parameters #### Request Body - **text** (string) - Required - The Chinese text to process (must be Unicode). ### Request Example from snownlp import SnowNLP s = SnowNLP('这个东西真心很赞') ### Response - **words** (list) - Segmented words - **tags** (list) - POS tags - **sentiments** (float) - Sentiment score (0-1) - **pinyin** (list) - Pinyin representation ``` -------------------------------- ### Training Custom Models with SnowNLP Source: https://context7.com/isnowfy/snownlp/llms.txt Enables training custom models for word segmentation, POS tagging, and sentiment analysis using user-provided data. The code demonstrates how to train and save models for each of these components using their respective modules (`seg`, `tag`, `sentiment`). ```python # Training word segmentation model from snownlp import seg # Train on custom data file (space-separated words, one sentence per line) seg.train('custom_seg_data.txt') seg.save('custom_seg.marshal') # Load custom model seg.load('custom_seg.marshal') # Training POS tagging model from snownlp import tag # Train on custom data (word/tag pairs, space-separated) tag.train('custom_tag_data.txt') tag.save('custom_tag.marshal') # Training sentiment analysis model from snownlp import sentiment # Train with separate negative and positive example files sentiment.train('negative_reviews.txt', 'positive_reviews.txt') sentiment.save('custom_sentiment.marshal') # After training, modify the data_path in respective __init__.py files # to point to your custom models, or use load() to switch models at runtime ``` -------------------------------- ### Generate Text Summaries Source: https://context7.com/isnowfy/snownlp/llms.txt Shows how to use the TextRank algorithm to extract the most important sentences from a document for summarization. ```python from snownlp import SnowNLP text = '自然语言处理是计算机科学领域与人工智能领域中的一个重要方向。' s = SnowNLP(text) summary = s.summary(3) for sent in summary: print(sent) ``` -------------------------------- ### Perform Word Segmentation Source: https://context7.com/isnowfy/snownlp/llms.txt Shows how to segment Chinese text into individual words using the SnowNLP class or the lower-level seg module. ```python from snownlp import SnowNLP from snownlp import seg s = SnowNLP('自然语言处理是计算机科学领域与人工智能领域中的一个重要方向') print(s.words) words = seg.seg('我爱北京天安门') print(words) ``` -------------------------------- ### Extract Keywords with TextRank Source: https://context7.com/isnowfy/snownlp/llms.txt Demonstrates how to extract the most relevant keywords from a text block, with options to limit the count and merge adjacent terms. ```python from snownlp import SnowNLP text = '自然语言处理是计算机科学领域与人工智能领域中的一个重要方向。' s = SnowNLP(text) print(s.keywords(5)) print(s.keywords(3, merge=True)) ``` -------------------------------- ### Training Custom Models Source: https://context7.com/isnowfy/snownlp/llms.txt Allows training custom models for segmentation, POS tagging, and sentiment analysis. ```APIDOC ## Training Custom Models SnowNLP allows training custom models for segmentation, POS tagging, and sentiment analysis using your own data. ### Training Word Segmentation Model #### Method N/A (Function calls for training and saving) #### Endpoint N/A #### Parameters - **custom_seg_data.txt** (file path) - File containing custom segmentation data (space-separated words, one sentence per line). #### Request Example ```python from snownlp import seg seg.train('custom_seg_data.txt') seg.save('custom_seg.marshal') ``` ### Training POS Tagging Model #### Method N/A (Function calls for training and saving) #### Endpoint N/A #### Parameters - **custom_tag_data.txt** (file path) - File containing custom POS tagging data (word/tag pairs, space-separated). #### Request Example ```python from snownlp import tag tag.train('custom_tag_data.txt') tag.save('custom_tag.marshal') ``` ### Training Sentiment Analysis Model #### Method N/A (Function calls for training and saving) #### Endpoint N/A #### Parameters - **negative_reviews.txt** (file path) - File containing negative sentiment examples. - **positive_reviews.txt** (file path) - File containing positive sentiment examples. #### Request Example ```python from snownlp import sentiment sentiment.train('negative_reviews.txt', 'positive_reviews.txt') sentiment.save('custom_sentiment.marshal') ``` ### Loading Custom Models After training, you can load custom models at runtime. #### Request Example ```python # Example for loading segmentation model from snownlp import seg seg.load('custom_seg.marshal') ``` ``` -------------------------------- ### Text Similarity Calculation with SnowNLP Source: https://context7.com/isnowfy/snownlp/llms.txt Computes TF-IDF statistics and BM25 similarity scores between documents. SnowNLP must be initialized with a list of word lists (documents) to enable similarity features. The `tf`, `idf`, and `sim` properties/methods are used for these calculations. ```python from snownlp import SnowNLP # Initialize with a corpus of documents (each document is a list of words) docs = [ ['这篇', '文章', '讲述', '自然语言处理'], ['那篇', '论文', '研究', '机器学习'], ['这个', '文档', '关于', '深度学习'] ] s = SnowNLP(docs) # Get term frequencies for each document print(s.tf) # Output: [{'这篇': 1, '文章': 1, ...}, {'那篇': 1, ...}, ...] # Get IDF scores for terms print(s.idf) # Output: {'这篇': 0.405, '文章': 0.405, '那篇': 0.405, ...} # Calculate similarity of a query to all documents query = ['文章', '自然语言处理'] similarity_scores = s.sim(query) print(similarity_scores) # Output: [1.216, 0, 0] # First document most similar ``` -------------------------------- ### Sentence Tokenization using SnowNLP Source: https://context7.com/isnowfy/snownlp/llms.txt Splits Chinese text into individual sentences based on common punctuation marks (。!?;) and line breaks. The `sentences` property of SnowNLP handles this task, and the `normal.get_sentences` function provides an alternative method. ```python from snownlp import SnowNLP text = '''今天天气很好。我们去公园玩吧! 你觉得怎么样?应该会很有趣。''' s = SnowNLP(text) sentences = s.sentences print(sentences) # Output: ['今天天气很好', '我们去公园玩吧', '你觉得怎么样', '应该会很有趣'] # Using normal module directly from snownlp import normal sents = normal.get_sentences('这是第一句。这是第二句!这是第三句?') print(sents) # Output: ['这是第一句', '这是第二句', '这是第三句'] ``` -------------------------------- ### TextRank for Custom Summarization Source: https://context7.com/isnowfy/snownlp/llms.txt Utilizes TextRank classes for fine-grained control over keyword and summary extraction. ```APIDOC ## TextRank for Custom Summarization Use the TextRank classes directly for fine-grained control over keyword and summary extraction. ### Method N/A (Class instantiation and method calls) ### Endpoint N/A ### Parameters N/A ### Request Example (Sentence Summarization) ```python from snownlp import normal, seg from snownlp.summary.textrank import TextRank, KeywordTextRank text = ''' 机器学习是人工智能的一个分支。 它使用算法来分析数据并学习模式。 深度学习是机器学习的一个子领域。 神经网络是深度学习的核心技术。 ''' # Prepare document: segment text and filter stopwords sentences = normal.get_sentences(text) doc = [] for sent in sentences: words = seg.seg(sent) words = normal.filter_stop(words) doc.append(words) # TextRank for sentence summarization rank = TextRank(doc) rank.solve() top_sentence_indices = rank.top_index(2) print("Top sentences:") for idx in top_sentence_indices: print(f" {sentences[idx]}") ``` ### Response (Sentence Summarization) #### Success Response (200) - **Top sentences** (list of strings) - The most relevant sentences from the text. #### Response Example ``` Top sentences: 机器学习是人工智能的一个分支。 深度学习是机器学习的一个子领域。 ``` ### Request Example (Keyword Extraction - Conceptual) *Note: The provided code focuses on sentence summarization. Keyword extraction would typically involve using `KeywordTextRank` similarly.* ```python # Conceptual example for keyword extraction # keyword_rank = KeywordTextRank(doc) # keywords = keyword_rank.solve(2) # print(keywords) ``` ``` -------------------------------- ### Keyword Extraction and Summarization Source: https://context7.com/isnowfy/snownlp/llms.txt Extracts keywords and summarizes text using the TextRank algorithm. ```APIDOC ## POST /extract ### Description Extracts key information from the initialized text. ### Parameters #### Query Parameters - **limit** (int) - Optional - Number of items to return. - **merge** (bool) - Optional - Whether to merge adjacent keywords. ### Response #### Success Response (200) - **result** (list) - List of strings representing keywords or summary sentences. ### Response Example ['自然语言', '计算机科学', '领域'] ``` -------------------------------- ### Traditional to Simplified Chinese Conversion using SnowNLP Source: https://context7.com/isnowfy/snownlp/llms.txt Converts Traditional Chinese characters to Simplified Chinese using the SnowNLP library. It utilizes a Trie-based maximum matching algorithm. The `han` property provides a direct conversion, and the `normal.zh2hans` function offers an alternative method. ```python from snownlp import SnowNLP # Traditional Chinese text s = SnowNLP('「繁體字」「繁體中文」的叫法在臺灣亦很常見。') # Convert to Simplified Chinese simplified = s.han print(simplified) # Output: 「繁体字」「繁体中文」的叫法在台湾亦很常见。 # Using normal module directly from snownlp import normal text = '計算機科學與人工智能' print(normal.zh2hans(text)) # Output: 计算机科学与人工智能 ``` -------------------------------- ### Extract Keywords using KeywordTextRank Source: https://context7.com/isnowfy/snownlp/llms.txt This snippet demonstrates how to use the KeywordTextRank class to extract the top N keywords from a document. It requires a pre-processed document and returns the indices of the most relevant terms. ```python from snownlp.summary import KeywordTextRank keyword_rank = KeywordTextRank(doc) keyword_rank.solve() top_keywords = keyword_rank.top_index(5) print(f"Top keywords: {top_keywords}") ``` -------------------------------- ### Sentence Tokenization Source: https://context7.com/isnowfy/snownlp/llms.txt Splits text into individual sentences based on Chinese punctuation marks and line breaks. ```APIDOC ## Sentence Tokenization (sentences property) Splits text into individual sentences based on Chinese punctuation marks (。!?;) and line breaks. ### Method N/A (This is a property access, not an API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from snownlp import SnowNLP text = '''今天天气很好。我们去公园玩吧! 你觉得怎么样?应该会很有趣。''' s = SnowNLP(text) sentences = s.sentences print(sentences) ``` ### Response #### Success Response (200) - **sentences** (list of strings) - A list of strings, where each string is a sentence. #### Response Example ``` ['今天天气很好', '我们去公园玩吧', '你觉得怎么样', '应该会很有趣'] ``` ## Using normal module directly ### Method N/A (This is a function call, not an API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from snownlp import normal sents = normal.get_sentences('这是第一句。这是第二句!这是第三句?') print(sents) ``` ### Response #### Success Response (200) - **sents** (list of strings) - A list of segmented sentences. #### Response Example ``` ['这是第一句', '这是第二句', '这是第三句'] ``` ``` -------------------------------- ### Text Similarity (tf, idf, sim) Source: https://context7.com/isnowfy/snownlp/llms.txt Computes TF-IDF statistics and BM25 similarity scores between documents. ```APIDOC ## Text Similarity (tf, idf, sim) Computes TF-IDF statistics and BM25 similarity scores between documents. Initialize SnowNLP with a list of word lists (documents) to use similarity features. ### Method N/A (These are property accesses and a method call, not API endpoints) ### Endpoint N/A ### Parameters N/A ### Request Example (Initialization and TF/IDF) ```python from snownlp import SnowNLP docs = [ ['这篇', '文章', '讲述', '自然语言处理'], ['那篇', '论文', '研究', '机器学习'], ['这个', '文档', '关于', '深度学习'] ] s = SnowNLP(docs) # Get term frequencies for each document print(s.tf) # Get IDF scores for terms print(s.idf) ``` ### Response (TF/IDF) #### Success Response (200) - **s.tf** (list of dictionaries) - Term frequencies for each document. - **s.idf** (dictionary) - Inverse Document Frequency scores for terms. #### Response Example (TF/IDF) ``` [{'这篇': 1, '文章': 1, ...}, {'那篇': 1, ...}, ...] {'这篇': 0.405, '文章': 0.405, '那篇': 0.405, ...} ``` ### Request Example (Similarity) ```python query = ['文章', '自然语言处理'] similarity_scores = s.sim(query) print(similarity_scores) ``` ### Response (Similarity) #### Success Response (200) - **similarity_scores** (list of floats) - Similarity scores of the query to each document. #### Response Example (Similarity) ``` [1.216, 0, 0] ``` ``` -------------------------------- ### Naive Bayes Text Classification with SnowNLP Source: https://context7.com/isnowfy/snownlp/llms.txt Implements a Naive Bayes classifier for custom text classification tasks beyond sentiment analysis. The `Bayes` class allows training on custom data, classifying new text, and saving/loading the trained model. ```python from snownlp.classification.bayes import Bayes # Create and train a custom classifier classifier = Bayes() # Training data: list of [features, label] pairs training_data = [ [['苹果', '手机', '电子产品'], 'tech'], [['华为', '芯片', '5G'], 'tech'], [['足球', '比赛', '冠军'], 'sports'], [['篮球', '运动员', 'NBA'], 'sports'], [['股票', '投资', '基金'], 'finance'], [['银行', '贷款', '利率'], 'finance'], ] classifier.train(training_data) # Classify new text result, probability = classifier.classify(['手机', '芯片', '技术']) print(f"Category: {result}, Probability: {probability}") # Output: Category: tech, Probability: ~0.8 # Save and load the model classifier.save('my_classifier.marshal') classifier.load('my_classifier.marshal') ``` -------------------------------- ### Sentiment Analysis API Source: https://context7.com/isnowfy/snownlp/llms.txt Provides sentiment classification using a Naive Bayes model trained on product reviews. ```APIDOC ## GET /sentiment ### Description Returns a sentiment score between 0 and 1, where higher values indicate positive sentiment. ### Method Property Access / Method Call ### Response #### Success Response (200) - **score** (float) - Sentiment probability (0.0 to 1.0) ### Response Example 0.98 ``` -------------------------------- ### Chinese Character to Pinyin Conversion using SnowNLP Source: https://context7.com/isnowfy/snownlp/llms.txt Converts Chinese characters to their Pinyin romanization using SnowNLP. The `pinyin` property handles the conversion, supporting both pure Chinese text and mixed text with English characters. It employs a Trie-based maximum matching approach. ```python from snownlp import SnowNLP s = SnowNLP('中华人民共和国') pinyin = s.pinyin print(pinyin) # Output: ['zhong', 'hua', 'ren', 'min', 'gong', 'he', 'guo'] # Mixed text with Chinese and non-Chinese s2 = SnowNLP('Hello世界') print(s2.pinyin) # Output: ['Hello', 'shi', 'jie'] ``` -------------------------------- ### Traditional to Simplified Chinese Conversion Source: https://context7.com/isnowfy/snownlp/llms.txt Converts Traditional Chinese characters to Simplified Chinese using a Trie-based maximum matching algorithm. ```APIDOC ## Traditional to Simplified Chinese Conversion (han property) Converts Traditional Chinese characters to Simplified Chinese using a Trie-based maximum matching algorithm. ### Method N/A (This is a property access, not an API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from snownlp import SnowNLP s = SnowNLP('「繁體字」「繁體中文」的叫法在臺灣亦很常見。') simplified = s.han print(simplified) ``` ### Response #### Success Response (200) - **simplified** (string) - The converted Simplified Chinese text. #### Response Example ``` 「繁体字」「繁体中文」的叫法在台湾亦很常见。 ``` ## Using normal module directly ### Method N/A (This is a function call, not an API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from snownlp import normal text = '計算機科學與人工智能' print(normal.zh2hans(text)) ``` ### Response #### Success Response (200) - **output** (string) - The converted Simplified Chinese text. #### Response Example ``` 计算机科学与人工智能 ``` ``` -------------------------------- ### Pinyin Conversion Source: https://context7.com/isnowfy/snownlp/llms.txt Converts Chinese characters to Pinyin romanization using Trie-based maximum matching. ```APIDOC ## Pinyin Conversion (pinyin property) Converts Chinese characters to Pinyin romanization using Trie-based maximum matching. ### Method N/A (This is a property access, not an API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from snownlp import SnowNLP s = SnowNLP('中华人民共和国') pinyin = s.pinyin print(pinyin) ``` ### Response #### Success Response (200) - **pinyin** (list of strings) - A list of Pinyin strings for the Chinese characters. #### Response Example ``` ['zhong', 'hua', 'ren', 'min', 'gong', 'he', 'guo'] ``` ## Mixed text with Chinese and non-Chinese ### Method N/A (This is a property access, not an API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example ```python s2 = SnowNLP('Hello世界') print(s2.pinyin) ``` ### Response #### Success Response (200) - **pinyin** (list of strings) - A list of Pinyin strings, preserving non-Chinese parts. #### Response Example ``` ['Hello', 'shi', 'jie'] ``` ``` -------------------------------- ### Naive Bayes Text Classification Source: https://context7.com/isnowfy/snownlp/llms.txt Uses the Bayes classifier for custom text classification tasks. ```APIDOC ## Naive Bayes Text Classification The Bayes classifier can be used directly for custom text classification tasks beyond sentiment analysis. ### Method N/A (Class instantiation, method calls for training, classifying, saving, and loading) ### Endpoint N/A ### Parameters N/A ### Request Example (Training) ```python from snownlp.classification.bayes import Bayes classifier = Bayes() training_data = [ [['苹果', '手机', '电子产品'], 'tech'], [['华为', '芯片', '5G'], 'tech'], [['足球', '比赛', '冠军'], 'sports'], [['篮球', '运动员', 'NBA'], 'sports'], [['股票', '投资', '基金'], 'finance'], [['银行', '贷款', '利率'], 'finance'], ] classifier.train(training_data) ``` ### Request Example (Classification) ```python result, probability = classifier.classify(['手机', '芯片', '技术']) print(f"Category: {result}, Probability: {probability}") ``` ### Response (Classification) #### Success Response (200) - **result** (string) - The predicted category. - **probability** (float) - The probability of the predicted category. #### Response Example ``` Category: tech, Probability: ~0.8 ``` ### Request Example (Save and Load) ```python classifier.save('my_classifier.marshal') classifier.load('my_classifier.marshal') ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.