Skip to main content

Hazm - Persian NLP Toolkit

Tests PyPI - Downloads PyPI - Python Version GitHub

Evaluation

Module name
DependencyParser 85.6%
POSTagger 98.8%
Chunker 93.4%
Lemmatizer 89.9%

Introduction

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.

Features

  • Normalization: Converts text to a standard form, such as removing diacritics, correcting spacing, 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.
  • Persian corpora reading: Easily read popular Persian corpora with ready-made scripts and minimal code.

Installation

To install the latest version of Hazm, run the following command in your terminal:

pip install hazm

Alternatively, you can install the latest update from GitHub (this version may be unstable and buggy):

pip install git+https://github.com/roshan-research/hazm.git

Pretrained-Models

Finally if you want to use our pretrained models, you can download it from the links below:

Module name Size
Download WordEmbedding ~ 5 GB
Download SentEmbedding ~ 1 GB
Download POSTagger ~ 18 MB
Download DependencyParser ~ 15 MB
Download Chunker ~ 4 MB

Usage

>>> from hazm import *

>>> normalizer = Normalizer()
>>> normalizer.normalize('اصلاح نويسه ها و استفاده از نیم‌فاصله پردازش را آسان مي كند')
'اصلاح نویسه‌ها و استفاده از نیم‌فاصله پردازش را آسان می‌کند'

>>> sent_tokenize('ما هم برای وصل کردن آمدیم! ولی برای پردازش، جدا بهتر نیست؟')
['ما هم برای وصل کردن آمدیم!', 'ولی برای پردازش، جدا بهتر نیست؟']
>>> word_tokenize('ولی برای پردازش، جدا بهتر نیست؟')
['ولی', 'برای', 'پردازش', '،', 'جدا', 'بهتر', 'نیست', '؟']

>>> stemmer = Stemmer()
>>> stemmer.stem('کتاب‌ها')
'کتاب'
>>> lemmatizer = Lemmatizer()
>>> lemmatizer.lemmatize('می‌روم')
'رفت#رو'

>>> tagger = POSTagger(model='pos_tagger.model')
>>> tagger.tag(word_tokenize('ما بسیار کتاب می‌خوانیم'))
[('ما', 'PRO'), ('بسیار', 'ADV'), ('کتاب', 'N'), ('می‌خوانیم', 'V')]

>>> chunker = Chunker(model='chunker.model')
>>> tagged = tagger.tag(word_tokenize('کتاب خواندن را دوست داریم'))
>>> tree2brackets(chunker.parse(tagged))
'[کتاب خواندن NP] [را POSTP] [دوست داریم VP]'

>>> word_embedding = WordEmbedding(model_type = 'fasttext', model_path = 'word2vec.bin')
>>> word_embedding.doesnt_match(['سلام' ,'درود' ,'خداحافظ' ,'پنجره'])
'پنجره'
>>> word_embedding.doesnt_match(['ساعت' ,'پلنگ' ,'شیر'])
'ساعت'

>>> parser = DependencyParser(tagger=tagger, lemmatizer=lemmatizer)
>>> parser.parse(word_tokenize('زنگ‌ها برای که به صدا درمی‌آید؟'))
<DependencyGraph with 8 nodes>

Documentation

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

Hazm in other languages

Disclaimer: These ports are not developed or maintained by Roshan. They may not have the same functionality or quality as the original Hazm..

  • JHazm: A Java port of Hazm
  • NHazm: A C# port of Hazm

Contribution

We welcome and appreciate any contributions to this repo, such as bug reports, feature requests, code improvements, documentation updates, etc. Please follow the Contribution guideline when contributing. You can open an issue, fork the repo, write your code, create a pull request and wait for a review and feedback. Thank you for your interest and support in this repo!

Thanks

Code contributores

Alt

Others

  • Thanks to Virastyar project for providing the persian word list.

Star History Chart

Metadata

Release files for hazm 0.9.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hazm 0.9.4
File Size Uploaded
hazm-0.9.4.tar.gz 354.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hazm 0.9.4
File Interpreter ABI Platform
hazm-0.9.4-py3-none-any.whl Python 3 none any Details

Total release size: 726.6 kB

Release files / hazm-0.9.4.tar.gz

Download URL hazm-0.9.4.tar.gz
Size 354.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6f8dcd0b6010df7cb372f7445d3fc998b4e4efc83b493de31f3599811490073d
BLAKE2b-256 checksum
How to use checksums
99556e9c8ff2a1e49d5bd9d6b56f0059329c8450c17cf69c5a3c547baf886e75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-1012-azure

Release files / hazm-0.9.4-py3-none-any.whl

Download URL hazm-0.9.4-py3-none-any.whl
Size 371.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a396027b6e07bfd7e96334e949720ad1dea454bea9bf824094b79ee6ae66429e
BLAKE2b-256 checksum
How to use checksums
6a06302d69a49f0ea8d5700341fcd5f0630361fe7d8cf17254ff45702ecf6058
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-1012-azure

Release history Release notifications | RSS feed

0.11.0

2 release files

0.10.0

2 release files

This release

0.9.4 This release

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.7.0

3 release files

0.5.2

2 release files

0.5.1

2 release files

0.5

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page