Search code examples
pythonpython-2.7python-3.xarabic

Arabic words representation after using split() function in Python


I have a problem with representing Arabic words after using split() function in Python. I used Arabic text as input file (you can try any Arabic source).

here is my original code I worked on it derived from here

#!/usr/bin/env python
# encoding: utf8


from pprint import pprint
from random import choice
import codecs



# ----------------------build deictionary--------------------------
EOS = ['.', '?', '!']
file=u'C:\python27\نجود.txt'
fname = open(file, 'r')
#file1=codecs.open(file , 'r', 'utf-8')
text = fname.read()
#text=file1.read()
words = text.split()
d = {}
for i, word in enumerate(words):
    try:
        first, second, third = words[i], words[i + 1], words[i + 2]
    except IndexError:
        break
    key = (first, second)
    if key not in d:
        d[key] = []
        #
    d[key].append(third)

pprint (d)
    #for k, v in d.iteritems():
      #  print k, v
# -------------------------generate text---------------------------
li = []
first, second = key
li.append(first)
li.append(second)
while True:
    try:

        third = choice(d[key])  
    except KeyError:
        break
    li.append(third)
    if third[-1] in EOS:  
        break
    # else
    key = (second, third)
    first, second = key

gtext = ' '.join(li)
#decoded = gtext.decode('utf8')
print "النص المنشأ:", gtext
#file1.close()

the output I need that same as if I use English input file it will be like this:

{('He', 'is'): ['happy.',
                'happy.',
                'going.',
                'sleep.',
                'sad.',
                'happy.',
                'happy.',
                'sleep.',
                'angry.',
                'angry.'],
 ('angry.', 'He'): ['is', 'is'],
 ('going.', 'He'): ['is'],
 ('happy.', 'He'): ['is', 'is', 'is', 'is'],
 ('is', 'angry.'): ['He', 'He'],
 ('is', 'going.'): ['He'],
 ('is', 'happy.'): ['He', 'He', 'He', 'He'],
 ('is', 'sad.'): ['He'],
 ('is', 'sleep.'): ['He', 'He'],
 ('sad.', 'He'): ['is'],
 ('sleep.', 'He'): ['is', 'is'],
 ('He', 'is'): ['angry.']}

But the words appears like unreadable text:

    {('\xd8\xa3\xd9\x86\xd8\xa7', '\xd8\xa8\xd8\xb5\xd8\xaf\xd8\xaf'):['\xd8\xaa\xd8\xac\xd8\xb1\xd8\xa8\xd8\xa9'],('\xd8\xa7\xd9\x84\xd8\xa8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xd8\xb9', '\xd9\x88\xd8\xac\xd9\x85\xd9\x8a\xd9\x84,'): ['\xd9\x88\xd9\x84\xd9\x83\xd9\x86'],('\xd8\xb1\xd8\xa7\xd8\xa6\xd8\xb9', '\xd9\x88\xd9\x8a\xd8\xb3\xd8\xaa\xd8\xad\xd9\x82'): ['\xd8\xa7\xd9\x84\xd8\xaa\xd8\xac\xd8\xb1\xd8\xa8\xd8\xa9.'],('\xd8\xb3\xd8\xaa\xd9\x83\xd9\x88\xd9\x86', '\xd9\x85\xd8\xae\xd8\xaa\xd9\x84\xd9\x81\xd8\xa9.'): ['\xd8\xa8\xd8\xa7\xd9\x8a\xd8\xab\xd9\x88\xd9\x86'],('\xd8\xb9\xd9\x84\xd9\x8a\xd9\x83\xd9\x85', '\xd8\xa3\xd9\x86\xd8\xa7'): ['\xd8\xa8\xd8\xb5\xd8\xaf\xd8\xaf'],('\xd9\x81\xd9\x8a', '\xd8\xa8\xd8\xa7\xd9\x8a\xd8\xab\xd9\x88\xd9\x86.'): ['\xd8\xa8\xd8\xa7\xd9\x8a\xd8\xab\xd9\x88\xd9\x86'],('\xd9\x85\xd8\xae\xd8\xaa\xd9\x84\xd9\x81\xd8\xa9.', '\xd8\xa8\xd8\xa7\xd9\x8a\xd8\xab\xd9\x88\xd9\x86'): ['\xd8\xb1\xd8\xa7\xd8\xa6\xd8\xb9'],('\xd9\x85\xd8\xb9', '\xd8\xa7\xd9\x84\xd8\xb9\xd8\xb1\xd8\xa8\xd9\x8a'): ['\xd8\xb3\xd8\xaa\xd9\x83\xd9\x88\xd9\x86'],('\xd9\x87\xd8\xb0\xd8\xa7', '\xd8\xa7\xd9\x84\xd8\xa8\xd8\xb1\xd9\x86\xd8\xa7\xd9\x85\xd8\xac'): ['\xd9\x81\xd9\x8a'],('\xd9\x88\xd8\xac\xd9\x85\xd9\x8a\xd9\x84,', '\xd9\x88\xd9\x84\xd9\x83\xd9\x86'): ['\xd8\xaa\xd8\xac\xd8\xb1\xd8\xa8\xd8\xa9'],('\xd9\x88\xd9\x84\xd9\x83\xd9\x86', '\xd8\xaa\xd8\xac\xd8\xb1\xd8\xa8\xd8\xa9'): ['\xd8\xa8\xd8\xa7\xd9\x8a\xd8\xab\xd9\x88\xd9\x86'],('\xef\xbb\xbf\xd8\xa7\xd9\x84\xd8\xb3\xd9\x84\xd8\xa7\xd9\x85', '\xd8\xb9\xd9\x84\xd9\x8a\xd9\x83\xd9\x85'): ['\xd8\xa3\xd9\x86\xd8\xa7']}

