-
Notifications
You must be signed in to change notification settings - Fork 12
/
CedricTokenizer.py
47 lines (40 loc) · 1.58 KB
/
CedricTokenizer.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
import re
emoticons_str = r"""
(?:
[<>]?
[:;=8] # eyes
[\-o\*\'-]? # optional nose
[\)\]\(\[dDpP/\:\>\<\}\{@\|\\] # mouth
|
[\)\]\(\[dDpP/\:\>\<\}\{@\|\\] # mouth
[\-o\*\'-]? # optional nose
[:;=8] # eyes
[<>]?
|
<3 # heart
)"""
regex_str = [
emoticons_str,
r'<[^>]+>', # HTML tags
r'([a-zA-Z0-9_%\.+-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z0-9-.]+)', # <<<<<<< V.2 -process email addresses
# r'(.+@.+)', # <<<<<<< V.1 -process email addresses
r'(?:@[\w_]+)', # @-mentions
r"(?:\#+[\w_]+[\w\'_\-]*[\w_]+)", # hash-tags
r'http[s]?://(?:[a-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-f][0-9a-f]))+', # URLs
r'(?:(?:\d+,?)+(?:\.?\d+)?)', # numbers
r"(?:[a-z][a-z'\-_]+[a-z])", # words with - and '
r'(?:[\w_]+)', # other words
r'(?:\S)' # anything else
]
tokens_re = re.compile(r'('+'|'.join(regex_str)+')', re.VERBOSE | re.IGNORECASE)
emoticon_re = re.compile(r'^'+emoticons_str+'$', re.VERBOSE | re.IGNORECASE)
def tokenize(s):
return tokens_re.findall(s)
def preprocess(s, lowercase=False):
tokens = tokenize(s)
if lowercase:
tokens = [token if emoticon_re.search(token) else token.lower() for token in tokens]
return tokens
tweet = '@Priscilla: The world is flat! #flat-world :-> email me at
priscilla.Presley%[email protected], https://neverland.ut'
print(preprocess(tweet))