Skip to content

Commit f1767e6

Browse files
committed
Update core.py
1 parent 8c74498 commit f1767e6

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

pythainlp/corpus/core.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,18 @@ def get_corpus(filename: str) -> frozenset:
8888
return frozenset(lines)
8989

9090

91-
def _update_db(name, path):
92-
local_db = TinyDB(corpus_db_path())
93-
query = Query()
94-
local_db.update({"filename": path}, query.name == name)
95-
local_db.close()
96-
9791
def _update_all():
9892
print("Update Corpus...")
99-
local_db = TinyDB(corpus_db_path())
100-
item_all = local_db.all()
93+
with TinyDB(corpus_db_path()) as local_db:
94+
item_all = local_db.all()
95+
query = Query()
96+
for item in item_all:
97+
name = item['name']
98+
if 'file_name' in item.keys():
99+
local_db.update({"filename": item['file_name']}, query.name == name)
100+
elif 'file' in item.keys():
101+
local_db.update({"filename": item['file']}, query.name == name)
101102
local_db.close()
102-
for item in item_all:
103-
name = item['name']
104-
if 'file_name' in item.keys():
105-
_update_db(name, item['file_name'])
106-
elif 'file' in item.keys():
107-
_update_db(name, item['file'])
108103

109104
def get_corpus_path(name: str) -> Union[str, None]:
110105
"""
@@ -143,9 +138,10 @@ def get_corpus_path(name: str) -> Union[str, None]:
143138
"""
144139
# check if the corpus is in local catalog, download if not
145140
corpus_db_detail = get_corpus_db_detail(name)
146-
if (corpus_db_detail.get("file_name") is not None or corpus_db_detail.get("file") is not None) and corpus_db_detail.get("filename") is None:
141+
if corpus_db_detail.get("file_name") is not None and corpus_db_detail.get("filename") is None:
142+
_update_all()
143+
elif corpus_db_detail.get("file") is not None and corpus_db_detail.get("filename") is None:
147144
_update_all()
148-
149145

150146
if not corpus_db_detail or not corpus_db_detail.get("filename"):
151147
download(name)

0 commit comments

Comments
 (0)