Or like this if Python 3:

{('ï»؟ط§ظ„ط³ظ„ط§ظ…', 'ط¹ظ„ظٹظƒظ…'): ['ط£ظ†ط§'],
 ('ط£ظ†ط§', 'ط¨طµط¯ط¯'): ['طھط¬ط±ط¨ط©'],
 ('ط§ظ„ط¨ط±ظ†ط§ظ…ط¬', 'ظپظٹ'): ['ط¨ط§ظٹط«ظˆظ†.'],
 ('ط§ظ„ط¹ط±ط¨ظٹ', 'ط³طھظƒظˆظ†'): ['ظ…ط®طھظ„ظپط©.'],
 ('ط¨ط§ظٹط«ظˆظ†', 'ط±ط§ط¦ط¹'): ['ظˆط¬ظ…ظٹظ„,', 'ظˆظٹط³طھط\xadظ‚'],
 ('ط¨ط§ظٹط«ظˆظ†', 'ظ…ط¹'): ['ط§ظ„ط¹ط±ط¨ظٹ'],
 ('ط¨ط§ظٹط«ظˆظ†.', 'ط¨ط§ظٹط«ظˆظ†'): ['ط±ط§ط¦ط¹'],
 ('ط¨طµط¯ط¯', 'طھط¬ط±ط¨ط©'): ['ظ‡ط°ط§'],
 ('ط±ط§ط¦ط¹', 'ظˆط¬ظ…ظٹظ„,'): ['ظˆظ„ظƒظ†'],
 ('ط±ط§ط¦ط¹', 'ظˆظٹط³طھط\xadظ‚'): ['ط§ظ„طھط¬ط±ط¨ط©.'],
 ('ط³طھظƒظˆظ†', 'ظ…ط®طھظ„ظپط©.'): ['ط¨ط§ظٹط«ظˆظ†'],
 ('ط¹ظ„ظٹظƒظ…', 'ط£ظ†ط§'): ['ط¨طµط¯ط¯'],
 ('طھط¬ط±ط¨ط©', 'ط¨ط§ظٹط«ظˆظ†'): ['ظ…ط¹'],
 ('طھط¬ط±ط¨ط©', 'ظ‡ط°ط§'): ['ط§ظ„ط¨ط±ظ†ط§ظ…ط¬'],
 ('ظˆط¬ظ…ظٹظ„,', 'ظˆظ„ظƒظ†'): ['طھط¬ط±ط¨ط©'],
 ('ظˆظ„ظƒظ†', 'طھط¬ط±ط¨ط©'): ['ط¨ط§ظٹط«ظˆظ†'],
 ('ظپظٹ', 'ط¨ط§ظٹط«ظˆظ†.'): ['ط¨ط§ظٹط«ظˆظ†'],
 ('ظ‡ط°ط§', 'ط§ظ„ط¨ط±ظ†ط§ظ…ط¬'): ['ظپظٹ'],
 ('ظ…ط®طھظ„ظپط©.', 'ط¨ط§ظٹط«ظˆظ†'): ['ط±ط§ط¦ط¹'],
 ('ظ…ط¹', 'ط§ظ„ط¹ط±ط¨ظٹ'): ['ط³طھظƒظˆظ†']}

I've been tried many solutions and still the problem have not yet solved

I tried:

Solution 1:

stack= d.items()
    while stack:
        k, v = stack.pop()
        if isinstance(v, dict):
            stack.extend(v.iteritems())
        else:
            output= "%s: %s" % (k, v)
            print output.decode('utf8')

Solution 2:

 for k, v in d.items():
       print k, v

Solution 3:

for k, v in d.keys():
    print k, v
    for attribute, value in d.items():
        print('{} : {}'.format(attribute, value))

Solution 4:

 words = text.split()
    uniwords= unicode(words)
    print uniwords

but the problem still exist. Also I tried codecs module but still prints like above output. This problem is actually appeared with only if I use file I/O not with a normal text.

So what I need is, if I print split function it should be printed in Arabic words as in English example.


Solution

  • In python3, you need to change fname = open(file, 'r') to fname = open(file, 'r', encoding='utf-8').


    Example: run the following code on your file:

    with open('/tmp/test.txt', 'r', encoding='utf-8') as f:
        l = f.read()
        print(l.split())
    

    You will get a list of words split by " "