-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexicon.py
51 lines (43 loc) · 2.04 KB
/
lexicon.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
import sqlite3
from os import path
DB_PATH = r"{path}/db/lexicon.db".format(path=path.dirname(path.abspath(__file__)))
QUERIES_PATH = path.dirname(path.abspath(__file__)) + '/queries'
class Lexicon:
def __init__(self):
self.connection = sqlite3.connect(DB_PATH)
def get_lexicon_mention(self, target_words=[]):
template_name = 'get_mentions_urls'
if not target_words:
template_name = 'read_all_lexicon'
with open('{path}/{name}.sql'.format(path=QUERIES_PATH, name=template_name), 'r') as query_file:
query_template = query_file.read()
cursor = self.connection.cursor()
values = "'" + "','".join(target_words) + "'"
query = query_template.format(target_words=values)
lexicon = {}
mention_urls_pairs = cursor.execute(query).fetchall()
for mention, url in mention_urls_pairs:
if mention not in lexicon.keys():
lexicon.update({mention: []})
lexicon[mention].append(url)
return lexicon
def insert_mentions_uris(self, lexicon):
with open('{path}/insert_mention.sql'.format(path=QUERIES_PATH), 'r') as query_file:
query = query_file.read()
cursor = self.connection.cursor()
if lexicon:
cursor.executemany(query, lexicon)
def insert_categories_uri(self, uri, categories):
with open('{path}/insert_category.sql'.format(path=QUERIES_PATH), 'r') as query_file:
query = query_file.read()
cursor = self.connection.cursor()
if categories:
uri_categories = [(uri, category) for category in categories]
cursor.executemany(query, uri_categories)
def insert_links_uri(self, source_uri, linked_uris):
with open('{path}/insert_link.sql'.format(path=QUERIES_PATH), 'r') as query_file:
query = query_file.read()
cursor = self.connection.cursor()
if linked_uris:
uri_links = [(source_uri, target_uri) for target_uri in linked_uris]
cursor.executemany(query, uri_links)