forked from linuxscout/mishkal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thalab-console.py
executable file
·188 lines (180 loc) · 6.08 KB
/
thalab-console.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/python
#-*- coding: UTF-8 -*-
import sys
sys.path.append('/opt/mishkal/lib');
sys.path.append('mishkal/');
sys.path.append('mishkal/lib/');
from itertools import tee, islice, chain, izip
def previous_and_next(some_iterable):
prevs, items, nexts = tee(some_iterable, 3)
prevs = chain([None], prevs)
nexts = chain(islice(nexts, 1, None), [None])
return izip(prevs, items, nexts)
import re
import string
import datetime
import getopt
import os
# join the actual dirctory to lib path
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), 'lib'));
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), 'mishkal/lib/'));
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), 'mishkal/'));
import pyarabic.araby as araby
import qalsadi.analex
import aranasyn.anasyn
import aranasyn.synnode
scriptname = os.path.splitext(os.path.basename(sys.argv[0]))[0]
scriptversion = '0.1'
AuthorName="Taha Zerrouki"
def usage():
# "Display usage options"
print "(C) CopyLeft 2012, %s"%AuthorName
print "Usage: %s -f filename [OPTIONS]" % scriptname
print (u" %s 'السلام عليكم' [OPTIONS]\n" % scriptname).encode('utf8');
#"Display usage options"
print "\t[-f | --file= filename]input file to %s"%scriptname
print "\t[-h | --help] outputs this usage message"
print "\t[-v | --version] program version"
print "\n\t* Tashkeel Actions\n\t-------------------"
print "\t[-r | --reduced] Reduced Tashkeel."
print "\t[-s | --strip] Strip tashkeel (remove harakat)."
print "\t[-c | --compare] compare the vocalized text with the program output"
print "\n\t* Tashkeel Options\n\t------------------"
print "\t[-l | --limit] vocalize only a limited number of line"
print "\t[-x | --syntax] disable syntaxic analysis"
print "\t[-m | --semantic] disable semantic analysis"
print "\t[-i | --ignore] ignore the last Mark on output words."
print "\t[-t | --stat] disable statistic tashkeel"
print "\r\nThis program is licensed under the GPL License\n"
def grabargs():
# "Grab command-line arguments"
fname = ''
suggestion=False;
ignore= False;
limit=False;
compare = False;
disableSyntax = False;
disableSemantic = False;
disableStatistic= False;
stripTashkeel = False
reducedTashkeel = False
if not sys.argv[1:]:
usage()
sys.exit(0)
try:
opts, args = getopt.getopt(sys.argv[1:], "hVtcixsmrv:f:l:",
["help", "version","stat","compare","reduced","strip", "syntax","semantic", "ignore","limit=", "file="],)
except getopt.GetoptError:
usage()
sys.exit(0)
for o, val in opts:
if o in ("-h", "--help"):
usage()
sys.exit(0)
if o in ("-V", "--version"):
print scriptversion
sys.exit(0)
if o in ("-x", "--syntax"):
disableSyntax = True
if o in ("-s", "--strip"):
stripTashkeel = True
if o in ("-r", "--reduced"):
reducedTashkeel = True
if o in ("-m", "--semantic"):
disableSemantic = True
if o in ("-i", "--ignore"):
ignore = True;
if o in ("-c", "--compare"):
compare = True;
if o in ("-t", "--stat"):
disableStatistic = True;
if o in ("-l", "--limit"):
try: limit = int(val);
except: limit=0;
if o in ("-f", "--file"):
fname = val
utfargs=[]
for a in args:
utfargs.append( a.decode('utf8'));
text= u' '.join(utfargs);
#if text: print text.encode('utf8');
return (fname, text, stripTashkeel, reducedTashkeel,disableSyntax, disableSemantic, disableStatistic, ignore, limit ,compare)
import tashkeel
if __name__ == '__main__':
filename, text, stripTashkeel, reducedTashkeel, disableSyntax, disableSemantic, disableStat, ignore, limit, compare =grabargs()
#filename="samples/randomtext.txt"
if not text and not filename:
usage()
sys.exit(0)
if not text:
try:
myfile=open(filename)
except:
print " Can't Open the given File ", filename;
sys.exit();
else:
lines = text.split('\n');
counter=1;
if not limit :
limit= 100000000
nolimit = False;
if not text:
line=(myfile.readline()).decode('utf8');
else:
if lines:
line= lines[0];
# lexical analyzer
morphanalyzer=qalsadi.analex.analex()
# syntaxic analyzer
anasynt=aranasyn.anasyn.SyntaxAnalyzer();
while line and (nolimit or counter<=limit):
# lineIncorrect = 0;
#strip harakat from line
line= araby.stripTashkeel(line);
#split texts into phrases to treat one phrase in time
texts= morphanalyzer.splitIntoPhrases(line);
# texts=[inputtext,]
for texty in texts:
#morpholigical analysis of text
detailled_stem = morphanalyzer.check_text(texty);
#syntaxical analysis of text
detailled_syntax, synnodeList= anasynt.analyze(detailled_stem);
# print detailled_syntax;
for synnode in synnodeList:
if synnode.getBreakType() in ("break", "mostBreak"):
print;
print synnode.getWord().encode('utf8'),
for previous, currents, nxt in previous_and_next(detailled_syntax):
for wordcase in currents:
print wordcase.getWord().encode('utf8'), wordcase.getNext();
nextWordCasePositions = wordcase.getNext();
previousWordCasePositions = wordcase.getPrevious();
if previous and previousWordCasePositions:
for p in previousWordCasePositions:
if p <len(previous):
if nxt and nextWordCasePositions:
for n in nextWordCasePositions:
if n<len(nxt):
print u' '.join([ previous[p].getVocalized(), wordcase.getVocalized(), nxt[n].getVocalized(),]).encode('utf8')
else:
print u' '.join([ previous[p].getVocalized(), wordcase.getVocalized()]).encode('utf8')
else:
print u' '.join([ previous[p].getVocalized(), wordcase.getVocalized()]).encode('utf8')
else:
if nxt and nextWordCasePositions:
for n in nextWordCasePositions:
if n<len(nxt):
print u' '.join([ wordcase.getVocalized(), nxt[n].getVocalized(),]).encode('utf8')
else:
print u' '.join([ wordcase.getVocalized(),]).encode('utf8')
else:
print u' '.join([ wordcase.getVocalized(),]).encode('utf8')
counter+=1;
#get the next line
if not text:
line=(myfile.readline()).decode('utf8');
else:
if counter<len(lines):
line= lines[counter];
else:
line =None;