-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_text.py
More file actions
102 lines (73 loc) · 3.13 KB
/
Copy pathprocess_text.py
File metadata and controls
102 lines (73 loc) · 3.13 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
# encoding: utf-8
"""Module that will hold the code for processing text."""
from knuth_moriss_pratt import knp_iterator
PATTERN_LIST = [u'.', u'?', u'!', u',', u';', u':', u'...', u'(', u')', u' − ', u' - ', u'„', u'“', u'’',
u'‘', u'/', u'*', u' ', u'−', u'–', u'¹', u'²', u'³', u'⁴', u'⁵', u'⁶', u'⁷',
u'⁸', u'\u2079', u'⁰', u'=', u'+', u'-', u'\u00D7', u'>', u'<', u'\u2264',
u'\u2265', u'%', u'\u00B0', u'~', u'§', u'@']
WORD_TOKEN = u'_'
class ProcessText(object):
"""Process text by removing characters."""
def __init__(self, text, pattern_list, word_token):
"""
Initialize ProcessText.
:param text: The text to be processed.
:type text: unicode
"""
super(ProcessText, self).__init__()
self.text = text
self.pattern_list = pattern_list
self.word_token = word_token
self.matches = []
def process(self):
"""Process the text."""
text_lines = self.text.split(u'\n')
for line_index in range(len(text_lines)):
matches = MatchWarehouse()
for pattern in self.pattern_list:
for start_index in knp_iterator(text_lines[line_index], pattern):
matches.add_match(Match(start_index, start_index + len(pattern), pattern))
new_list_text = []
chars_to_delete = 0
list_text = list(text_lines[line_index])
for index in range(len(list_text)):
if not matches.in_range(index):
chars_to_delete += 1
else:
if chars_to_delete > 0:
chars_to_delete = 0
new_list_text.append(self.word_token)
new_list_text.append(list_text[index])
if chars_to_delete > 0:
new_list_text.append(self.word_token)
text_lines[line_index] = ''.join(new_list_text)
return '\n'.join(text_lines)
class MatchWarehouse(object):
"""A warehouse of match objects."""
def __init__(self, matches=None):
"""Initialize a warehouse of match objects."""
super(MatchWarehouse, self).__init__()
if matches is None:
matches = []
self.matches = matches
def add_match(self, match):
"""Add match to the warehouse."""
self.matches.append(match)
def in_range(self, index):
"""Check if index is in range."""
for match in self.matches:
if match.start_pos <= index < match.end_pos:
return True
return False
class Match(object):
"""A match class."""
def __init__(self, start_pos, end_pos, pattern):
"""Initialize Match."""
super(Match, self).__init__()
self.start_pos = start_pos
self.end_pos = end_pos
self.pattern = pattern
def __repr__(self):
"""Override __repr__ method."""
return '<Match (start_pos={0}, end_pos={1}, pattern="{2}")>'.format(self.start_pos, self.end_pos,
self.pattern)