-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.py
48 lines (37 loc) · 1.33 KB
/
result.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
import re
class RegExFormatter():
def __init__(self, text):
self.text = text
def has_numbers(self):
matches = re.search(r'\d+', self.text)
return matches is not None
def count_all_words(self):
found = re.split(r"\s+", self.text)
return len(found)
def count_word(self, word):
"""
Abbiamo usato \W invece di \b perche
vogliamo prendere anche le parole che afianco,
possono avere non soltanto uno spazio ma anche
per esempio: una virgola (')
"""
found = re.findall("\W+{}\W+".format(word), self.text)
return len(found)
def count_word2(self, word):
""" Same as count_word but case insensitive
"""
found = re.findall("\W+{}\W+".format(word), self.text, re.IGNORECASE)
return len(found)
def has_whole_word(self, word):
matches = re.search(r'\b{}\b'.format(word), self.text)
return matches is not None
def search_followed_by_numbers(self):
string_founds = []
for match in re.finditer(r'\b[n,N]el (?=\d+)', self.text):
string_founds.append({
match.start(): match.group(0)
})
return string_founds
def sub_starts_with_ma(self, word):
result = re.subn(r'\bma\S*', word, self.text)
return result