Skip to content

Commit

Permalink
Add IPA to wiktionary
Browse files Browse the repository at this point in the history
  • Loading branch information
1over137 committed Mar 23, 2024
1 parent c628283 commit 69ff4c0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
10 changes: 9 additions & 1 deletion vocabsieve/dictformats.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,16 @@ def kaikki_line_to_textdef(row: dict) -> str:
res = ""
if row.get("pos"):
res += f"<i>{row['pos'].capitalize()}</i>"
res += "\n<strong>"
if row.get("head_templates"):
res += f"\n{row['head_templates'][-1]['expansion']}"
res += f"{row['head_templates'][-1]['expansion']}"
res += "</strong>\n"
if row.get("sounds"):
for item in row['sounds']:
if item.get('ipa'):
res += f" {item['ipa']} "
if item.get('tags'):
res += f" [{','.join(item['tags'])}]"
count = 1
if row.get("senses"):
for item in row['senses']:
Expand Down
4 changes: 2 additions & 2 deletions vocabsieve/sources/wiktionary_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _lookup(self, word: str) -> LookupResult:
f"https://kaikki.org/dictionary/{language_longname}/meaning/{word[0]}/{word[0:2]}/{word}.json")
except Exception as e:
logger.error(f"Failed to get data from Wiktionary: {repr(e)}")
return LookupResult(error=str(e))
return LookupResult(error=repr(e))
if res.status_code != 200:
return LookupResult(error=str(res.text))
datalines = res.content.decode('utf-8').splitlines()
Expand All @@ -33,6 +33,6 @@ def _lookup(self, word: str) -> LookupResult:
data = json.loads(line)
items.append(kaikki_line_to_textdef(data))
if items:
return LookupResult(definition="\n\n".join(items))
return LookupResult(definition="<br>\n".join(items))
else:
return LookupResult(error="No definitions found")
7 changes: 5 additions & 2 deletions vocabsieve/ui/multi_definition_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,11 @@ def populateDefinitions(self):
self.definitions.sort(key=lambda defi: index_map[defi.source])
# filter out error definitions
self.definitions = [defi for defi in self.definitions if defi.definition is not None]
if not any(defi.definition for defi in self.definitions) and self.word_widget:
self.word_widget.setText(self.current_target)
if not any(defi.definition for defi in self.definitions):
if self.word_widget:
self.word_widget.setText(self.current_target)
self.setPlaceholderText("No definitions found for \"" + self.current_target
+ "\". You can still type in a definition manually to add to Anki.")
self.currentIndex = 0
self.updateIndex()

Expand Down

0 comments on commit 69ff4c0

Please sign in to comment.