### Install pkuseg via pip Source: https://github.com/lancopku/pkuseg-python/blob/master/README.md Commands to install or update the pkuseg package using pip, including options for using mirror sources. ```bash pip3 install pkuseg pip3 install -U pkuseg pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pkuseg ``` -------------------------------- ### Perform Chinese Word Segmentation Source: https://github.com/lancopku/pkuseg-python/blob/master/README.md Examples of using pkuseg to perform word segmentation with default settings, specific domain models, and part-of-speech tagging. ```python import pkuseg # Default configuration seg = pkuseg.pkuseg() print(seg.cut('我爱北京天安门')) # Specific domain model seg_med = pkuseg.pkuseg(model_name='medicine') print(seg_med.cut('我爱北京天安门')) # With part-of-speech tagging seg_pos = pkuseg.pkuseg(postag=True) print(seg_pos.cut('我爱北京天安门')) ``` -------------------------------- ### Load and Use Custom or Pre-trained Models Source: https://context7.com/lancopku/pkuseg-python/llms.txt Shows how to initialize the pkuseg segmenter with custom models, pre-trained release models, and how to integrate user dictionaries with part-of-speech tagging enabled. ```python import pkuseg # Load local model seg = pkuseg.pkuseg(model_name='./my_trained_model') print(seg.cut('使用自己训练的模型进行分词')) # Load pre-trained model seg_ctb8 = pkuseg.pkuseg(model_name='./ctb8') print(seg_ctb8.cut('我爱北京天安门')) # Use with user dictionary and POS tagging seg = pkuseg.pkuseg(model_name='./domain_model', user_dict='./domain_dict.txt', postag=True) print(seg.cut('领域专业术语测试')) ``` -------------------------------- ### Train and Fine-tune pkuseg Models Source: https://context7.com/lancopku/pkuseg-python/llms.txt Demonstrates how to train a new model from scratch, adjust training iterations, and perform fine-tuning on a pre-trained model using the pkuseg.train method. ```python import pkuseg # Train new model pkuseg.train(trainFile='msr_training.utf8', testFile='msr_test_gold.utf8', savedir='./my_models') # Train with custom iterations pkuseg.train(trainFile='training.utf8', testFile='test.utf8', savedir='./models', train_iter=30) # Fine-tune from pre-trained model pkuseg.train(trainFile='domain_train.txt', testFile='domain_test.txt', savedir='./fine_tuned_model', train_iter=10, init_model='./pretrained') ``` -------------------------------- ### pkuseg.pkuseg Initialization Source: https://github.com/lancopku/pkuseg-python/blob/master/README.md Initializes the word segmentation model with specific configurations such as domain selection and part-of-speech tagging. ```APIDOC ## POST /pkuseg/pkuseg ### Description Initializes the segmentation model. Users can specify the domain model, custom user dictionaries, and whether to enable part-of-speech tagging. ### Parameters #### Request Body - **model_name** (string) - Optional - Path or name of the model (default: "default", options: "news", "web", "medicine", "tourism") - **user_dict** (string) - Optional - Path to custom dictionary or "default" - **postag** (boolean) - Optional - Whether to perform part-of-speech tagging (default: false) ### Request Example { "model_name": "medicine", "postag": true } ### Response #### Success Response (200) - **seg_object** (object) - The initialized segmentation instance. ``` -------------------------------- ### Train and Test pkuseg Model Source: https://github.com/lancopku/pkuseg-python/blob/master/readme/environment.md Demonstrates how to train a pkuseg model using a training file and a gold standard test file, and how to perform inference on raw text. The training function saves the model to a specified directory, while the test function outputs segmentation results to a file. ```python import pkuseg # Train the model pkuseg.train('msr_training.utf8', 'msr_test_gold.utf8', './models') # Test the model pkuseg.test('msr_test.raw', 'output.txt', user_dict=None) ``` -------------------------------- ### Basic Chinese Word Segmentation Source: https://context7.com/lancopku/pkuseg-python/llms.txt Demonstrates how to initialize the default pkuseg segmenter and process text strings or lists of sentences into segmented word lists. ```python import pkuseg # 创建默认分词器(使用混合领域模型) seg = pkuseg.pkuseg() # 对句子进行分词 text = seg.cut('我爱北京天安门') print(text) # 分词多个句子 sentences = ['北京大学是中国著名高等学府', '今天天气真不错'] for sentence in sentences: result = seg.cut(sentence) print(f'{sentence} -> {result}') ``` -------------------------------- ### Perform Part-of-Speech Tagging Source: https://context7.com/lancopku/pkuseg-python/llms.txt Illustrates how to enable part-of-speech tagging in pkuseg and map the resulting tags to human-readable labels using the provided POS_TAGS dictionary. ```python import pkuseg POS_TAGS = {'n': '名词', 't': '时间词', 's': '处所词', 'v': '动词', 'a': '形容词', 'nr': '人名', 'ns': '地名', 'nt': '机构名称'} seg = pkuseg.pkuseg(postag=True) result = seg.cut('2024年北京大学举办了人工智能研讨会') for word, pos in result: pos_name = POS_TAGS.get(pos, pos) print(f'{word}: {pos} ({pos_name})') ``` -------------------------------- ### Implementing Multiprocessing in pkuseg Source: https://github.com/lancopku/pkuseg-python/blob/master/readme/multiprocess.md Demonstrates the correct way to wrap pkuseg test and train functions within a multiprocessing-safe block. This ensures that the script executes correctly when using the nthread parameter. ```python import pkuseg if __name__ == '__main__': pkuseg.test('input.txt', 'output.txt', nthread=20) pkuseg.train('msr_training.utf8', 'msr_test_gold.utf8', './models', nthread=20) ``` -------------------------------- ### Pkuseg: Fine-tuning a Pre-trained Model Source: https://github.com/lancopku/pkuseg-python/blob/master/readme/interface.md Fine-tunes an existing pre-trained model using new training and testing data. This allows for adapting a general model to a specific domain or dataset. It supports specifying the number of training iterations and the directory of the pre-trained model. ```python import pkuseg pkuseg.train('train.txt', 'test.txt', './models', train_iter=10, init_model='./pretrained') ``` -------------------------------- ### Multi-processing Best Practices Source: https://context7.com/lancopku/pkuseg-python/llms.txt Illustrates the required use of if __name__ == '__main__' block when using multi-processing features in Python scripts. ```python import pkuseg if __name__ == '__main__': # 多进程文件分词 pkuseg.test('input.txt', 'output.txt', nthread=20) # 多进程模型训练 pkuseg.train('training.utf8', 'test.utf8', './models', nthread=20) ``` -------------------------------- ### Pkuseg: Word Segmentation with Part-of-Speech Tagging Source: https://github.com/lancopku/pkuseg-python/blob/master/readme/interface.md Performs word segmentation and part-of-speech tagging simultaneously. The POS tags can be detailed by referring to the 'tags.txt' file. It takes a string as input and returns segmented words with their corresponding POS tags. ```python import pkuseg seg = pkuseg.pkuseg(postag=True) text = seg.cut('我爱北京天安门') print(text) ``` -------------------------------- ### Pkuseg: Training a New Model Source: https://github.com/lancopku/pkuseg-python/blob/master/readme/interface.md Trains a new word segmentation model from scratch using specified training and testing files. The trained model is saved to a designated directory. This function supports UTF-8 encoding and requires words to be space-separated. ```python import pkuseg pkuseg.train('msr_training.utf8', 'msr_test_gold.utf8', './models') ``` -------------------------------- ### Pkuseg: Fine-grained Domain Word Segmentation Source: https://github.com/lancopku/pkuseg-python/blob/master/readme/interface.md Performs word segmentation using a specific domain model (e.g., 'medicine'). The library automatically downloads the required model. This is recommended when the domain is known. It takes a string as input and returns a list of segmented words. ```python import pkuseg seg = pkuseg.pkuseg(model_name='medicine') text = seg.cut('我爱北京天安门') print(text) ``` -------------------------------- ### Batch Process Files for Segmentation Source: https://github.com/lancopku/pkuseg-python/blob/master/README.md Use the test method to process an input file and save the segmented output to a file using multiple threads. ```python import pkuseg # Process input.txt and save to output.txt using 20 threads pkuseg.test('input.txt', 'output.txt', nthread=20) ``` -------------------------------- ### Domain-Specific Word Segmentation Source: https://context7.com/lancopku/pkuseg-python/llms.txt Shows how to load specialized pre-trained models (medicine, news, web, tourism) to improve accuracy for specific text domains. ```python import pkuseg # 使用医药领域模型 seg_medicine = pkuseg.pkuseg(model_name='medicine') text = seg_medicine.cut('甲状腺功能减退症简称甲减,是甲状腺制造的甲状腺激素过少而引发的疾病') print(text) # 使用新闻领域模型 seg_news = pkuseg.pkuseg(model_name='news') text = seg_news.cut('乌克兰政府正式通过最新宪法修正案') print(text) ``` -------------------------------- ### Pkuseg: Word Segmentation with Custom Dictionary Source: https://github.com/lancopku/pkuseg-python/blob/master/readme/interface.md Performs word segmentation while incorporating words from a user-defined dictionary file. This allows for custom vocabulary inclusion. It takes a string as input and returns a list of segmented words. ```python import pkuseg seg = pkuseg.pkuseg(user_dict='my_dict.txt') text = seg.cut('我爱北京天安门') print(text) ``` -------------------------------- ### Pkuseg: Word Segmentation with Self-Trained Model Source: https://github.com/lancopku/pkuseg-python/blob/master/readme/interface.md Performs word segmentation using a self-trained model located in a specified directory (e.g., './ctb8'). This requires pre-training a model using the `pkuseg.train` function or obtaining one externally. ```python import pkuseg seg = pkuseg.pkuseg(model_name='./ctb8') text = seg.cut('我爱北京天安门') print(text) ``` -------------------------------- ### Pkuseg: Default Word Segmentation Source: https://github.com/lancopku/pkuseg-python/blob/master/readme/interface.md Performs word segmentation using the default model configuration. This is recommended when the specific domain of the text is unknown. It takes a string as input and returns a list of segmented words. ```python import pkuseg seg = pkuseg.pkuseg() text = seg.cut('我爱北京天安门') print(text) ``` -------------------------------- ### pkuseg.train Model Training Source: https://github.com/lancopku/pkuseg-python/blob/master/README.md Trains a new segmentation model using provided training and testing datasets. ```APIDOC ## POST /pkuseg/train ### Description Trains a custom model based on provided training and testing files. ### Parameters #### Request Body - **trainFile** (string) - Required - Path to training data - **testFile** (string) - Required - Path to testing data - **savedir** (string) - Required - Directory to save the trained model - **train_iter** (integer) - Optional - Number of training iterations (default: 20) ### Request Example { "trainFile": "train.txt", "testFile": "test.txt", "savedir": "./my_model/" } ### Response #### Success Response (200) - **status** (string) - Success message indicating the model has been saved. ``` -------------------------------- ### Cite PKUSEG in BibTeX Source: https://github.com/lancopku/pkuseg-python/blob/master/README.md This BibTeX entry provides the standard citation format for the PKUSEG toolkit. It should be used in academic papers that utilize this library for multi-domain Chinese word segmentation. ```BibTeX @article{pkuseg, author = {Luo, Ruixuan and Xu, Jingjing and Zhang, Yi and Zhang, Zhiyuan and Ren, Xuancheng and Sun, Xu}, journal = {CoRR}, title = {PKUSEG: A Toolkit for Multi-Domain Chinese Word Segmentation.}, url = {https://arxiv.org/abs/1906.11455}, volume = {abs/1906.11455}, year = 2019 } ``` -------------------------------- ### Part-of-Speech Tagging Source: https://context7.com/lancopku/pkuseg-python/llms.txt Enables part-of-speech tagging during segmentation, returning tuples of (word, tag) to provide grammatical context. ```python import pkuseg # 开启词性标注功能 seg = pkuseg.pkuseg(postag=True) # 分词并标注词性 text = seg.cut('我爱北京天安门') print(text) # 输出: [('我', 'r'), ('爱', 'v'), ('北京', 'ns'), ('天安门', 'ns')] ``` -------------------------------- ### Custom User Dictionaries Source: https://context7.com/lancopku/pkuseg-python/llms.txt Configures the segmenter to use custom word lists or files to ensure specific terminology is segmented correctly. ```python import pkuseg # 使用用户自定义词典文件 seg = pkuseg.pkuseg(user_dict='my_dict.txt') # 也可以传入词语列表 user_words = ['北京天安门', '机器学习', '自然语言处理'] seg = pkuseg.pkuseg(user_dict=user_words) text = seg.cut('自然语言处理是机器学习的重要应用领域') print(text) ``` -------------------------------- ### pkuseg.test File Processing Source: https://github.com/lancopku/pkuseg-python/blob/master/README.md Performs word segmentation on an input file and saves the result to an output file using multi-processing. ```APIDOC ## POST /pkuseg/test ### Description Processes a text file for word segmentation and optional part-of-speech tagging, outputting the result to a specified file. ### Parameters #### Request Body - **readFile** (string) - Required - Path to the input text file - **outputFile** (string) - Required - Path to save the output - **nthread** (integer) - Optional - Number of processes to use (default: 10) ### Request Example { "readFile": "input.txt", "outputFile": "output.txt", "nthread": 20 } ### Response #### Success Response (200) - **status** (string) - Success message indicating file processing completion. ``` -------------------------------- ### Batch File Processing Source: https://context7.com/lancopku/pkuseg-python/llms.txt Performs segmentation on large input files using multi-processing for improved performance. ```python import pkuseg # 基本文件分词(使用10个进程) pkuseg.test('input.txt', 'output.txt', nthread=10) # 文件分词同时进行词性标注 pkuseg.test('input.txt', 'output_with_pos.txt', postag=True, nthread=10) ``` -------------------------------- ### Pkuseg: File Word Segmentation Source: https://github.com/lancopku/pkuseg-python/blob/master/readme/interface.md Segments words from an input text file and writes the output to another file. This function supports parallel processing using multiple threads (nthread). ```python import pkuseg pkuseg.test('input.txt', 'output.txt', nthread=20) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.