hazm

roshan-researchMIT更新放缓

面向波斯语(Farsi)的 Python NLP 工具包,提供文本归一化、分词、词性标注、依存句法分析、词形还原和词向量等完整处理流程。

Persian NLP Toolkit

v0.12.1发布于 2026-04-01 18:46 UTC1,435star207fork同步于 2026-11-07 07:31 UTC

怎么上手

由 README 提炼,执行前请确认
pip install hazm
pip install huggingface-hub
硬件要求按 README 说明

README 未说明

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

面向波斯语文本的 Python NLP 工具库,覆盖从规范化到依存分析的完整流程

面向波斯语(Farsi)的 Python NLP 工具包,提供文本归一化、分词、词性标注、依存句法分析、词形还原和词向量等完整处理流程。

核心能力

  • 规范化文本:去除变音符号并修正 ZWNJ
  • 按句子和词两级切分波斯语文本
  • 提供词形还原与词性标注
  • 支持依存句法分析与组块
  • 从 Hugging Face 自动下载并缓存预训练模型

容易踩的坑

需要 Python 3.12 及以上版本;使用预训练模型需额外安装 huggingface-hub 并联网下载;调用 POSTagger 等模型时须自行传入 repo_id 和 model_filename

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v0.12.1
  1. v0.12.1Hazm 0.12.1最新正式版2026-04-01 · 2026-04-01 18:46 UTC

    Fixed

    • Normalizer: Resolved a TypeError: argument of type 'NoneType' is not iterable in the token_spacing method. This occurred when correct_spacing was enabled but seperate_mi was disabled, causing the internal verb list to remain uninitialized as None.
    • WordTokenizer: Resolved an AttributeError where internal markers like before_verbs and after_verbs were missing if the tokenizer was initialized with join_verb_parts=False. These attributes are now safely initialized as empty sets by default.
  2. v0.12.0Hazm 0.12.02026-04-01 · 2026-04-01 14:50 UTC
    查看更新说明

    ⚠️ Notice: A critical bug present in v0.11.0 was inadvertently included in this release. Please upgrade to v0.12.1 immediately for the fix.

    • Added optional dependencies support: Introduced separate installation modes to reduce package size and installation time.
      • Base Installation: pip install hazm now installs only core dependencies for lightweight usage.
      • Full Installation: pip install hazm[all] installs all optional dependencies for complete functionality.
  3. v0.11.0Hazm 0.11.02025-12-20 · 2025-12-20 10:54 UTC
    查看更新说明
    • Fixed compatibility issues with Python 3.12; the library is now compatible with Python 3.12 and 3.13.
    • Resolved installation issues in Google Colab and other environments.
    • Added support for downloading models directly from Hugging Face.
    • Rewrite all documentation in English.
    • Various improvements and bug fixes.

    Thanks to everyone who reported issues and helped make Hazm better!

  4. v0.10.0Hazm 0.10.02024-01-16 · 2024-01-16 16:49 UTC
    查看更新说明
    • Added SpacyPOSTagger class for utilizing the hazm deep learning transformer-based model in POS tagging. @MortezaMahdaviMortazavi
    • Added SpacyChunker class for leveraging the hazm deep learning transformer-based model in chunking. @MortezaMahdaviMortazavi
    • Added SpacyDependencyParser class for employing the hazm deep learning transformer-based model in dependency parsing. @MortezaMahdaviMortazavi
    • Added 160,000 new words to improve normalizer and lemmatizer. @sir-kokabi
    • Added FaSpellReader to read FAspell corpus. @sir-kokabi
    • Added ArmanReader to read ArmanPersoNERCorpus. @sir-kokabi
    • Added PnSummaryReader to read pn-summary corpus. @sir-kokabi
    • Removed unnecessary old Stanford dependencies.. @sir-kokabi

    Download pretrained-models

    Full Changelog: https://github.com/roshan-research/hazm/compare/v0.9.4...v0.10.0

  5. v0.9.4Hazm 0.9.42023-10-01 · 2023-10-01 20:12 UTC
    查看更新说明
    • Added join_abbreviations to skip abbrs tokenizing using ParsiNorm's abbreviation lists. #216 @optimopium @sir-kokabi.
    • Added MizanReader to read Mizan corpus. @sir-kokabi.
    • Added NaabReader to read Naab corpus. @sir-kokabi.
    • Added NerReader to read NER corpus. @sir-kokabi.
    • Improved Normalizer by adding support for normalizing words with the suffix 'هایی'. @sir-kokabi.
    • Fixed #298: Incompatibility issues with numpy. @mhdi707 @sir-kokabi

    Download pretrained-models

    Full Changelog: https://github.com/roshan-research/hazm/compare/v0.9.3...v0.9.4

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Hazm - Persian NLP Toolkit

    Tests PyPI - Downloads PyPI - Python Version GitHub

    Hazm is a python library to perform natural language processing tasks on Persian text. It offers various features for analyzing, processing, and understanding Persian text. You can use Hazm to normalize text, tokenize sentences and words, lemmatize words, assign part-of-speech tags, identify dependency relations, create word and sentence embeddings, or read popular Persian corpora.

    sample

    Features

    • Normalization: Converts text to a standard form (diacritics removal, ZWNJ correction, etc).
    • Tokenization: Splits text into sentences and words.
    • Lemmatization: Reduces words to their base forms.
    • POS tagging: Assigns a part of speech to each word.
    • Dependency parsing: Identifies the syntactic relations between words.
    • Embedding: Creates vector representations of words and sentences.
    • Hugging Face Integration: Automatically download and cache pretrained models from the Hub.
    • Persian corpora reading: Easily read popular Persian corpora with ready-made scripts.

    Installation

    To install the latest version of Hazm (requires Python 3.12+), run:

    pip install hazm
    

    To use the pretrained models from Hugging Face, ensure you have the huggingface-hub package:

    pip install huggingface-hub
    

    Pretrained-Models

    Hazm supports automatic downloading of pretrained models. You can find all available models (POS Tagger, Chunker, Embeddings, etc.) on our official Hugging Face page:

    👉 Roshan Research on Hugging Face

    When using Hazm, simply provide the repo_id and model_filename as shown in the examples below, and the library will handle the rest.

    Usage

    from hazm import *
    
    # ===============================
    # Stemming
    # ===============================
    stemmer = Stemmer()
    stem = stemmer.stem('کتاب‌ها')
    print(stem) # کتاب
    
    # ===============================
    # Normalizing
    # ===============================
    normalizer = Normalizer()
    normalized_text = normalizer.normalize('من کتاب های زیــــادی دارم .')
    print(normalized_text) # من کتاب‌های زیادی دارم.
    
    # ===============================
    # Lemmatizing
    # ===============================
    lemmatizer = Lemmatizer()
    lem = lemmatizer.lemmatize('می‌نویسیم')
    print(lem) # نوشت#نویس
    
    # ===============================
    # Sentence tokenizing
    # ===============================
    sentence_tokenizer = SentenceTokenizer()
    sent_tokens = sentence_tokenizer.tokenize('ما کتاب می‌خوانیم. یادگیری خوب است.')
    print(sent_tokens) # ['ما کتاب می\u200cخوانیم.', 'یادگیری خوب است.']
    
    # ===============================
    # Word tokenizing
    # ===============================
    word_tokenizer = WordTokenizer()
    word_tokens = word_tokenizer.tokenize('ما کتاب می‌خوانیم')
    print(word_tokens) # ['ما', 'کتاب', 'می\u200cخوانیم']
    
    # ===============================
    # Part of speech tagging
    # ===============================
    tagger = POSTagger(repo_id="roshan-research/hazm-postagger", model_filename="pos_tagger.model")
    tagged_words = tagger.tag(word_tokens)
    print(tagged_words) # [('ما', 'PRON'), ('کتاب', 'NOUN'), ('می\u200cخوانیم', 'VERB')]
    
    # ===============================
    # Chunking
    # ===============================
    chunker = Chunker(repo_id="roshan-research/hazm-chunker", model_filename="chunker.model")
    chunked_tree = tree2brackets(chunker.parse(tagged_words))
    print(chunked_tree) # [ما NP] [کتاب NP] [می‌خوانیم VP]
    
    # ===============================
    # Word embedding
    # ===============================
    word_embedding = WordEmbedding.load(repo_id='roshan-research/hazm-word-embedding', model_filename='fasttext_skipgram_300.bin', model_type='fasttext')
    odd_word = word_embedding.doesnt_match(['کتاب', 'دفتر', 'قلم', 'پنجره'])
    print(odd_word) # پنجره
    
    # ===============================
    # Sentence embedding
    # ===============================
    sent_embedding = SentEmbedding.load(repo_id='roshan-research/hazm-sent-embedding', model_filename='sent2vec-naab.model')
    sentence_similarity = sent_embedding.similarity('او شیر میخورد','شیر غذا می‌خورد')
    print(sentence_similarity) # 0.4643607437610626
    
    # ===============================
    # Dependency parsing
    # ===============================
    parser = DependencyParser(tagger=tagger, lemmatizer=lemmatizer, repo_id="roshan-research/hazm-dependency-parser", model_filename="langModel.mco")
    dependency_graph = parser.parse(word_tokens)
    print(dependency_graph)
    """
    {0:  {'address': 0,
          'ctag': 'TOP',
          'deps': defaultdict(<class 'list'>, {'root': [3]}),
          'feats': None,
          'head': None,
          'lemma': None,
          'rel': None,
          'tag': 'TOP',
          'word': None},
      1: {'address': 1,
          'ctag': 'PRON',
          'deps': defaultdict(<class 'list'>, {}),
          'feats': '_',
          'head': 3,
          'lemma': 'ما',
          'rel': 'SBJ',
          'tag': 'PRON',
          'word': 'ما'},
      2: {'address': 2,
          'ctag': 'NOUN',
          'deps': defaultdict(<class 'list'>, {}),
          'feats': '_',
          'head': 3,
          'lemma': 'کتاب',
          'rel': 'OBJ',
          'tag': 'NOUN',
          'word': 'کتاب'},
      3: {'address': 3,
          'ctag': 'VERB',
          'deps': defaultdict(<class 'list'>, {'SBJ': [1], 'OBJ': [2]}),
          'feats': '_',
          'head': 0,
          'lemma': 'خواند#خوان',
          'rel': 'root',
          'tag': 'VERB',
          'word': 'می\u200cخوانیم'}})
    
    """
    

    Documentation

    Visit https://roshan-ai.ir/hazm to view the full documentation.

    Evaluation

    Module name
    DependencyParser85.6%
    POSTagger98.8%
    Chunker93.4%
    Lemmatizer89.9%
    MetricValue
    SpacyPOSTaggerPrecision0.99250
    Recall0.99249
    F1-Score0.99249
    EZ Detection in SpacyPOSTaggerPrecision0.99301
    Recall0.99297
    F1-Score0.99298
    SpacyChunkerAccuracy96.53%
    F-Measure95.00%
    Recall95.17%
    Precision94.83%
    SpacyDependencyParserTOK Accuracy99.06
    UAS92.30
    LAS89.15
    SENT Precision98.84
    SENT Recall99.38
    SENT F-Measure99.11

    Star History Chart