-
Notifications
You must be signed in to change notification settings - Fork 5
/
stringProcessing.py
94 lines (81 loc) · 1.88 KB
/
stringProcessing.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
'''
Copyright (c) 2017 Kartikeya Sharma
Copyright (c) 2017 Radhika Sood
Copyright (c) 2017 Harsh Tiku
'''
import re
def remove_extra_spaces(input_string):
"""input: string as input
removes extra white space like tabs and more than 1 consecutive space.
output: string with no extra white spaces
"""
return ' '.join(input_string.split())
def remove_unwanted_suffix(input_string):
"""input: string
output: string
"""
return ''.join(input_string[:-4])
def remove_non_ascii_letters(input_string):
"""input:string
output:string
"""
return ''.join(i for i in input_string if ord(i)<128)
def get_formated_meaning(input_string):
"""input: string
output: string
"""
r=remove_extra_spaces(input_string)
s=remove_non_ascii_letters(r)
newlinesinserted = re.sub(r"([0-9]+)", r"\n \1", s)
return newlinesinserted
def get_formated_synonyms(input_string):
"""input: string
output: list of synonyms
"""
l=input_string.split('\n')
synonyms=[]
for i in l:
s=remove_unwanted_suffix(i)
if not len(s)<=0 :
synonyms.append(s)
return synonyms
def get_last_word(string):
string=string.rstrip()
try:
x=string.rindex(",")
newstr=string[x+1:]
except:
try:
x=string.rindex(" ")
newstr=string[x+1:]
except: pass
try:
x=string.rindex("(")
newstr=string[x+1:-1]
except: pass
return newstr
def replace_last_word(replace,string):
string=string.rstrip()
try:
x=string.rindex(" ")
except:
x=string.rindex(",")
string=string[:x+1]+replace
return string
'''
try:
x=string.rindex(",")
newstr=string[x+1:]
except:
try:
x=string.rindex(" ")
newstr=string[x+1:]
except:
x=string.rindex("(")
newstr=string[x+1:-1]
return newstr
'''
def str_cmp(a,b):
if(a.tolower()==b.tolower()):
return True
return False