-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKTP.py
More file actions
363 lines (323 loc) · 11.7 KB
/
KTP.py
File metadata and controls
363 lines (323 loc) · 11.7 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 4 15:14:44 2017
@author: Karrar (karrar.kazuya@gmail.com)
Version: 1.0
Disc: A text processing class
"""
class KTP(object):
percent = 0
arlist = ["؟",",","."]
enlist = [" a ", " is ", " an ", " and ", " the ", " of ", " in ", ".", ",", "?", "!"]
@classmethod
def main(cls, args):
print "KTP!"
#
# The Description of this method is to find out in percent how much what you are looking for exists in a string
# ,"text" is the line of string to search in
# ,"search" is the letter to search for
# the value returned by this method is the number in percent of how much what you are looking for exists in the text, if returns from 0 to 100 depends how the percent, It will also return 0 if you pass a null value
#
@classmethod
def Search(cls, text, search):
if text == None or search == None:
return 0
temp = ""
temptext = ""
temp2 = ""
tempsearch = ""
added = False
# 1# check if it is same text
temptext = text.lower()
tempsearch = search.lower()
if temptext == tempsearch:
return 100
# 2# check if it got the whole part of the search
if tempsearch in temptext:
return 90
# 3# Handling the "..." words
while KTP.howManyLetter(search, "\"") > 0:
temp = KTP.cropFromTo(search, "\"", "\"", False)
if temp == None:
break
if temp not in text:
return 0
else:
search = search.replace("\"" + temp + "\"", "")
if not added:
KTP.AddToPercent(50)
added = True
# 4# removing unwanted spaces
text = text+" "
search = search+" "
while " " in text:
text = text.replace(" ", " ")
try:
k = text.index(" ")
except:
k = -0
if k == 0:
text = text.replace(" ", "",1)
try:
z = search.index(" ")
except:
z = -0
if z == 0:
search = search.replace(" ", "",1)
# 5# Handling the -... words
while KTP.howManyLetter(search, "-") > 0:
temp = KTP.cropFromTo(search, "-", " ", False)
if temp == None:
break
if temp in text:
return 0
search = search.replace("-" + temp + " ", "")
# 6# removing every useless or not needed words
text = text.lower()
search = search.lower()
text = KTP.replaceAll(text, cls.arlist, " ")
text = KTP.replaceAll(text, cls.enlist, " ")
search = KTP.replaceAll(search, cls.arlist, " ")
search = KTP.replaceAll(search, cls.enlist, " ")
# 7# converting both strings to lists
temptext = text
tempsearch = search
w = KTP.ConverToList(temptext)
s = KTP.ConverToList(tempsearch)
# 8# checking for similarities
num = 0
x = 0
i = 0
while x < len(s):
while i < len(w):
num = KTP.checkMistake(w[i], s[x]) / len(s)
if num > 0:
KTP.AddToPercent(num)
i += 1
x += 1
i = 0
return cls.percent
@classmethod
def checkMistake(cls, f1, f2):
val = 0
val2 = 0
num = 0
if len(f2) < len(f1):
val2 = len(f2)
else:
val2 = len(f1)
if val2 == 1:
return 20
num = 100 / val2
i = 0
while i < val2:
if f1[i] == f2[i]:
val = val + num
else:
val = val - num
i += 1
if val < 60:
return 0
return val
@classmethod
def AddToPercent(cls, num):
if cls.percent != 100:
cls.percent = cls.percent + num
while cls.percent > 100:
cls.percent = cls.percent - 1
#
# The Description of this method is to find out how many a letter exists in a string
# ,"text" is the line of string to search in
# ,"letter" is the letter to search for
# the value returned by this method is the number of how many a letter exist in the text, if returns 0 it means there is no such letter in the text, It will also return 0 if you pass a null value
#
@classmethod
def howManyLetter(cls, text, letter):
if text == None or letter == None:
return 0
exist = 0
charat = ""
i = 0
while i < len(text):
charat = text[i]
if charat == letter:
exist = exist + 1
i += 1
return exist
#
# The Description of this method is to find out how many a word exists in a string
# ,"text" is the line of string to search in
# ,"word" is the word to search for
# the value returned by this method is the number of how many a word exist in the text, if returns 0 it means there is no such letter in the text, It will also return 0 if you pass a null value
#
@classmethod
def howManyWord(cls, text, word):
if text == None or word == None:
return 0
exist = 0
while word in text:
text = text.replace(word, "",1)
exist = exist + 1
return exist
#
# The Description of this method is to crop a part of a string from long string, "text" is the text to
# crop from,"from" is the start point, "to" is the end point "leavesides" is a boolean value if it is false then it will crop without sides else it will crop with sides
# the value returned by this method is the cropped part of the string after from the start point till end point, It will also return null if you pass a null value
#
@classmethod
def cropFromTo(cls, text, from_, to, leavesides):
if text == None or from_ == None or to == None:
return None
realfrom = from_
realto = to
if from_ == to:
text = text.replace(from_, "%%>>%%ww",1)
text = text.replace(to, "%%>>%%MM",1)
from_ = "%%>>%%ww"
to = "%%>>%%MM"
f1 = 0
t1 = 0
try:
f1 = text.index(from_)
except:
f1 = 0
try:
t1 = text.index(to)
except:
t1 = 0
if from_ in text and to in text:
while f1 > t1:
text = text.replace(to, "",1)
try:
f1 = text.index(from_)
except:
break
try:
t1 = text.index(to)
except:
break
if f1 < t1:
if not leavesides:
return text[text.index(from_) + len(from_):text.index(to)]
else:
return text[text.index(from_):text.index(to)] + to
return None
#
# The Description of this method is to crop a part of a string from long string, "text" is the text to
# crop from,"from" is the start point, "to" is the end point.
# the value returned by this method is the cropped part of the string after from the start point till end point, It will also return null if you pass a null value
#
@classmethod
def cropFromTo1(cls, text, from_, to):
if text == None or to == None:
return None
if to in text:
if 0 != text.index(to):
return text[from_:text.index(to)]
return None
#
# The Description of this method is to crop a part of a string from long string, "text" is the text to
# crop from,"from" is the start point, "to" is the end point.
# the value returned by this method is the cropped part of the string after from the start point till end point, It will also return null if you pass a null value
#
@classmethod
def cropFromTo3(cls, text, from_, to):
if text == None:
return None
if 2 > len(text):
if from_ < to:
return text[from_:to]
return None
#
# The Description of this method is to crop a part of a string from long string, "text" is the text to
# crop from,"from" is the start point, "to" is the end point.
# the value returned by this method is the cropped part of the string after from the start point till end point, It will also return null if you pass a null value
#
@classmethod
def cropFromTo2(cls, text, from_, to):
if text == None or from_ == None:
return None
if from_ in text:
if to > text.index(from_):
return text[text.index(from_):to]
return None
#
# The Description of this method is to remove any digit from a string, "text" is the text to
# remove digits from.
# the value returned by this method is the string after removing the digits from, It will also return null if you pass a null value
#
@classmethod
def getOnlyString(cls, text):
if text == None:
return None
lasttext = ""
i = 0
while i < len(text):
if not text[i].isdigit():
lasttext = lasttext + text[i]
i += 1
return lasttext
#
# The Description of this method is to remove any digit from a string, "text" is the text to
# remove digits from.
# the value returned by this method is the string after removing the digits from, It will also return null if you pass a null value
#
@classmethod
def getOnlyDigit(cls, text):
if text == None:
return None
lasttext = ""
i = 0
while i < len(text):
if text[i].isdigit():
lasttext = lasttext + text[i]
i += 1
return lasttext
#
# The Description of this method is to remove every word in string based on a String[], "text" is the text to
# remove words from, search is the String[] containing the words
# the value returned by this method is the string after removing the words from, It will also return null if you pass a null value
#
@classmethod
def removeAll(cls, text, search):
if text == None or search == None:
return None
lasttext = ""
i = 0
while i < len(search):
text = text.replace(search[i].__str__(), "")
i += 1
return text
# replace each word in list from string
@classmethod
def replaceAll(cls, text, search, replacement):
if text == None or search == None or replacement == None:
return None
lasttext = ""
i = 0
while i < len(search):
text = text.replace(search[i].__str__(), replacement)
i += 1
return text
# converts each word in string to list
@classmethod
def ConverToList(cls, text):
if text == None:
return None
temptext = ""
text = text + " "
list_ = []
while " " in text:
text = text.replace(" ", " ")
if text.index(" ") == 0:
text = text.replace(" ", "",1)
while " " in text:
temptext = KTP.cropFromTo1(text, 0, " ")
text = text.replace(temptext + " ", "")
list_.append(temptext)
return list_
if __name__ == '__main__':
import sys
#KTP.main(sys.argv)
print KTP.Search("hello everybody it going","haloo")