forked from Deffro/text-preprocessing-techniques
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtechniques.py
212 lines (174 loc) · 7.82 KB
/
techniques.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
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
""" Copyright 2017, Dimitrios Effrosynidis, All rights reserved. """
import re
from functools import partial
from collections import Counter
import nltk
from nltk.corpus import wordnet
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from nltk.stem.porter import PorterStemmer
def removeUnicode(text):
""" Removes unicode strings like "\u002c" and "x96" """
text = re.sub(r'(\\u[0-9A-Fa-f]+)',r'', text)
text = re.sub(r'[^\x00-\x7f]',r'',text)
return text
def replaceURL(text):
""" Replaces url address with "url" """
text = re.sub('((www\.[^\s]+)|(https?://[^\s]+))','url',text)
text = re.sub(r'#([^\s]+)', r'\1', text)
return text
def replaceAtUser(text):
""" Replaces "@user" with "atUser" """
text = re.sub('@[^\s]+','atUser',text)
return text
def removeHashtagInFrontOfWord(text):
""" Removes hastag in front of a word """
text = re.sub(r'#([^\s]+)', r'\1', text)
return text
def removeNumbers(text):
""" Removes integers """
text = ''.join([i for i in text if not i.isdigit()])
return text
def replaceMultiExclamationMark(text):
""" Replaces repetitions of exlamation marks """
text = re.sub(r"(\!)\1+", ' multiExclamation ', text)
return text
def replaceMultiQuestionMark(text):
""" Replaces repetitions of question marks """
text = re.sub(r"(\?)\1+", ' multiQuestion ', text)
return text
def replaceMultiStopMark(text):
""" Replaces repetitions of stop marks """
text = re.sub(r"(\.)\1+", ' multiStop ', text)
return text
def countMultiExclamationMarks(text):
""" Replaces repetitions of exlamation marks """
return len(re.findall(r"(\!)\1+", text))
def countMultiQuestionMarks(text):
""" Count repetitions of question marks """
return len(re.findall(r"(\?)\1+", text))
def countMultiStopMarks(text):
""" Count repetitions of stop marks """
return len(re.findall(r"(\.)\1+", text))
def countElongated(text):
""" Input: a text, Output: how many words are elongated """
regex = re.compile(r"(.)\1{2}")
return len([word for word in text.split() if regex.search(word)])
def countAllCaps(text):
""" Input: a text, Output: how many words are all caps """
return len(re.findall("[A-Z0-9]{3,}", text))
""" Creates a dictionary with slangs and their equivalents and replaces them """
with open('slang.txt') as file:
slang_map = dict(map(str.strip, line.partition('\t')[::2])
for line in file if line.strip())
slang_words = sorted(slang_map, key=len, reverse=True) # longest first for regex
regex = re.compile(r"\b({})\b".format("|".join(map(re.escape, slang_words))))
replaceSlang = partial(regex.sub, lambda m: slang_map[m.group(1)])
def countSlang(text):
""" Input: a text, Output: how many slang words and a list of found slangs """
slangCounter = 0
slangsFound = []
tokens = nltk.word_tokenize(text)
for word in tokens:
if word in slang_words:
slangsFound.append(word)
slangCounter += 1
return slangCounter, slangsFound
""" Replaces contractions from a string to their equivalents """
contraction_patterns = [ (r'won\'t', 'will not'), (r'can\'t', 'cannot'), (r'i\'m', 'i am'), (r'ain\'t', 'is not'), (r'(\w+)\'ll', '\g<1> will'), (r'(\w+)n\'t', '\g<1> not'),
(r'(\w+)\'ve', '\g<1> have'), (r'(\w+)\'s', '\g<1> is'), (r'(\w+)\'re', '\g<1> are'), (r'(\w+)\'d', '\g<1> would'), (r'&', 'and'), (r'dammit', 'damn it'), (r'dont', 'do not'), (r'wont', 'will not') ]
def replaceContraction(text):
patterns = [(re.compile(regex), repl) for (regex, repl) in contraction_patterns]
for (pattern, repl) in patterns:
(text, count) = re.subn(pattern, repl, text)
return text
def replaceElongated(word):
""" Replaces an elongated word with its basic form, unless the word exists in the lexicon """
repeat_regexp = re.compile(r'(\w*)(\w)\2(\w*)')
repl = r'\1\2\3'
if wordnet.synsets(word):
return word
repl_word = repeat_regexp.sub(repl, word)
if repl_word != word:
return replaceElongated(repl_word)
else:
return repl_word
def removeEmoticons(text):
""" Removes emoticons from text """
text = re.sub(':\)|;\)|:-\)|\(-:|:-D|=D|:P|xD|X-p|\^\^|:-*|\^\.\^|\^\-\^|\^\_\^|\,-\)|\)-:|:\'\(|:\(|:-\(|:\S|T\.T|\.\_\.|:<|:-\S|:-<|\*\-\*|:O|=O|=\-O|O\.o|XO|O\_O|:-\@|=/|:/|X\-\(|>\.<|>=\(|D:', '', text)
return text
def countEmoticons(text):
""" Input: a text, Output: how many emoticons """
return len(re.findall(':\)|;\)|:-\)|\(-:|:-D|=D|:P|xD|X-p|\^\^|:-*|\^\.\^|\^\-\^|\^\_\^|\,-\)|\)-:|:\'\(|:\(|:-\(|:\S|T\.T|\.\_\.|:<|:-\S|:-<|\*\-\*|:O|=O|=\-O|O\.o|XO|O\_O|:-\@|=/|:/|X\-\(|>\.<|>=\(|D:', text))
### Spell Correction begin ###
""" Spell Correction http://norvig.com/spell-correct.html """
def words(text): return re.findall(r'\w+', text.lower())
WORDS = Counter(words(open('corporaForSpellCorrection.txt').read()))
def P(word, N=sum(WORDS.values())):
"""P robability of `word`. """
return WORDS[word] / N
def spellCorrection(word):
""" Most probable spelling correction for word. """
return max(candidates(word), key=P)
def candidates(word):
""" Generate possible spelling corrections for word. """
return (known([word]) or known(edits1(word)) or known(edits2(word)) or [word])
def known(words):
""" The subset of `words` that appear in the dictionary of WORDS. """
return set(w for w in words if w in WORDS)
def edits1(word):
""" All edits that are one edit away from `word`. """
letters = 'abcdefghijklmnopqrstuvwxyz'
splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [L + R[1:] for L, R in splits if R]
transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R)>1]
replaces = [L + c + R[1:] for L, R in splits if R for c in letters]
inserts = [L + c + R for L, R in splits for c in letters]
return set(deletes + transposes + replaces + inserts)
def edits2(word):
""" All edits that are two edits away from `word`. """
return (e2 for e1 in edits1(word) for e2 in edits1(e1))
### Spell Correction End ###
### Replace Negations Begin ###
def replace(word, pos=None):
""" Creates a set of all antonyms for the word and if there is only one antonym, it returns it """
antonyms = set()
for syn in wordnet.synsets(word, pos=pos):
for lemma in syn.lemmas():
for antonym in lemma.antonyms():
antonyms.add(antonym.name())
if len(antonyms) == 1:
return antonyms.pop()
else:
return None
def replaceNegations(text):
""" Finds "not" and antonym for the next word and if found, replaces not and the next word with the antonym """
i, l = 0, len(text)
words = []
while i < l:
word = text[i]
if word == 'not' and i+1 < l:
ant = replace(text[i+1])
if ant:
words.append(ant)
i += 2
continue
words.append(word)
i += 1
return words
### Replace Negations End ###
def addNotTag(text):
""" Finds "not,never,no" and adds the tag NEG_ to all words that follow until the next punctuation """
transformed = re.sub(r'\b(?:not|never|no)\b[\w\s]+[^\w\s]',
lambda match: re.sub(r'(\s+)(\w+)', r'\1NEG_\2', match.group(0)),
text,
flags=re.IGNORECASE)
return transformed
def addCapTag(word):
""" Finds a word with at least 3 characters capitalized and adds the tag ALL_CAPS_ """
if(len(re.findall("[A-Z]{3,}", word))):
word = word.replace('\\', '' )
transformed = re.sub("[A-Z]{3,}", "ALL_CAPS_"+word, word)
return transformed
else:
return word