Skip to content

Commit

Permalink
fixed cache file read
Browse files Browse the repository at this point in the history
  • Loading branch information
eriktks committed Oct 10, 2024
1 parent 8732fd7 commit 2bb5078
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions orangecontrib/storynavigation/modules/settinganalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ class SettingAnalyzer:
ENTITY_GROUPS = [DATE_LABELS, EVENT_LABELS, LOCATION_LABELS]
ENTITY_LABELS = DATE_LABELS + EVENT_LABELS + LOCATION_LABELS
ENTITY_CACHE_FILE_NAME = "orange_story_navigator_wikidata_entity_cache.json"
LINUX_TMP_DIR = "/tmp"
WINDOWS_TMP_DIR = os.path.join(str(Path.home()), "AppData/Local/Temp")
ENTITY_CACHE_FILE = os.path.join(LINUX_TMP_DIR, ENTITY_CACHE_FILE_NAME) if os.path.isdir(LINUX_TMP_DIR) else (os.path.join(WINDOWS_TMP_DIR, ENTITY_CACHE_FILE_NAME) if os.path.isdir(WINDOWS_TMP_DIR) else None)


def __init__(self, language, n_segments, text_tuples, story_elements, user_defined_entities, callback=None):
Expand Down Expand Up @@ -238,13 +235,13 @@ def __select_best_entities(self, entity_data):


def __get_wikidata_info(self, entity_name, find_property=False):
if self.ENTITY_CACHE_FILE:
with open(self.ENTITY_CACHE_FILE, "r") as cache_file:
if self.ENTITY_CACHE_FILE_NAME and os.path.isfile(self.ENTITY_CACHE_FILE_NAME):
with open(self.ENTITY_CACHE_FILE_NAME, "r") as cache_file:
cache = json.load(cache_file)
cache_file.close()
else:
cache = {}
if entity_name in cache:
if entity_name in cache and len(cache[entity_name]) > 0:
return cache[entity_name]
print("__get_wikidata_info: looking up", entity_name)
url = f"https://www.wikidata.org/w/api.php"
Expand All @@ -258,15 +255,22 @@ def __get_wikidata_info(self, entity_name, find_property=False):
}
if find_property:
params["type"] = "property"
response = requests.get(url, params=params)
time.sleep(1)
data = response.json()
if 'search' in data.keys():
try:
response = requests.get(url, params=params)
time.sleep(1)
data = response.json()
except:
print("lookup failure: no network connection?")
data = {}
if 'search' in data.keys() and len(data["search"]) > 0:
cache[entity_name] = data["search"]
else:
cache[entity_name] = []
if self.ENTITY_CACHE_FILE:
with open(self.ENTITY_CACHE_FILE, "w") as cache_file:
json.dump(cache, cache_file)
cache_file.close()
cache[entity_name] = [{"not found": True}]
if self.ENTITY_CACHE_FILE_NAME:
try:
with open(self.ENTITY_CACHE_FILE_NAME, "w") as cache_file:
json.dump(cache, cache_file)
cache_file.close()
except:
pass
return cache[entity_name]

0 comments on commit 2bb5078

Please sign in to comment.