forked from alfredolainez/news-media-topics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topics.py
160 lines (114 loc) · 4.78 KB
/
topics.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
from words_graph import SimpleGraphBuilder, NounPhraseGraphBuilder
from extractor import NewsScraper
import graph_cluster
import text_processing
import time
import community
import networkx as nx
import matplotlib.pyplot as plt
def get_words_by_partition(partition):
"""
Given a community partition of the form:
{ "word1": 2, "word2": 1, "word3": 1 .... }
it returns the inverse dictionary:
{ 1: ["word1", "word3"], 2: ["word2"] .... }
"""
words_by_part = {}
for elem in partition:
if partition[elem] not in words_by_part:
words_by_part[partition[elem]] = [elem]
else:
words_by_part[partition[elem]].append(elem)
return words_by_part
def get_news(url, number, nthreads = 10):
"""
Retrieves news from the specified source
"""
t0 = time.time()
news = NewsScraper(url, nthreads = nthreads)
news.pull()
news.scrape(number)
texts = [article['text'] for article in news.polished()]
print "Scraped %d articles" % len(texts)
return texts
def get_topics_from_partitions(G, words_by_part, num_words_per_topic=10):
topics = []
for counter in xrange(0, len(words_by_part)):
H = G.subgraph(words_by_part[counter])
topics.append(graph_cluster.pagerank_top_k(H, num_words_per_topic).tolist())
return topics
def print_topics_from_partitions(G, words_by_part, num_words_per_topic=10):
for counter in xrange(0, len(words_by_part)):
print '\nTopic {}:\n----------'.format(counter)
H = G.subgraph(words_by_part[counter])
print ', '.join(graph_cluster.pagerank_top_k(H, num_words_per_topic))
def get_topics_by_standard_words(num_news, draw=False, url='http://cnn.com'):
texts = get_news(url, num_news)
gb = SimpleGraphBuilder(text_processing.clean_punctuation_and_stopwords)
gb.load_texts(texts)
G = gb.create_graph()
print "Graph built"
partition = community.best_partition(G)
words_by_part = get_words_by_partition(partition)
mod = community.modularity(partition,G)
print("modularity:", mod)
print_topics_from_partitions(G, words_by_part, 10)
if draw:
values = [partition.get(node) for node in G.nodes()]
nx.draw_spring(G, cmap = plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=False)
plt.show()
return G
def get_topics_non_dictionary(num_news, draw=False, url='http://cnn.com'):
texts = get_news(url, num_news)
gb = SimpleGraphBuilder(text_processing.only_non_dictionary_words, stem_words=False)
gb.load_texts(texts)
G = gb.create_graph()
print "Graph built"
partition = community.best_partition(G)
words_by_part = get_words_by_partition(partition)
mod = community.modularity(partition,G)
print("modularity:", mod)
print_topics_from_partitions(G, words_by_part, 10)
if draw:
values = [partition.get(node) for node in G.nodes()]
nx.draw_spring(G, cmap = plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=False)
plt.show()
topics = get_topics_from_partitions(G, words_by_part, 10)
return G, topics
def get_topics_noun_phrases(num_news, draw=False, url='http://cnn.com'):
texts = get_news(url, num_news)
gb = NounPhraseGraphBuilder(text_processing.clean_punctuation_and_stopwords)
gb.load_texts(texts)
G = gb.create_graph()
print "Graph built"
partition = community.best_partition(G)
words_by_part = get_words_by_partition(partition)
print_topics_from_partitions(G, words_by_part, 10)
mod = community.modularity(partition,G)
print("modularity:", mod)
#print_topics_from_partitions(G, words_by_part, 10)
if draw:
values = [partition.get(node) for node in G.nodes()]
nx.draw_spring(G, cmap = plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=False)
plt.show()
topics = get_topics_from_partitions(G, words_by_part, 10)
return G, topics
def get_topics_non_dictionary_overlapping(num_news, k, url='http://cnn.com'):
texts = get_news(url, num_news)
gb = SimpleGraphBuilder(text_processing.only_non_dictionary_words, stem_words=False)
gb.load_texts(texts)
G = gb.create_graph()
print "Graph built"
words_by_part = graph_cluster.get_overlap_clusters(G, k, 1)
#print_topics_from_partitions(G, words_by_part, 10)
return G
def get_topics_noun_phrases_overlapping(num_news, k, url='http://cnn.com'):
texts = get_news(url, num_news)
gb = NounPhraseGraphBuilder(text_processing.clean_punctuation_and_stopwords)
gb.load_texts(texts)
G = gb.create_graph()
print "Graph built"
words_by_part = graph_cluster.get_overlap_clusters(G, k, 1)
#print_topics_from_partitions(G, words_by_part, 10)
topics = get_topics_from_partitions(G, words_by_part, 10)
return G, topics