-
Notifications
You must be signed in to change notification settings - Fork 0
/
imdb_final.py
229 lines (172 loc) · 6.96 KB
/
imdb_final.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
from networkx.generators import directed
from nltk.stem import WordNetLemmatizer
from nltk import pos_tag
import numpy as np
import math
from nltk.util import pr
from numpy.core.fromnumeric import partition
import pandas as pd
import requests
import re
import nltk
from nltk import word_tokenize
from nltk.corpus import stopwords
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import networkx as nx
#implementation of textRank
class TextRank:
def __init__(self,stl):
self.stl = stl
pass
def word_prepration(self):
#case folding
lower_text = self.stl.lower()
#del numbers
clear_text = re.sub("\d+","", lower_text)
#tokenize words
tokenized_words = word_tokenize(clear_text)
#delete stop words
stop_words = stopwords.words("english")
non_stops = []
for word in tokenized_words:
if word not in stop_words:
if word not in non_stops:
non_stops.append(word)
wl = WordNetLemmatizer()
# tags = {"adj":['JJ','JJR','JJS'], "noun":['NN', 'NNS']}
taged_text = pos_tag(non_stops)
lemmatized_words = []
for word in taged_text:
w = wl.lemmatize(word[0], pos="a")
w = wl.lemmatize(w, pos="n")
w = wl.lemmatize(w, pos="v")
w = wl.lemmatize(w, pos="r")
lemmatized_words.append(w)
#create vocab
vocab = list(set(lemmatized_words))
return vocab
def matrix_preration(self):
vocabulary = self.word_prepration()
vocab_len = len(vocabulary)
weighted_edge = np.zeros((vocab_len,vocab_len),dtype=np.float32)
score = np.zeros((vocab_len),dtype=np.float32)
window_size = 3
covered_occurrences = []
for i in range(0,vocab_len):
score[i]=1
for j in range(0,vocab_len):
if j==i:
weighted_edge[i][j]=0
else:
for window_start in range(0,(len(vocabulary)-window_size+1)):
window_end = window_start+window_size
window = vocabulary[window_start:window_end]
if (vocabulary[i] in window) and (vocabulary[j] in window):
index_of_i = window_start + window.index(vocabulary[i])
index_of_j = window_start + window.index(vocabulary[j])
if [index_of_i,index_of_j] not in covered_occurrences:
weighted_edge[i][j] += 1
covered_occurrences.append([index_of_i,index_of_j])
return weighted_edge,score,vocabulary
def score_provider(self):
weighted_edge,score,vocabulary = self.matrix_preration()
vocab_len = len(vocabulary)
inout = np.zeros((vocab_len),dtype=np.float32)
for i in range(0,vocab_len):
for j in range(0,vocab_len):
inout[i]+=weighted_edge[i][j]
dictionary={}
MAX_ITERATIONS = 50
d=0.85
threshold = 0.0001 #convergence threshold
for iter in range(0,MAX_ITERATIONS):
prev_score = np.copy(score)
for i in range(0,vocab_len):
summation = 0
for j in range(0,vocab_len):
if weighted_edge[i][j] != 0:
summation += (weighted_edge[i][j]/inout[j])*score[j]
score[i] = (1-d) + d*(summation)
if np.sum(np.fabs(prev_score-score)) <= threshold: #convergence condition
# print("Converging at iteration "+str(iter)+"....")
score = sorted(score)
for i in range(0,vocab_len):
dictionary[vocabulary[i]] =score[i]
dictionary = {k:v for k,v in sorted(dictionary.items(), key=lambda item: item[1],reverse=True)}
import itertools
i=0
for keys,values in dictionary.items():
if(i<10):
# print("Score of "+keys+": "+str(values))
i+=1
break
return(list(dictionary.keys()))
url = "https://www.imdb.com/chart/top/"
#make beautifulsoup object
def r_soup(web_url):
page = requests.get(web_url)
soup = BeautifulSoup(page.content, 'html.parser')
return soup
# print(soup.prettify())
# print(type(soup))
results = r_soup(url).find_all("td", class_="titleColumn")
links = []
movie_story=[]
movie_keyword={}
new_url = re.sub("/chart/top/","",url)
for result in results:
link = result.find("a").attrs.get('href')
complete_link = new_url + link
links.append(complete_link)
# print(links)
def story_line(web_url):
for url in web_url:
sub_page = r_soup(url)
# story_line_txt =sub_page.find("div",id="titleStoryLine").find("div",class_=("inline canwrap")).find("span").text
story_line_txt = sub_page.find("div", class_ = "Storyline__StorylineWrapper-sc-1b58ttw-0").find("div", class_ = "ipc-overflowText").find("div", class_ = "ipc-html-content").find("div").text
# title= re.sub(r"[(\d)]","",sub_page.find("div",class_="title_wrapper").find("h1").text)
t = re.sub(r"[(\d)]", "", sub_page.find("div", class_ = "TitleBlock__TitleContainer-sc-1nlhx7j-1").find("h1").text)
movie_story.append(dict(title = re.sub("[\...]$","",t), stry=story_line_txt))
# print(movie_story)
return movie_story
mvst = story_line(links)
# print(mvst)
d = {} #keeps title and keywords of storyLine
all = [] #adjacency list for weight of each node
labeldict = {} #to be used in networkx for labeling the nodes
for index, movie in enumerate(movie_story):
title = movie['title']
labeldict[index]=title
stry = movie['stry']
tr = TextRank(stry)
d[title] = tr.score_provider()
#check for common keywords
for k, v in d.items():
w = []
for kk, vv in d.items():
inter = len(set(v).intersection(set(vv)))
if k!=kk and inter:
w.append(inter)
else:
w.append(0)
all.append(w)
arr = np.array(all) #convert list to numpy array
# print(arr)
# up = np.triu(arr, k=0)
# print(up)
#creating graph
# G = nx.from_numpy_matrix(np.matrix(up), create_using=nx.DiGraph)
# layout = nx.spring_layout(G)
# nx.draw(G, layout, labels = labeldict, with_labels = True)
# nx.draw_networkx_edge_labels(G, pos=layout)
# #write graph to csv file
# nx.write_edgelist(G, "edge.csv")
# plt.show()
G = nx.from_numpy_matrix(np.matrix(arr), create_using=nx.Graph)
layout = nx.spring_layout(G)
nx.draw(G, layout, labels = labeldict, with_labels = True)
nx.draw_networkx_edge_labels(G, pos=layout)
#write graph to csv file
nx.write_edgelist(G, "edge.csv")
plt.show()