-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrawler.py
70 lines (55 loc) · 2 KB
/
Crawler.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
import nltk
import re
from Connection import Connection
from CrudImage import CrudImage
class Crawler:
def insertWord(self, word):
# print(word)
conn = Connection().conn()
cursor = conn.cursor()
sql = "Select id from palavras where palavra = '{word}'".format(word = word)
# print("sql: ", sql)
cursor.execute(sql)
result = cursor.fetchone()
if(result == None):
sql = "Insert Into palavras (palavra) Values (%s)"
cursor.execute(sql, (word, ))
conn.commit()
result = cursor.lastrowid
else:
result = result[0]
cursor.close()
conn.close()
return result
def inserLocationWord(self, docId, wordId, position):
# wordId = self.insertWord(word)
conn = Connection().conn()
cursor = conn.cursor()
sql = "INSERT INTO localizacao_palavra (id_doc, id_palavra, posicao) VALUES (%s, %s, %s)"
cursor.execute(sql, (docId, wordId, position))
conn.commit()
cursor.close()
conn.close()
def splitterWord(self, texto):
stop = nltk.corpus.stopwords.words('portuguese')
stemmer = nltk.stem.RSLPStemmer()
splitter = re.compile('\\W*')
lista_palavras = []
lista = [p for p in splitter.split(texto) if p != '']
for p in lista:
if p.lower() not in stop:
if len(p) > 1:
lista_palavras.append(stemmer.stem(p).lower())
return lista_palavras
def insetDocument(self, doc, text):
words = self.splitterWord(text)
crud = CrudImage()
docId = crud.insert(doc, text)
for i in range(len(words)):
word = words[i]
idWord = self.insertWord(word)
self.inserLocationWord(docId, idWord, i)
return docId
# result = Crawler().insetDocument("doc", "texto de exemplo para teste")
# result = Crawler().insertWord("show")
# print(result)