99import datetime
1010import time
1111import uuid
12+ import yaml
1213
1314def save_to_sqlite (result_df , db_path , table_name ):
1415 result_df = result_df .astype (str )
@@ -34,7 +35,7 @@ def save_to_sqlite(result_df, db_path, table_name):
3435 finally :
3536 conn .close ()
3637
37- def initialize_database (db_path = 'reference_checked.db' ):
38+ def initialize_database (db_path ):
3839 conn = sqlite3 .connect (db_path )
3940 cursor = conn .cursor ()
4041 cursor .execute ("""
@@ -77,6 +78,7 @@ def initialize_database(db_path='reference_checked.db'):
7778 CREATE TABLE IF NOT EXISTS aggregated_results (
7879 id INTEGER PRIMARY KEY AUTOINCREMENT,
7980 triple TEXT,
81+ property_id TEXT,
8082 url TEXT,
8183 Results TEXT,
8284 qid TEXT,
@@ -114,11 +116,21 @@ def initialize_database(db_path='reference_checked.db'):
114116def get_random_qids (num_qids = 5 , max_retries = 3 , delay = 5 ):
115117 sparql = SPARQLWrapper ("https://query.wikidata.org/sparql" )
116118 sparql .setQuery ("""
117- SELECT ?item
118- WHERE {
119- ?item wdt:P31 wd:Q5 . # Instance of human
120- }
121- LIMIT 1000 # Fetch more items than needed
119+ SELECT ?item {
120+ SERVICE bd:sample {
121+ ?item wikibase:sitelinks [].
122+ bd:serviceParam bd:sample.limit "100".
123+ }
124+ MINUS {?item wdt:P31/wdt:P279* wd:Q4167836.}
125+ MINUS {?item wdt:P31/wdt:P279* wd:Q4167410.}
126+ MINUS {?item wdt:P31 wd:Q13406463.}
127+ MINUS {?item wdt:P31/wdt:P279* wd:Q11266439.}
128+ MINUS {?item wdt:P31 wd:Q17633526.}
129+ MINUS {?item wdt:P31 wd:Q13442814.}
130+ MINUS {?item wdt:P3083 [].}
131+ MINUS {?item wdt:P1566 [].}
132+ MINUS {?item wdt:P442 [].}
133+ }
122134 """ )
123135 sparql .setReturnFormat (JSON )
124136
@@ -237,8 +249,15 @@ def prove_process(db_path, batch_qids, algo_version):
237249 if 'conn' in locals ():
238250 conn .close ()
239251
240- def main (db_path , batch_qids , algo_version , Test_mode ):
241- reset_database = False # Developer mode to test, it initialize db for getting clean db
252+ def load_config (config_path : str ):
253+ with open (config_path , 'r' ) as file :
254+ return yaml .safe_load (file )
255+
256+
257+ def main (batch_qids , algo_version ):
258+ reset_database = True # Developer mode to test, it initialize db for getting clean db
259+ config = load_config ('config.yaml' )
260+ db_path = config ['database' ]['result_db_for_API' ]
242261 if reset_database and os .path .exists (db_path ):
243262 os .remove (db_path )
244263 print (f"Database file { db_path } has been deleted." )
@@ -254,12 +273,8 @@ def main(db_path, batch_qids, algo_version, Test_mode):
254273 time .sleep (30 )
255274
256275
257-
258-
259276if __name__ == "__main__" :
260- db_path = 'reference_checked.db'
261- batch_qids = 3
262- algo_version = '1.0.2'
263- Test_mode = True #using different temp .db to test code.
264- main (db_path , batch_qids , algo_version , Test_mode )
277+ batch_qids = 5
278+ algo_version = '1.0.3'
279+ main (batch_qids , algo_version )
265280 # nohup python3 eventHandler.py > output.log 2>&1 &
0 commit comments