Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__pycache__/
env/
dist/
.DS_Store
*.un~
vocabsieve.egg-info/
build/
Expand All @@ -12,5 +13,8 @@ htmlcov
docs/_site
docs/.jekyll-cache
.vscode
testdata
testdir
testdir

testdata/kaikki/*
!kaikki.org-dictionary-English-by-pos-postp.jsonl
!kaikki.org-dictionary-English-by-pos-postp.jsonl.gz

Large diffs are not rendered by default.

Binary file not shown.
20 changes: 20 additions & 0 deletions tests/test_dictformats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from vocabsieve.dictformats import dictinfo


def test_dictinfo_kaikki_jsonl():
info = dictinfo("testdata/kaikki/kaikki.org-dictionary-English-by-pos-postp.jsonl")

assert info == {
"basename": "kaikki.org-dictionary-English-by-pos-postp",
"path": "testdata/kaikki/kaikki.org-dictionary-English-by-pos-postp.jsonl",
"type": "wiktdump",
}

def test_dictinfo_kaikki_jsonl_gz():
info = dictinfo("testdata/kaikki/kaikki.org-dictionary-English-by-pos-postp.jsonl.gz")

assert info == {
"basename": "kaikki.org-dictionary-English-by-pos-postp",
"path": "testdata/kaikki/kaikki.org-dictionary-English-by-pos-postp.jsonl.gz",
"type": "wiktdump",
}
26 changes: 21 additions & 5 deletions vocabsieve/dictformats.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,26 @@ def dictinfo(path) -> dict[str, str]:
return {"type": "audiolib", "basename": basename, "path": path}
if ext not in supported_dict_extensions:
raise NotImplementedError("Unsupported format")
if ext in ('.json', '.jsonl', '.xz', '.bz2', '.gz'):

for jsonl_ext in (".jsonl", ".jsonl.xz", ".jsonl.gz", ".jsonl.bz2"):
if not path.endswith(jsonl_ext):
continue

with zopen(path) as f:
first_line = f.readline()
try:
if json.loads(first_line):
logger.debug("Detected Kaikki wiktionary dump")
return {
"type": "wiktdump",
"basename": os.path.basename(path.removesuffix(jsonl_ext)),
"path": path
}
except json.JSONDecodeError:
pass
raise NotImplementedError(f"File {path} is not a supported jsonl format")

if ext in ('.json', '.xz', '.bz2', '.gz'):
with zopen(path) as f:
try:
d = json.load(f)
Expand All @@ -108,10 +127,7 @@ def dictinfo(path) -> dict[str, str]:
f.seek(0)
first_line = f.readline()
logger.debug("First line of bad json file: ", first_line)
logger.debug("Detected Kaikki wiktionary dump")
if json.loads(first_line):
return {"type": "wiktdump", "basename": basename, "path": path}
raise NotImplementedError(f"File {path} is not a supported json format")
raise NotImplementedError(f"File {path} is not a supported json format")
elif ext == ".ifo":
return {"type": "stardict", "basename": basename, "path": path}
elif ext == ".mdx":
Expand Down