-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsh_code_utils.py
More file actions
224 lines (198 loc) · 8.37 KB
/
sh_code_utils.py
File metadata and controls
224 lines (198 loc) · 8.37 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
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
from SPARQLWrapper import SPARQLWrapper, JSON
from urllib.parse import urlparse
from bs4 import BeautifulSoup, SoupStrainer
import urllib
from urllib.request import urlopen
from urllib.parse import quote
import json
import re
import importlib
import llms
import globals
importlib.reload(globals)
def load_json_data(file_name):
try:
with open(file_name, 'r') as json_file:
data = json.load(json_file)
return data['data']
except FileNotFoundError:
print(f"Error: File '{file_name}' not found.")
return []
def write_to_json(result, out_file_path):
with open(out_file_path, "w", encoding="utf-8") as f:
json.dump({"data": result}, f, indent=4)
print("Successfully written to file!")
def query_sparql_endpoint(endpoint_url, sparql_query, search_key, flag=False):
sparql = SPARQLWrapper(endpoint_url)
if flag:
sparql.setQuery(sparql_query % (search_key, search_key, search_key.strip("<>").strip()))
else:
sparql.setQuery(sparql_query % search_key)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
return_result = []
for result in results["results"]["bindings"]:
converted_result = {}
for key, value_info in result.items():
value = value_info.get('value')
if value:
converted_result[key] = value
return_result.append(converted_result)
return return_result
def run_sparql_query(sparql_endpoint, sparql_query, param='', flag=False):
if flag:
sparql_query = sparql_query % param
try:
sparql = SPARQLWrapper(sparql_endpoint)
sparql.setQuery(sparql_query)
sparql.setReturnFormat(JSON)
query_results = sparql.query().convert()
search_result = []
if query_results:
for result in query_results["results"]["bindings"]:
temp = {}
for key, value_info in result.items():
temp[key] = value_info.get('value')
search_result.append(temp)
return search_result
except Exception as e:
print(f"An error occurred: {str(e)}")
return None
def get_examples(key, file_path="./examples.json"):
with open(file_path, 'r', encoding='utf-8') as file:
examples = json.load(file)
return examples.get(key, None)
def identify_title(question):
example = get_examples("identify_title")
prompt = f"""Task: Your task is extracting a publication title from the given phrase.
Example
{example}
Do not add anything else.
Please provide your result in JSON format only. Please do not include the Example in your response.
phrase: {question}
"""
title = llms.chatgpt(prompt, 2)
if 'title' in title:
if title['title']:
return title['title'][0]
else:
return ''
def entity_linking(entity='', flag=True):
sparql_end_point = "https://dblp-april24.skynet.coypu.org/sparql"
if flag:
if entity == '':
return None
entity = entity.rstrip("'")
entity = entity.lstrip("'")
entity = entity.rstrip('"')
entity = entity.lstrip('"')
if not entity.endswith('.'):
entity += '.'
query = """PREFIX dblp: <https://dblp.org/rdf/schema#>
SELECT *
WHERE {
?paper dblp:title "%s" .
?author ^dblp:authoredBy ?paper ;
dblp:primaryCreatorName ?primarycreatorname ;
dblp:orcid ?orcid ;
dblp:wikipedia ?wikipedia .
FILTER (CONTAINS(STR(?wikipedia), "en.wikipedia.org"))
}"""
else:
entity = entity.strip("<>")
entity = f"<{entity}>"
query = """PREFIX dblp: <https://dblp.org/rdf/schema#>
SELECT *
WHERE {
%s dblp:primaryCreatorName ?primarycreatorname ;
dblp:orcid ?orcid ;
dblp:wikipedia ?wikipedia .
FILTER (CONTAINS(STR(?wikipedia), "en.wikipedia.org"))
}"""
sparql_result = run_sparql_query(sparql_end_point, query, entity, True)
# print(search_result)
return sparql_result
def search_author(author_dblp_uri):
result = entity_linking(f"<{author_dblp_uri}>", False)
if result:
for r in result:
if 'orcid' in r:
globals.global_orcid = r['orcid']
globals.global_author_wiki_uri = r['wikipedia']
return r['primarycreatorname'], {"orcid": r['orcid'], "author_wikipedia": r['wikipedia']}
def resolve_author(question, author_dblp_uri, q_type='bridge'):
title = identify_title(question)
if title == '':
return '', {}
entity = entity_linking(title)
if q_type == 'bridge':
if entity:
for entity_item in entity:
if 'orcid' in entity_item:
if urlparse(entity_item['author']) == urlparse(author_dblp_uri):
globals.global_orcid = entity_item['orcid']
globals.global_author_wiki_uri = entity_item['wikipedia']
return entity_item['primarycreatorname'], {"orcid": entity_item['orcid'], "author_wikipedia": entity_item['wikipedia']}
return search_author(author_dblp_uri)
else:
author_uris = [value.strip('<>') for item in author_dblp_uri for value in item.values()]
if entity:
for entity_item in entity:
if 'orcid' in entity_item:
if entity_item['author'] in author_uris:
if entity_item['author'] not in globals.global_visited_author_uri:
globals.global_visited_author_uri.append(entity_item['author'])
return entity_item['primarycreatorname'], {"orcid": entity_item['orcid'],
"author_wikipedia": entity_item['wikipedia']}
if len(author_uris) > 1:
if author_uris[0] not in globals.global_visited_author_uri:
globals.global_visited_author_uri.append(author_uris[0])
return search_author(author_uris[0])
if author_uris[1] not in globals.global_visited_author_uri:
globals.global_visited_author_uri.append(author_uris[1])
return search_author(author_uris[1])
def search_semoa_author(author_dblp_orcid):
sparql_endpoint = "http://localhost:3030/sopena/sparql"
orcid_query = """PREFIX ns2: <https://semopenalex.org/ontology/>
PREFIX ns3: <http://purl.org/spar/bido/>
PREFIX ns4: <https://dbpedia.org/ontology/>
PREFIX ns5: <https://dbpedia.org/property/>
SELECT * WHERE
{
GRAPH <https://semopenalex.org/authors/context> {
{
OPTIONAL {?author_uri ns2:orcidId ?orcid . }
OPTIONAL {?author_uri ns3:orcidId ?orcid . }
OPTIONAL { ?author_uri ns4:orcidId ?orcid . }
OPTIONAL { ?author_uri ns5:orcidId ?orcid . }
FILTER (?orcid = "%s")
}
}
}
"""
orcid_query_result = query_sparql_endpoint(sparql_endpoint, orcid_query, author_dblp_orcid)
if orcid_query_result:
author_semoa_uri = orcid_query_result[0]['author_uri']
return author_semoa_uri
else:
return None
def extract_text_from_wikipedia(url):
try:
decoded_url = urllib.parse.unquote(url)
wikipedia_url = decoded_url.replace(" ", "_")
encoded_url = quote(wikipedia_url, safe=':/')
source = urlopen(encoded_url).read()
soup = BeautifulSoup(source, 'lxml')
title = soup.title.string.strip()
title = title.rstrip('- Wikipedia')
text = ''
for paragraph in soup.find(id="bodyContent").find_all('p'):
text += paragraph.text
pattern = r'\[\d+\]'
cleaned_wikipedia_text = re.sub(pattern, '', text)
cleaned_wikipedia_text = cleaned_wikipedia_text.strip()
return cleaned_wikipedia_text
except Exception as e:
print(f"General Exception: {e}")
print(f"Failed to retrieve: {url}")
return None