-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
167 lines (131 loc) · 4.9 KB
/
search.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
import os
import numpy as np
import pandas as pd # can't use polars
import pymysql.cursors
from omegaconf import OmegaConf
from sentence_transformers import SentenceTransformer
class Search:
def __init__(self, config: str) -> None:
"""
初期設定を行う
Args:
config (str): 設定ファイルパス
"""
self.config = OmegaConf.load(config)
self.model = SentenceTransformer(self.config.bert_model)
self.category = "Fairness"
self.title_embed = np.load(
os.path.join(self.config.path_data, f"{self.category}_title_embed.npy")
)
self.abst_embed = np.load(
os.path.join(self.config.path_data, f"{self.category}_abstract_embed.npy")
)
def setup(self, category: str) -> None:
"""
選択されたカテゴリー用のファイルを読み込む
Args:
category (str): 論文カテゴリー
"""
self.category = category
self.title_embed = np.load(
os.path.join(self.config.path_data, f"{self.category}_title_embed.npy")
)
self.abst_embed = np.load(
os.path.join(self.config.path_data, f"{self.category}_abstract_embed.npy")
)
def search_title(self, title: str, top: str) -> pd.DataFrame:
"""
類似タイトルの論文検索を行う
Args:
title (str): 論文タイトル
top (str): 上位何件取得するか
Returns:
pd.DataFrame: 検索結果
"""
pred = self.model.encode([title]).squeeze()
prob = np.dot(self.title_embed, pred)
rank = np.argsort(prob)[::-1][0 : int(top)]
return self.save_as_dataframe(rank)
def search_abst(self, abst: str, top: str) -> pd.DataFrame:
"""
類似アブストラクトの論文検索を行う
Args:
title (str): 論文アブストラクト
top (str): 上位何件取得するか
Returns:
pd.DataFrame: 検索結果
"""
pred = self.model.encode([abst]).squeeze()
prob = np.dot(self.abst_embed, pred)
rank = np.argsort(prob)[::-1][0 : int(top)]
return self.save_as_dataframe(rank)
def search_keyword(
self, key1: str, key2: str, key3: str, target: str, top: str
) -> pd.DataFrame:
"""
キーワードによる論文検索を行う
Args:
key1 (str): 1つ目のキーワード
key2 (str): 2つ目のキーワード
key3 (str): 3つ目のキーワード
target (str): 検索対象(タイトル or アブストラクト)
top (str): 上位何件取得するか
Returns:
pd.DataFrame: 検索結果
"""
counts = np.zeros(len(self.title_embed))
for keyword in [key1, key2, key3]:
connection = pymysql.connect(
host="localhost",
user="root",
password=self.config.password,
database="paper",
cursorclass=pymysql.cursors.DictCursor,
)
with connection:
with connection.cursor() as cursor:
sql = f"select id, title, link from {self.category} \
where {target} like '%{keyword}%'"
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
counts[row["id"] - 1] += 1
rank = np.argsort(np.array(counts))[::-1][0 : int(top)]
return self.save_as_dataframe(rank)
def save_as_dataframe(self, rank: str) -> pd.DataFrame:
"""
SQLの問い合わせ結果をpd.DataFrame形式で出力する
Args:
rank (str): 上位何件を取得するか
Returns:
pd.DataFrame: 検索結果
"""
connection = pymysql.connect(
host=self.config.host,
user="root",
password=self.config.password,
database="paper",
cursorclass=pymysql.cursors.DictCursor,
)
with connection:
with connection.cursor() as cursor:
s = ""
for x in rank:
s += "id=" + str(x + 1) + " OR "
sql = (
f"SELECT id, title, author, link FROM {self.category} WHERE "
+ s.removesuffix(" OR ")
)
cursor.execute(sql)
rows = cursor.fetchall()
dict = {}
for i in range(len(rank)):
dict[rank[i] + 1] = i
result_list = sorted(rows, key=lambda x: dict[x["id"]])
df = pd.DataFrame([], columns=["title", "url"])
for idx, result in enumerate(result_list):
df.loc[idx] = {
"title": result["title"],
"url": result["link"],
}
return df