-
Notifications
You must be signed in to change notification settings - Fork 0
/
feat_gen.py
executable file
·141 lines (112 loc) · 5.13 KB
/
feat_gen.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
#!/bin/python
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
import string
def preprocess_corpus(train_sents):
"""Use the sentences to do whatever preprocessing you think is suitable,
such as counts, keeping track of rare features/words to remove, matches to lexicons,
loading files, and so on. Avoid doing any of this in token2features, since
that will be called on every token of every sentence.
Of course, this is an optional function.
Note that you can also call token2features here to aggregate feature counts, etc.
"""
pass
emoticons = [':-)', ':)', ';)', ':o)', ':]', ':3', ':c)', ':>', '=]', '8)', '=)', ':}',
':^)', ':-D', ':D', '8-D', '8D', 'x-D', 'xD', 'X-D', 'XD', '=-D', '=D', ';D', ';P'
'=-3', '=3', ':-))', ":'-)", ":')", ':*', ':^*', '>:P', ':-P', ':P', 'X-P',
'x-p', 'xp', 'XP', ':-p', ':p', '=p', ':-b', ':b', '>:)', '>;)', '>:-)',
'<3', ':L', ':-/', '>:/', ':S', '>:[', ':@', ':-(', ':[', ':-||', '=L', ':<',
':-[', ':-<', '=\\', '=/', '>:(', ':(', '>.<', ":'-(", ":'(", ':\\', ':-c',
':c', ':{', '>:\\', ';(']
stop_words = set(stopwords.words('english'))
abbreviations = {'atm','aka','atb','wth','wtf','af','ik','ly','bf','gf','fu','fo','imho', 'potd', 'l8', 'ffs', 'ianal', 'wcw', 'irl', 'nsfl', 'hmu', 'idk', 'cc', 'dgaf', 'omg', 'w/',
'idgaf', 'cmon','bamf','mtfbwy', 'omw', 'btw', 'eml', 'b4', 'nsfw', 'fath', 'tldr', 'ysk', 'bff', 'futab', 'b/c', 'jk', 'nm',
'bae', 'fomo', 'qotd', 'ppl', 'eli5', 'mt', 'lol', 'fyi', 'tbh', 'lolz', 'gg', 'til', 'ymmv', 'op', 'dftba',
'ftfy', 'orly', 'imo', 'wotd', 'ootd', 'em', 'oh', 'lms', 'wdymbt', 'g2g', 'ttys', 'gtg', 'sfw', 'fbf',
'brb', 'idc', 'tl;dr', 'oan', 'hmb', 'otp', 'lmk', 'bc', 'mcm', 'yt', 'ttyn', 'rofl', 'btaim', 'afaik', 'f2f',
'thx', 'ikr', 'hbd', 'tmi', 'txt', 'ttyl', 'gtr', 'hth', 'ama', 'nvm', 'lmao', 'ily', 'asl', 'ianad', 'wbu', 'fbo',
'tbt', 'mm', 'tgif', 'tx', 'smh', 'icymi', 'gr8', 'yolo', 'dae', 'roflmao'}
slang_words = []
with open("slangtext.txt", "r") as file:
for line in file:
key, value = line.split(",")
slang_words.append(key.strip())
def token2features(sent, i, add_neighs = True):
"""Compute the features of a token.
All the features are boolean, i.e. they appear or they do not. For the token,
you have to return a set of strings that represent the features that *fire*
for the token. See the code below.
The token is at position i, and the rest of the sentence is provided as well.
Try to make this efficient, since it is called on every token.
One thing to note is that it is only called once per token, i.e. we do not call
this function in the inner loops of training. So if your training is slow, it's
not because of how long it's taking to run this code. That said, if your number
of features is quite large, that will cause slowdowns for sure.
add_neighs is a parameter that allows us to use this function itself in order to
recursively add the same features, as computed for the neighbors. Of course, we do
not want to recurse on the neighbors again, and then it is set to False (see code).
"""
ftrs = []
# bias
ftrs.append("BIAS")
# position features
if i == 0:
ftrs.append("SENT_BEGIN")
if i == len(sent)-1:
ftrs.append("SENT_END")
# the word itself
word = unicode(sent[i])
ftrs.append("WORD=" + word)
ftrs.append("LCASE=" + word.lower())
# some features of the word
if word.isalnum():
ftrs.append("IS_ALNUM")
if word.isnumeric():
ftrs.append("IS_NUMERIC")
if word.isdigit():
ftrs.append("IS_DIGIT")
if word.isupper():
ftrs.append("IS_UPPER")
if word.islower():
ftrs.append("IS_LOWER")
if word.startswith("#"):
ftrs.append("IS_HASHTAG")
if word in string.punctuation:
ftrs.append("IS_PUNC")
if word.startswith("@"):
ftrs.append("IS_MENTION")
if word.startswith("http"):
ftrs.append("IS_LINK")
if word[0].isupper():
ftrs.append("IS_PROPER")
if word in emoticons:
ftrs.append("IS_EMOTICON")
#if word in stop_words:
# ftrs.append("IS_STOPWORD")
#if word in abbreviations:
# ftrs.append("IS_ABBR")
if word in slang_words:
ftrs.append("IS_SLANG")
if word.endswith("ing"):
ftrs.append("ENDS_WITH_ING")
if word.endswith("ly"):
ftrs.append("ENDS_WITH_LY")
# previous/next word feats
if add_neighs:
if i > 0:
for pf in token2features(sent, i-1, add_neighs = False):
ftrs.append("PREV_" + pf)
if i < len(sent)-1:
for pf in token2features(sent, i+1, add_neighs = False):
ftrs.append("NEXT_" + pf)
# return it!
return ftrs
if __name__ == "__main__":
sents = [
[ "I", "loving", "Food", ":)", "." ]
]
preprocess_corpus(sents)
for sent in sents:
for i in xrange(len(sent)):
print sent[i], ":", token2features(sent, i)