pyhanlp Documentation

repository·master·Indexed 25 days ago

https://github.com/hankcs/pyhanlp

Python interfaces for HanLP1.x, a natural language processing library. It supports Chinese word segmentation, dependency parsing, keyword extraction, and automatic summarization. Features include a command-line interface (CLI) for segmentation and parsing, a built-in HTTP server, and the ability to access low-level Java classes via JClass, SafeJClass, and LazyLoadingJClass.

Tokens
1.1K
Snippets
2
Records
10
Agent score
85%

What's inside pyhanlp

  1. Install pyhanlp via conda and pip

    master

    For engineers, the recommended installation method is to use conda to install openjdk and python=3.8, followed by pip to install pyhanlp.

    After installation, verify it by running the hanlp command. If automatic installation fails due to network issues, refer to the manual configuration or Windows guide in the repository wiki.

    conda install -c conda-forge openjdk python=3.8 -y
    pip install pyhanlp
  2. Configure HanLP with custom properties

    master

    You can share models and dictionaries between projects by copying a hanlp.properties configuration file to the pyhanlp installation directory.

    To find your installation directory, run hanlp --version.

    You can also load a specific configuration file temporarily using the --config flag with the CLI.

  3. Run the HanLP HTTP server

    master

    You can start a simple HTTP server to expose HanLP services (like lexical analysis and dependency parsing) via web requests. By default, the server runs on port 8765.

    To run the server, you can call the run function from pyhanlp.server.

  4. Interact with the HanLP server via GET requests

    master

    The server supports GET requests to perform lexical analysis and dependency parsing on a provided sentence.

    Query Parameters

    • sentence: The text you want to analyze. If omitted, the server selects a random sentence from a predefined list.

    Behavior

    1. The server extracts the first complete sentence (up to the first punctuation mark like , !, or ?).
    2. It removes spaces.
    3. It enforces a MAX_LENGTH of 50 characters. If the sentence is longer, it returns an error message.
    4. It performs lexical analysis (using NLPTokenizer.ANALYZER) and dependency parsing (HanLP.parseDependency).

    Example Usage

    Using curl to send a sentence:

    curl "http://localhost:8765/?sentence=你好世界"
  5. Access low-level Java classes via JClass

    master

    To use advanced or lower-level features not exposed in the high-level HanLP class, you can use JClass to instantiate Java classes directly using their full package paths.

    • Use JClass for standard access.
    • Use SafeJClass if you require multi-thread safety.
    • Use LazyLoadingJClass if you require lazy loading.
    from pyhanlp import *
    
    # Example: Using PerceptronLexicalAnalyzer
    PerceptronLexicalAnalyzer = JClass('com.hankcs.hanlp.model.perceptron.PerceptronLexicalAnalyzer')
    analyzer = PerceptronLexicalAnalyzer()
    print(analyzer.analyze("上海华安工业(集团)公司董事长谭旭光和秘书胡花蕊来到美国纽约现代艺术博物馆参观"))
  6. Use the HanLP API for NLP tasks

    master
    The HanLP utility class provides high-level access to common NLP interfaces. You can perform word segmentation, keyword extraction, automatic summarization, and dependency parsing directly in Python.
  7. Use the hanlp CLI for segmentation and parsing

    master

    The hanlp command-line interface provides several tools:

    • Chinese Word Segmentation: Use hanlp segment to enter interactive mode or redirect input using <<<.
    • Dependency Parsing: Use hanlp parse to perform dependency parsing in interactive mode or via redirection.
    • Server: Use hanlp serve to start a built-in HTTP server at http://localhost:8765.
    • Update: Use hanlp update to upgrade HanLP1.x to the latest version.
    • Help: Use hanlp --help to view the manual.
  8. Configure HanLP via hanlp.properties

    master
    You can provide a custom configuration file to the HanLP CLI using the --config flag. This file must point to a valid hanlp.properties file. If a path is provided that does not exist, the CLI will exit with an error.
  9. Use the HanLP CLI for segmentation, parsing, and serving

    master

    The pyhanlp module provides a command-line interface for common NLP tasks. You can pipe text into the CLI via stdin to perform word segmentation or dependency parsing. It also supports starting an HTTP server and updating HanLP components.

    Available Tasks

    • segment: Performs word segmentation. Use --tag or --no-tag to control part-of-speech tagging, and -a or --algorithm to specify the segmentation algorithm (e.g., viterbi, perceptron).
    • parse: Performs dependency parsing.
    • serve: Starts an HTTP server for HanLP (Python 3 only).
    • update: Updates the HanLP JAR and data files.

    Global Options

    • -v, --version: Shows the installed versions of the HanLP JAR, data, and the path to the configuration file.
    • --config <path>: Specifies a custom path to hanlp.properties.