-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojection.py
executable file
·232 lines (179 loc) · 7.72 KB
/
projection.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
import networkx as nx
from networkx.algorithms.components import connected_components
import itertools
import argparse
from utils.cache import cache
from utils.data_reader import TripAdvisorDataReader
data_reader = TripAdvisorDataReader()
@cache('n')
def get_authors_with_at_least_reviews(n):
'Get the set of author IDs who created at least @n reviews'
return set(aid for aid, cnt in data_reader.count_reviews_aggregated_by('author_id').items() if cnt >= n)
@cache('n')
def get_hotels_with_at_least_reviews(n):
'Get the set of hotel IDs with at least @n reviews'
return set(hid for hid, cnt in data_reader.count_reviews_aggregated_by('hotel_id').items() if cnt >= n)
def aggregate_reviews_field(aggr_field, field, values, max_year):
'''Aggregate reviews field @field by field @aggr_field. Value of field @field must be
present in set @values'''
result = {}
for d in data_reader.reviews_with_max_year_month(max_year):
val = d[field]
if val not in values:
continue
agg_val = d[aggr_field]
if agg_val in result:
result[agg_val].add(val)
else:
result[agg_val] = { val }
return result
@cache('--min_reviews', 'max_year')
def get_hotel_review_authors_from(authors, max_year):
'''Get a mapping of hotels to sets of author IDs: for each hotel, the set of author IDs
who created reviews for that hotel. Only author IDs from the set @authors are considered.'''
return aggregate_reviews_field('hotel_id', 'author_id', authors, max_year)
@cache('--min_reviews', 'max_year')
def get_author_review_hotels_from(hotels, max_year):
'''Get a mapping of authors to sets of hotel IDs: for each author, the set of hotel IDs
for which the author created reviews. Only hotel IDs from the set @hotels are considered.'''
return aggregate_reviews_field('author_id', 'hotel_id', hotels, max_year)
@cache('nreviews', 'max_year')
def _create_author_projection(nreviews, max_year):
authors = get_authors_with_at_least_reviews(nreviews)
hotels = get_hotel_review_authors_from(authors, max_year)
G = nx.Graph()
# for every hotel get all authors that reviewed it
for hid, authors in hotels.items():
# and for every pair of authors
for u, v in itertools.combinations(authors, 2):
# increase association between the authors of that pair
if G.has_edge(u, v):
G[u][v]['n'] += 1
else:
G.add_edge(u, v, n=1)
max_n = max(G[u][v]['n'] for u, v in G.edges)
for u, v in G.edges:
G[u][v]['w'] = G[u][v]['n'] / max_n
return G
@cache('nreviews', 'max_year')
def _create_hotel_projection(nreviews, max_year):
hotels = get_hotels_with_at_least_reviews(nreviews)
authors = get_author_review_hotels_from(hotels, max_year)
G = nx.Graph()
# for every author get all hotels they reviewed
for aid, hotels in authors.items():
# and for every pair of hotels
for u, v in itertools.combinations(hotels, 2):
# increase association between the hotels of that pair
if G.has_edge(u, v):
G[u][v]['n'] += 1
else:
G.add_edge(u, v, n=1)
max_n = max(G[u][v]['n'] for u, v in G.edges)
for u, v in G.edges:
G[u][v]['w'] = G[u][v]['n'] / max_n
return G
def remove_edges_with_low_n(G, n, key='n'):
'Remove projection edges where number of common associations is less than @n'
# prepare a list of edges to remve
rm = []
for u, v in G.edges:
if G[u][v][key] < n:
rm.append((u, v))
# remove them
for u, v in rm:
G.remove_edge(u, v)
# remove isolated vertices
G.remove_nodes_from(list(nx.isolates(G)))
# it is possible that there are now multiple connected components,
# some of them may have too few vertices. Prepare a set of nodes
# from components with less than 10 vertices.
rm = set()
for c in connected_components(G):
if len(c) < 10:
rm.update(c)
# remove those nodes
G.remove_nodes_from(rm)
def remove_nodes_with_low_neighbors(G, n):
'Remove nodes with number of neighbors less than @n'
# repeat until all nodes have at least @n neighbors
while True:
rm = set()
for u in G.nodes:
if len(G[u]) < n:
rm.add(u)
if not rm:
break
G.remove_nodes_from(rm)
@cache('nreviews', 'min_common_hotels', 'min_neighbors', 'max_year')
def create_author_projection(nreviews, min_common_hotels, min_neighbors, max_year):
G = _create_author_projection(nreviews, max_year)
# remove edges that represent too few common hotels
remove_edges_with_low_n(G, min_common_hotels)
# remove nodes which have too few neighbors
remove_nodes_with_low_neighbors(G, min_neighbors)
# save attribtues
G.graph['nodes'] = 'authors'
G.graph['min_reviews'] = nreviews
G.graph['min_common'] = min_common_hotels
G.graph['min_neighbors'] = min_neighbors
G.graph['max_year'] = max_year
return G
@cache('nreviews', 'min_common_authors', 'min_neighbors', 'max_year')
def create_hotel_projection(nreviews, min_common_authors, min_neighbors, max_year):
G = _create_hotel_projection(nreviews, max_year)
# remove edges that represent too few common authors
remove_edges_with_low_n(G, min_common_authors)
# remove nodes which have too few neighbors
remove_nodes_with_low_neighbors(G, min_neighbors)
# save attributes
G.graph['nodes'] = 'hotels'
G.graph['min_reviews'] = nreviews
G.graph['min_common'] = min_common_authors
G.graph['min_neighbors'] = min_neighbors
if max_year is not None:
G.graph['max_year'] = max_year
hotels_gps = { k: v['gps'] for k, v in data_reader.hotels_by_id('gps').items() }
nx.set_node_attributes(G, hotels_gps, 'gps')
return G
parser = argparse.ArgumentParser(description='Utility to create and analyze TripAdvisor authors or hotel projection graph (Bipartite network projection)')
parser.add_argument('--cache', action='store_true',
help='cache the various computations / use cached values')
parser.add_argument('-y', '--max-year', type=int, metavar='Y',
help='consider only reviews published at most in year Y')
parser.add_argument('-p', '--projection-to', choices=['authors', 'hotels'], default='authors',
help='create a graph of authors / hotels')
parser.add_argument('-r', '--min-reviews', type=int, default=20, metavar='N',
help='consider only authors/hotels with at least N reviews, default 20')
parser.add_argument('-c', '--min-common', type=int, default=3, metavar='N',
help='consider only authors who reviewed at least N common hotels with another author, ' +\
'or hotels with at least N common authors with another hotel, default 3')
parser.add_argument('-n', '--min-neighbors', type=int, default=3, metavar='N',
help='drop authors/hotels with less than N neighbors in the constructed graph, default 3')
parser.add_argument('-i', '--input-dir', type=str,
help='input directory containing scraped or preprocessed data in json format')
parser.add_argument('-o', '--output', type=str, required=True,
help='file to output the graph to')
def main():
global args, data_reader
args = parser.parse_args()
if args.input_dir:
data_reader = TripAdvisorDataReader(args.input_dir)
node_type = args.projection_to
other_node_type = 'hotels' if node_type == 'authors' else 'authors'
print(f'creating {other_node_type} projection to {node_type} with')
print(f'- each node having at least {args.min_reviews} reviews')
print(f'- each node having at least {args.min_neighbors} neighbors')
print(f'- each edge representing at least {args.min_common} common {other_node_type}')
if args.max_year is not None:
print(f'- each review published at most in year {args.max_year}')
if node_type == 'authors':
G = create_author_projection(args.min_reviews, args.min_common, args.min_neighbors, args.max_year)
else:
G = create_hotel_projection(args.min_reviews, args.min_common, args.min_neighbors, args.max_year)
print(f'the projection graph has {len(G.nodes)} nodes with {len(G.edges)} edges')
nx.write_gpickle(G, args.output)
print(f'saved to {args.output}')
if __name__ == '__main__':
main()