Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using unicode in python for Farsi

I'm writing a script to read from a corpus file and find suffixes. Since there are Persian words in the corpus it is UTF-8 encoded, however when I use Persian suffixes for searching I get no results, English results on the other hand back fine.

from __future__ import unicode_literals
import nltk
import sys


for line in open("corpus.txt"):
for word in line.split():
     if word.endswith('ب'):
        print (word)
like image 666
Andrew Ravus Avatar asked Sep 01 '25 00:09

Andrew Ravus


1 Answers

In Python 3, you can just pass encoding=utf-8 to open:

with open("corpus.txt", encoding="utf-8") as fp:
    for line in fp:
        for word in line.split():
            process(word)

In Python 2, you'll need to do something like this:

import codecs
with codecs.open("corpus.txt", encoding="utf-8") as fp:
    for line in fp:
        for word in line.split():
            process(word)
like image 156
Benjamin Peterson Avatar answered Sep 03 '25 06:09

Benjamin Peterson