0
votes

When using nltk package to tokenize Arabic text, the results appear as numbers! There is no problem when tokenizing English text.

UnicodeDecodeError: 'ascii' codec can't decode byte 0xd8 in position 0: ordinal not in range(128)

# -*- coding: utf-8 -*-
import nltk
from nltk.tokenize import word_tokenize
import stop_words
from stop_words import get_stop_words
doc_a = "ذهب محمد الى المدرسه على دراجته. هذا اول يوم له في المدرسة"
sw = get_stop_words('ar')
tokens = nltk.word_tokenize(doc_a)
stopped_tokens = [i for i in tokens if not i in sw]
print(stopped_tokens)

When set tokens = nltk.word_tokenize(unicode(doc_a,"utf-8"))

Result:

[u'\u0630\u0647\u0628', u'\u0645\u062d\u0645\u062f', u'\u0627\u0644\u0645\u062f\u0631\u0633\u0647', u'\u062f\u0631\u0627\u062c\u062a\u0647', u'.', u'\u0627\u0644\u0645\u062f\u0631\u0633\u0629']

2

2 Answers

0
votes

For me the following code was working for me under python 3.X:

import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords

doc_a = "ذهب محمد الى المدرسه على دراجته. هذا اول يوم له في المدرسة"
doc_a = doc_a.decode('utf-8')
sw = stopwords.words('arabic')
tokens = nltk.word_tokenize(doc_a)
stopped_tokens = [i for i in tokens if not i in sw]
for item in stopped_tokens:
    print(item)      

This line gets you the right stopwords: sw = stopwords.words('arabic').

0
votes

Since I don't have stop-words list, I couldn't work on that part, but the tokenization part seems to be working well:

import nltk
from nltk.tokenize import word_tokenize
doc_a = "ذهب محمد الى المدرسه على دراجته. هذا اول يوم له في المدرسة"
tokens = nltk.word_tokenize(doc_a)
print(tokens)

This was the output I received:

['ذهب', 'محمد', 'الى', 'المدرسه', 'على', 'دراجته', '.', 'هذا', 'اول', 'يوم', 'له', 'في', 'المدرسة']