forked from kylehg/summarizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
172 lines (127 loc) · 5.12 KB
/
utils.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
"""Common utilities for working with text, etc."""
import os
import math
from collections import defaultdict
from nltk import tokenize
from memoizer import memoized
PROJECT_ROOT = '/home1/c/cis530/final_project'
INPUT_ROOT = PROJECT_ROOT + '/input'
MODELS_ROOT = PROJECT_ROOT + '/models'
BASELINE_ROOT = PROJECT_ROOT + '/baseline'
STEMMED_IDF_FILE = 'bgIdfValues.stemmed.txt'
UNSTEMMED_IDF_FILE = 'bgIdfValues.unstemmed.txt'
# The max and min word count to consider for a summary sentance.
MIN_SENT_LEN = 10
MAX_SENT_LEN = 55
# The maximum similarity between two sentences that one should be
# considered a duplicate of the other.
MAX_SIM_CUTOFF = 0.4
# Token and document utils
# ------------------------
def ls(path):
return [os.path.join(path, item) for item in os.listdir(path)]
def load_file_sents(path):
return [sent.lower()
for sent in tokenize.sent_tokenize(open(path).read())]
def load_collection_sents(path):
sents = []
for f in ls(path):
sents.extend(load_file_sents(f))
return sents
def get_sentences(path):
""" loads sentences from the given path (collection or file) """
sents = []
try:
# treat as a single file
open(path).read()
sents = load_file_sents(path)
except IOError:
# it's a directory!
sents = load_collection_sents(path)
return sents
def get_toks(path):
return [tokenize.word_tokenize(sent) for sent in get_sentences(path)]
def get_collections(fullpath=True):
"""Return a list of tuples of (documents, summaries, baselines)
for each collection."""
docs = sorted(ls(collection) if fullpath else os.listdir(collection)
for collection in ls(INPUT_ROOT))
models = sorted(ls(collection) if fullpath else os.listdir(collection)
for collection in ls(MODELS_ROOT))
baselines = sorted(ls(BASELINE_ROOT) if fullpath else
os.listdir(BASELINE_ROOT))
return zip(range(50), docs, models, baselines)
# Vectors and similarities
# ------------------------
def cosine_sim(x, y, vect_fun=None):
"""Return the cosine similarity between two vectors, defined as:
(sum over X, Y of (x * w)) /
sqrt(sum over X of x^2) * sqrt(sum over Y of y^2)
If a vectorize function is provided, assumes that x, y are a list
of tokens and compares by vectorizing with the given vector function.
"""
if vect_fun:
feat_space = feature_space(x, y)
x, y = vect_fun(feat_space, x), vect_fun(feat_space, y)
assert len(x) == len(y), 'Vectors are not the same length.'
zipped = zip(x, y)
top = float(sum(v * w for v, w in zipped))
bot = (math.sqrt(sum(pow(v, 2) for v in x))
* math.sqrt(sum(pow(w, 2) for w in y)))
try:
return top / bot
except ZeroDivisionError:
return top / 0.00001
def binary_vectorize(feature_space, doc):
"""Given a set of words as a feature space and a tokenized document,
return a (binary) vector representation of that document."""
return [1 if point in doc else 0 for point in feature_space]
def freq_vectorize(feature_space, doc):
freqs = defaultdict(lambda: 0)
for word in doc:
freqs[word] += 1
return [freqs[point] if point in freqs else 0
for point in feature_space]
@memoized
def load_idf_weights():
f = open(UNSTEMMED_IDF_FILE, 'r')
f.readline() # Ignore first line
return {line.split()[0]: float(line.split()[1]) for line in f}
def tfidf_vectorize(feature_space, doc):
idfs = load_idf_weights()
freq_vect = freq_vectorize(feature_space, doc)
return [freq * idfs[point] if point in idfs else 0
for freq, point in zip(freq_vect, feature_space)]
def feature_space(doc1, doc2):
"""Given two lists of tokens, return a common feature set."""
return sorted(set(doc1) | set(doc2))
# Summarizer utils
# ----------------
def is_valid_sent_len(sent, min_len=MIN_SENT_LEN, max_len=MAX_SENT_LEN):
"""Takes a list of tokens, returns if valid token length."""
return min_len <= len(sent) <= max_len
def is_repeat(sent, sents, vect_fun=tfidf_vectorize, max_sim=MAX_SIM_CUTOFF):
"""Given a tokenized sentence and a list of tokenized sentences,
return whether the sentences overlaps too highly in content with any
of the others."""
# TODO: Incorporate synonyms to better discern similarity
for other_sent in sents:
feat_space = feature_space(sent, other_sent)
x, y = vect_fun(feat_space, sent), vect_fun(feat_space, other_sent)
if cosine_sim(x, y) > max_sim:
return True
return False
def gen_summaries(name, summary_fun, start=0, end=50):
collections = get_collections()[start:end]
sums = []
for i, docs, models, baseline in collections:
collection = os.path.dirname(docs[0])
sum_name = 'summary%02d.txt' % i
collection_sents = get_sentences(collection)
summary = '\n'.join(summary_fun(collection_sents, 100))
with open(os.path.join('rouge', name, sum_name), 'w') as f:
f.write(summary)
sums.append((sum_name, map(os.path.basename, models)))
return sums
if __name__ == '__main__':
pass