-
Notifications
You must be signed in to change notification settings - Fork 0
/
Meme.py
164 lines (97 loc) · 4.24 KB
/
Meme.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
# ----------- Meme.py --------------
# this file contains the class definition for a meme, which will contain
# our representation of one.
from nltk.tokenize import word_tokenize, wordpunct_tokenize, sent_tokenize
from nltk import ngrams
from nltk.classify import *
from nltk import PorterStemmer
from collections import defaultdict
from random import shuffle
import operator
import math
import sys
import csv
import pickle
# Function: convert_to_feature_vector
# -----------------------------------
# dictionary representation of a meme -> feature vector
def convert_to_feature_vector (dict_rep):
print dict_rep['top_text']
print
meme_rep = Meme (dict_rep['meme_type'], dict_rep['top_text'], dict_rep['bottom_text'])
return meme_rep.get_features ()
class Meme:
##########################################################################################
##############################[ --- Instance Variables --- ]##############################
##########################################################################################
#--- meme_type ---
meme_type = None
#--- raw text ---
top_text_raw = None
bottom_text_raw = None
#--- tokenized text ---
top_text = None
bottom_text = None
all_text = None
#--- feature representations ---
ngram_features = {}
tfidf_features = {}
sentiment_features = {}
##########################################################################################
##############################[ --- Constructor/Initialization --- ]######################
##########################################################################################
# Function: Constructor
# ---------------------
# given the raw top/bottom text, this will fill in all the data.
def __init__(self, meme_type, top_text_raw, bottom_text_raw):
### Step 1: fill in basic data ###
self.meme_type = meme_type
self.top_text_raw = top_text_raw
self.bottom_text_raw = bottom_text_raw
### Step 2: get tokenized versions ###
self.tokenize ();
# Function: tokenize
# ------------------
# self.(top|bottom)_text_raw -> self.(top|bottom|all)_text
def tokenize (self):
top_sentences = word_tokenize (self.top_text_raw)
bottom_sentences = word_tokenize (self.bottom_text_raw)
top_tokenized_sentences = [word_tokenize (s) for s in top_sentences]
bottom_tokenized_sentences = [word_tokenize (s) for s in bottom_sentences]
self.top_text = []
for s in top_tokenized_sentences:
for word in s:
self.top_text.append (word)
self.bottom_text = []
for s in bottom_tokenized_sentences:
for word in s:
self.bottom_text.append(word)
self.all_text = ' '.join(self.top_text + self.bottom_text)
##########################################################################################
##############################[ --- Feature Representations --- ]#########################
##########################################################################################
# Function: get_ngram_features
# ----------------------------
# fills in ngram_features with a dict of stemmed_word:True pairs.
# (NOTE: currently only unigrams. Add in common bigrams?)
def get_ngram_features (self):
stemmer = PorterStemmer ()
top_features = [(stemmer.stem(token) + "__TOP__", True) for token in self.top_text]
bottom_features = [(stemmer.stem(token) + "__BOTTOM__", True) for token in self.bottom_text]
all_features = [(stemmer.stem(token) + "__ALL__", True) for token in self.all_text]
self.ngram_features = dict(top_features + bottom_features + all_features);
# Function: get_features
# ----------------------
# returns a feature vector representation of this meme
# since we are training maxent, it will be a dict of word:True pairs
def get_features (self):
self.get_ngram_features ()
return self.ngram_features
##########################################################################################
##############################[ --- String Representations --- ]##########################
##########################################################################################
# Function: string representation
# -------------------------------
# a string representation of the meme
def __str__ (self):
return self.meme_type + ": " + self.top_text_raw + " / " + self.bottom_text_raw