ikj
ikj

Reputation: 271

NLTK for Persian

How to use functions of NLTK for Persian?

For example: 'concordance'. When I use 'concordance', the answer is 'not match', however there is the parameter of concordance in my text.

the input is very simple .it contains of "hello سلام".when parameter of 'concordance' is 'hello' the answer is correct ,but , if it's 'سلام' the answer is 'not matches'.the expected output for me is 'Displaying 1 of 1 matches'.

    import nltk
    from urllib import urlopen
    url = "file:///home/.../1.html"
    raw = urlopen(url).read()
    raw = nltk.clean_html(raw)
    tokens = nltk.word_tokenize(raw)
    tokens = tokens[:12]
    text = nltk.Text(tokens)
    print text.concordance('سلام')

Upvotes: 27

Views: 10623

Answers (1)

alvas
alvas

Reputation: 121992

Strongly recommended python Persian library for NLP: https://github.com/sobhe/hazm

Usage:

>>> from __future__ import unicode_literals

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

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

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

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

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

Upvotes: 44

Related Questions