-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentsFeaturesGenerator.py
More file actions
105 lines (94 loc) · 8.82 KB
/
CommentsFeaturesGenerator.py
File metadata and controls
105 lines (94 loc) · 8.82 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
103
104
105
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 14 13:21:32 2018
@author: angli
"""
import re, numpy as np, pandas as pd
URL_PATTERN=re.compile(r'''(?i)\b((?:https?:(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)/)(?:[^\s()<>{}\[\]]+|\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\))+(?:\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])|(?:(?<!@)[a-z0-9]+(?:[.\-][a-z0-9]+)*[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)\b/?(?!@)))''')
SMILEYS_PATTERN = re.compile(r"(?:X|:|;|=)(?:-)?(?:\)|\(|O|D|P|S){1,}", re.IGNORECASE)
NUMBERS_PATTERN = re.compile(r"(^|\s)(\-?\d+(?:\.\d)*|\d+)")
IP_PATTERN = re.compile(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
def trySum(c, dic):
comment = c['clean_comment']
try:
comment_lst = comment.split(" ")
ct=0
for x in comment_lst:
if x in dic:
ct += 1
return (ct)
except AttributeError:
return (0)
def tryScore(c):
comment = c['clean_comment']
try:
comment_lst = comment.split(" ")
ct=0
sumscore = 0
for x in comment_lst:
sumscore += sem_score.get(x.strip(),0.00)
ct += 1
return (sumscore/(ct+0.001))
except AttributeError:
return (0)
if __name__ == "__main__":
#loading the dictionary
positive_list = pd.read_table("/Users/angli/ANG/OneDrive/Documents/Pitt_PhD/Class/2018Spring/ML/finalProject/data/dictionary/positive-words.txt", header=None)
positive_list = list(positive_list[0])
negative_list = pd.read_table("/Users/angli/ANG/OneDrive/Documents/Pitt_PhD/Class/2018Spring/ML/finalProject/data/dictionary/negative-words.txt", header=None, encoding = "ISO-8859-1")
negative_list = list(negative_list[0])
sem_score = pd.read_table("/Users/angli/ANG/OneDrive/Documents/Pitt_PhD/Class/2018Spring/ML/finalProject/data/dictionary/SemEval2015-English-Twitter-Lexicon.txt", header=None)
sem_score = sem_score.rename(index=str, columns={0: "score", 1: "word"})
sem_score = pd.Series(sem_score.score.values,index=sem_score.word).to_dict()
#get the basic features from raw comments
train = pd.read_csv("/Users/angli/ANG/OneDrive/Documents/Pitt_PhD/Class/2018Spring/ML/finalProject/data/train.csv")
train['total_length'] = train['comment_text'].apply(len)
train['capitals'] = train['comment_text'].apply(lambda comment: sum(1 for c in comment if c.isupper()))
train['caps_vs_length'] = train.apply(lambda row: float(row['capitals'])/float(row['total_length'] + 0.001), axis=1)
train['num_exclamation_marks'] = train['comment_text'].apply(lambda comment: comment.count('!'))
train['num_question_marks'] = train['comment_text'].apply(lambda comment: comment.count('?'))
train['num_punctuation'] = train['comment_text'].apply(lambda comment: sum(comment.count(w) for w in '.,;:'))
train['num_symbols'] = train['comment_text'].apply(lambda comment: sum(comment.count(w) for w in '*&$%'))
train['num_we'] = train['comment_text'].apply(lambda comment: sum(comment.count(w) for w in ['we', 'We', 'WE']))
train['num_words'] = train['comment_text'].apply(lambda comment: len(comment.split(" ")))
train['num_unique_words'] = train['comment_text'].apply(lambda comment: len(set(w for w in comment.split())))
train['words_vs_unique'] = train['num_unique_words'] / (train['num_words']+0.0001)
train['num_smilies'] = train['comment_text'].apply(lambda comment: sum(comment.count(w) for w in (':-)', ':)', ';-)', ';)')))
train['num_IP'] = train['comment_text'].apply(lambda comment: len(re.findall(IP_PATTERN, comment)))
train['num_URL'] = train['comment_text'].apply(lambda comment: len(re.findall(URL_PATTERN, comment)))
#features from dictionary
clean_train = pd.read_csv("/Users/angli/ANG/OneDrive/Documents/Pitt_PhD/Class/2018Spring/ML/finalProject/data/train_cleaned.csv")
#train['comment_text_lower'] = train['comment_text'].str.lower()
clean_train['num_positive'] = clean_train.apply(trySum, axis=1, dic = positive_list)
clean_train['num_negtive'] = clean_train.apply(trySum, axis=1, dic = negative_list)
clean_train['score'] = clean_train.apply(tryScore, axis=1)
clean_train = clean_train[['id', 'num_positive', 'num_negtive', 'score']]
#merge all features together
train = pd.merge(train, clean_train, on = "id")
train = train[['id', 'total_length', 'capitals', 'caps_vs_length', 'num_exclamation_marks', 'num_question_marks', 'num_punctuation', 'num_symbols', 'num_we', 'num_words', 'num_unique_words', 'words_vs_unique', 'num_smilies', 'num_IP', 'num_URL', 'num_positive', 'num_negtive', 'score', 'toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']]
train.to_csv("train_features.csv", index=False)
# test = pd.read_csv("/Users/angli/ANG/OneDrive/Documents/Pitt_PhD/Class/2018Spring/ML/finalProject/data/test.csv")
# #get the basic features
# test['total_length'] = test['comment_text'].apply(len)
# test['capitals'] = test['comment_text'].apply(lambda comment: sum(1 for c in comment if c.isupper()))
# test['caps_vs_length'] = test.apply(lambda row: float(row['capitals'])/float(row['total_length'] + 0.001), axis=1)
# test['num_exclamation_marks'] = test['comment_text'].apply(lambda comment: comment.count('!'))
# test['num_question_marks'] = test['comment_text'].apply(lambda comment: comment.count('?'))
# test['num_punctuation'] = test['comment_text'].apply(lambda comment: sum(comment.count(w) for w in '.,;:'))
# test['num_symbols'] = test['comment_text'].apply(lambda comment: sum(comment.count(w) for w in '*&$%'))
# test['num_we'] = test['comment_text'].apply(lambda comment: sum(comment.count(w) for w in ['we', 'We', 'WE']))
# test['num_words'] = test['comment_text'].apply(lambda comment: len(comment.split(" ")))
# test['num_unique_words'] = test['comment_text'].apply(lambda comment: len(set(w for w in comment.split())))
# test['words_vs_unique'] = test['num_unique_words'] / (train['num_words']+0.0001)
# test['num_smilies'] = test['comment_text'].apply(lambda comment: sum(comment.count(w) for w in (':-)', ':)', ';-)', ';)')))
# test['num_IP'] = test['comment_text'].apply(lambda comment: len(re.findall(IP_PATTERN, comment)))
# test['num_URL'] = test['comment_text'].apply(lambda comment: len(re.findall(URL_PATTERN, comment)))
# #features from dictionary
# test['comment_text_lower'] = test['comment_text'].str.lower()
# test['num_positive'] = test['comment_text_lower'].apply(lambda comment: sum(comment.count(w) for w in positive_list))
# test['num_negtive'] = test['comment_text_lower'].apply(lambda comment: sum(comment.count(w) for w in negative_list))
# test['score'] = test['comment_text_lower'].apply(lambda comment: sum(sem_score.get(c.strip(),0.00) for c in comment.split(" ")))
# test['score'] = test.apply(lambda row: float(row['score'])/float(row['num_words'] + 0.001), axis=1)
# test = test[['id', 'total_length', 'capitals', 'caps_vs_length', 'num_exclamation_marks', 'num_question_marks', 'num_punctuation', 'num_symbols', 'num_we', 'num_words', 'num_unique_words', 'words_vs_unique', 'num_smilies', 'num_IP', 'num_URL', 'num_positive', 'num_negtive', 'score']]
# test.to_csv("test_features.csv", index=False)