-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
157 lines (125 loc) · 5.08 KB
/
__init__.py
File metadata and controls
157 lines (125 loc) · 5.08 KB
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
# This package will contain the spiders of your Scrapy project
#
# Please refer to the documentation for information on how to create and manage
# your spiders.
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
from string import punctuation
import numpy as np
def compute_cosine_similarity(vector1, vector2):
sum = 0
square1 = 0
square2 = 0
for i,item in enumerate(vector1):
sum += item*vector2[i]
square1 += item**2
square2 += vector2[i]**2
if square1*square2 == 0: return 0
return sum/(np.sqrt(square1)*np.sqrt(square2))
def vectorice(texto, indexeds):
stop_words = set(stopwords.words('spanish'))
token = word_tokenize(texto)
ps = PorterStemmer()
vector = {}
for x in indexeds:
vector[x] = 0
for x in token:
x = ps.stem(x.lower())
if x in indexeds:
if not x in vector:
vector[x] = 0
vector[x] += 1
_max = max(vector.values())
return [vector[x]/_max for x in indexeds]
# Esta implementacion ahora mismo no garantiza q todos los terminos indexados
# sean sustantivos.
def indexed_terms(texto):
stop_words = set(stopwords.words('spanish'))
token = word_tokenize(texto)
#input('token = {0}'.format(token))
ps = PorterStemmer()
vector = {}
temp = []
for x in token:
x = ps.stem(x.lower())
if x in temp or x in punctuation or x.isdigit():
continue
if x in stop_words:
temp.append(x)
continue
if not x in vector:
vector[x] = 0
vector[x] += 1
_max = max(vector.values())
indexados = sorted(vector.keys(), key=lambda x: vector[x],reverse=True)[:50]
#input('indexados = {0}'.format(indexados))
return (vector.keys(),[vector[x]/_max for x in vector])
# CODE
import scrapy
from scrapy.selector import Selector
from scrapy.http.response.html import HtmlResponse
from scrapy.linkextractors import LinkExtractor
from scrapy.link import Link
from scrapy.http import Request, Response
import requests
class QuotesSpider(scrapy.Spider):
name = "quotes"
def __init__(self, category=None, *args, **kwargs):
super(QuotesSpider, self).__init__(*args, **kwargs)
self.descargas = 0
# Profe aqui la url puede que se la devuelva mal xq hasta el momento
# estuve trabajando con el servidor de la aplicacion de Wikipedia
# y no llegue a poner las mas populares de Google.
self.madre_url = 'http://10.127.127.1:8001/wikipedia_es_all_2017-01/A/Pintura.html' #'https://es.wikipedia.org/wiki/Pintura'
texto = self.extract_text_url(self.madre_url)
with open('./madre.txt','wb') as f:
f.write(texto.encode())
self.indexed, self.madre_vetor = indexed_terms(texto)
self.to_recolect = 5 #10 000
# Profe a lo mejor estas urls dan error xq las puse a mano no se si estan bien escritas
def start_requests(self):
urls = [
'http://10.127.127.1:8001/wikipedia_es_all_2017-01/A/Pintura.html',
'http://10.127.127.1:8001/wikipedia_es_all_2017-01/A/Color.html', #'https://es.wikipedia.org/wiki/Color',
'http://10.127.127.1:8001/wikipedia_es_all_2017-01/A/Luz.html', #'https://es.wikipedia.org/wiki/Luz',
'http://10.127.127.1:8001/wikipedia_es_all_2017-01/A/Dibujo.html']
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def extract_text(self,response):
sel = Selector(response)
e = sel.xpath('.//text()').getall()
style_text = sel.xpath('//style/text()').get()
script_text = sel.xpath('//script/text()').get()
texto = ''.join(e).replace(style_text,'').replace(script_text,'').replace('\n\n','')
#input('texto = {0}'.format(texto))
return texto
def extract_text_url(self,url):
response = requests.get(url)
return self.extract_text(response)
# La cola inicial de parse es start_request
def parse(self, response:HtmlResponse):
texto = self.extract_text(response)
vector = vectorice(texto,self.indexed)
similarity = compute_cosine_similarity(vector,self.madre_vetor)
print('url = {0}'.format(response.url))
print('similarity = {0}'.format(similarity))
input('look')
if similarity < 0.2:
return
splited = response.url.split('/')
splited = [x for x in splited if x != '']
page_name = ''.join(splited[len(splited)-1])
with open('web_pages/{0}.html'.format(page_name),'wb') as f:
f.write(response.body)
self.descargas += 1
input('downloaded: {0}'.format(response.url))
input('descargas = {0}'.format(self.descargas))
if self.descargas == self.to_recolect:
return
extractor = LinkExtractor()
links = extractor.extract_links(response)
for link in links:
if link is not None:
requests
yield Request(link.url)