-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKTP.java
461 lines (381 loc) · 12.6 KB
/
KTP.java
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import java.math.BigInteger;
import java.util.ArrayList;
/*
* Author: Karrar ([email protected])
* Date: 2017-7-3
* Version: 1.0
* Disc: A text processing class
*/
public class KTP {
private static int percent = 0;
private static String[] arlist = {"؟",",","."};
private static String[] enlist = {" a "," is "," an "," and "," the "," of "," in ",".",",","?","!"};
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/**
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
@param the parameters used by this method.
@return 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
*/
public static int Search(String text, String search){
if(text == null || search == null)
return 0;
String temp = "";
String temptext = "";
String temp2 = "";
String tempsearch = "";
boolean added = false;
// 1# check if it is same text
temptext = text.toLowerCase();
tempsearch = search.toLowerCase();
if(temptext.equals(tempsearch)){
return 100;
}
// 2# check if it got the whole part of the search
if(temptext.contains(tempsearch)){
return 90;
}
// 3# Handling the "..." words
while(howManyLetter(search, "\"") >1){
temp = cropFromTo(search, "\"", "\"",false);
if(temp == null){
break;
}
if(!text.contains(temp)){
return 0;
}else{
search = search.replace("\""+temp+"\"", "");
if(!added){
AddToPercent(50);
added = true;
}
}
}
// 4# removing unwanted spaces
text = text + " ";
search = search + " ";
while(text.contains(" ")){
text = text.replace(" ", " ");
}
if(text.indexOf(" ") == 0){
text = text.replaceFirst(" ", "");
}
if(search.indexOf(" ") == 0){
search = search.replaceFirst(" ", "");
}
// 5# Handling the -... words
while(howManyLetter(search, "-") >0){
temp = cropFromTo(search, "-", " ",false);
if(temp == null){
break;
}
if(text.contains(temp)){
return 0;
}
search = search.replace("-"+temp+" ", "");
}
// 6# removing every useless or not needed words
text = text.toLowerCase();
search = search.toLowerCase();
text = replaceAll(text, arlist," ");
text = replaceAll(text, enlist," ");
search = replaceAll(search, arlist," ");
search = replaceAll(search, enlist," ");
// 7# converting both strings to lists
temptext = text;
tempsearch = search;
ArrayList<String> w = new ArrayList();
ArrayList<String> s = new ArrayList();
w = ConverToList(temptext);
s = ConverToList(tempsearch);
// 8# checking for similarities
int num = 0;
for(int x = 0;x<s.size();x++){
for(int i = 0;i<w.size();i++){
num = checkMistake(w.get(i),s.get(x))/s.size();
if(num>0)
AddToPercent(num);
}
}
return percent;
}
private static int checkMistake(String f1,String f2){
int val = 0;
int val2 = 0;
int num = 0;
if(f2.length()<f1.length()){
val2 = f2.length();
}else{
val2 = f1.length();
}
if(val2 == 1){
return 20;
}
num = 100/val2;
for(int i = 0; i<val2;i++){
String s1 = f1.charAt(i)+"";
String s2 = f2.charAt(i)+"";
if(s1.equals(s2)){
val = val+num;
}else{
val = val - num;
}
}
if(val<60){
return 0;
}
return val;
}
private static void AddToPercent(int num){
if(percent != 100){
percent = percent + num;
}
while(percent>100){
percent = 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
@param the parameters used by this method.
@return 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
*/
public static int howManyLetter(String text, String letter){
if(text == null || letter == null)
return 0;
int exist = 0;
String charat = "";
for(int i = 0;i<text.length();i++){
charat = text.charAt(i)+"";
if(charat.equals(letter)){
exist = exist + 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
@param the parameters used by this method.
@return 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
*/
public static int howManyWord(String text, String word){
if(text == null || word == null)
return 0;
int exist = 0;
while(text.contains(word)){
text = text.replaceFirst(word, "");
exist = exist + 1;
}
return exist;
}
/**
The Description of this method is to find out if a text is digit or string
@param the parameters used by this method.
@return the value returned by this method is either true or false, It will also return false if you pass a null value
*/
public static boolean isDigit(String text){
if(text == null)
return false;
try{
int x = Integer.valueOf(text);
return true;
}catch(Exception e){
return false;
}
}
/**
The Description of this method is to replace as much as you want in a string, "text" is the text to
replace in,"word" is to find and replace, "replacement" is what to replace "word" with in "text", "number" is how many existing similarities you want to replace
@param the parameters used by this method.
@return the value returned by this method is the string after making the replace changes, It will also return null if you pass a null value
*/
public static String replaceByNumber(String text,String word, String replacement, int number){
if(text == null || word == null || replacement == null)
return null;
int replaced = 0;
while(replaced != number){
text = text.replaceFirst(word, replacement);
replaced = replaced + 1;
}
return text;
}
/**
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
@param the parameters used by this method.
@return 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
*/
public static String cropFromTo(String text,String from, String to, boolean leavesides){
if(text == null || from == null || to == null)
return null;
String realfrom = from;
String realto = to;
if(from.equals(to)){
text = text.replaceFirst(from, "%%>>%%ww");
text = text.replaceFirst(to, "%%>>%%MM");
from = "%%>>%%ww";
to = "%%>>%%MM";
}
if(text.contains(from) && text.contains(to)){
while(text.indexOf(from)>text.indexOf(to)){
text = text.replaceFirst(to, "");
}
if(text.indexOf(from)<text.indexOf(to)){
if(!leavesides){
return text.substring(text.indexOf(from)+from.length(), text.indexOf(to));
}else{
return text.substring(text.indexOf(from), text.indexOf(to))+to;
}
}
}
return null;
}
/**
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.
@param the parameters used by this method.
@return 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
*/
public static String cropFromTo(String text,int from, String to){
if(text == null || to == null)
return null;
if(text.contains(to)){
if(0!=text.indexOf(to)){
return text.substring(0, text.indexOf(to));
}
}
return null;
}
/**
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.
@param the parameters used by this method.
@return 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
*/
public static String cropFromTo(String text,int from, int to){
if(text == null)
return null;
if(text.length()>2){
if(from<to){
return text.substring(from, to);
}
}
return null;
}
/**
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.
@param the parameters used by this method.
@return 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
*/
public static String cropFromTo(String text,String from, int to){
if(text == null || from == null)
return null;
if(text.contains(from)){
if(to>text.indexOf(from)){
return text.substring(text.indexOf(from), to);
}
}
return null;
}
/**
The Description of this method is to remove any digit from a string, "text" is the text to
remove digits from.
@param the parameters used by this method.
@return 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
*/
public static String getOnlyString(String text){
if(text == null)
return null;
String lasttext = "";
for(int i = 0;i<text.length()-1;i++){
try{
Integer.valueOf(text.charAt(i)+"");
}catch(Exception e){
lasttext = lasttext + text.charAt(i);
}
}
return lasttext;
}
/**
The Description of this method is to remove any digit from a string, "text" is the text to
remove digits from.
@param the parameters used by this method.
@return 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
*/
public static BigInteger getOnlyDigit(String text){
if(text == null)
return null;
String lasttext = "";
for(int i = 0;i<text.length()-1;i++){
try{
Integer.valueOf(text.charAt(i)+"");
lasttext = lasttext + text.charAt(i);
}catch(Exception e){
// Pass
}
}
return new BigInteger(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
@param the parameters used by this method.
@return 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
*/
public static String removeAll(String text,String[] search){
if(text == null || search == null)
return null;
String lasttext = "";
for(int i = 0;i<search.length-1;i++){
text = text.replace(search[i].toString(), "");
}
return text;
}
/**
The Description of this method is to replace every word in string based on a String[] which is "search" with "replacement", "text" is the text to
remove words from, "search" is the String[] containing the words,"replacement" is the word to replace with
@param the parameters used by this method.
@return the value returned by this method is the string after replacing the words from, It will also return null if you pass a null value
*/
public static String replaceAll(String text,String[] search,String replacement){
if(text == null || search == null || replacement == null)
return null;
String lasttext = "";
for(int i = 0;i<search.length-1;i++){
text = text.replace(search[i].toString(), replacement);
}
return text;
}
/**
The Description of this method is to convert every word in string to ArrayList, "text" is the text to
convert words from.
@param the parameters used by this method.
@return the value returned by this method is the list after adding each word to, It will also return null if you pass a null value
*/
public static ArrayList ConverToList(String text){
if(text == null)
return null;
String temptext = "";
text = text+" ";
ArrayList<String> list = new ArrayList();
while(text.contains(" ")){
text = text.replace(" ", " ");
}
if(text.indexOf(" ") == 0){
text = text.replaceFirst(" ", "");
}
while(text.contains(" ")){
temptext = cropFromTo(text, 0, " ");
text = text.replace(temptext+" ", "");
list.add(temptext);
}
return list;
}
}