Skip to content

Commit

Permalink
Merge pull request neocl#37 from letuananh/main
Browse files Browse the repository at this point in the history
jamdict 0.1a10 is ready on PyPI
  • Loading branch information
letuananh authored May 21, 2021
2 parents 5754700 + f39c5a7 commit 886b2c2
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 106 deletions.
6 changes: 3 additions & 3 deletions jamdict/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
__url__ = "https://github.com/neocl/jamdict"
__maintainer__ = "Le Tuan Anh"
__version_major__ = "0.1"
__version__ = "{}a10.post2".format(__version_major__)
__version_long__ = "{} - Alpha - Post 002".format(__version_major__)
__status__ = "Prototype"
__version__ = "{}a10".format(__version_major__)
__version_long__ = "{} - Alpha 10".format(__version_major__)
__status__ = "3 - Alpha"
8 changes: 6 additions & 2 deletions jamdict/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,12 @@ def show_info(cli, args):
if not os.path.isdir(jamdict_home):
jamdict_home += " [Missing]"
output.print(f"JAMDICT_HOME : {jamdict_home}")
data_pkg = 'Installed' if jamdict.util._JAMDICT_DATA_AVAILABLE else 'Not installed'
output.print(f"jamdict_data package: {data_pkg}")
if jamdict.util._JAMDICT_DATA_AVAILABLE:
import jamdict_data
data_pkg = f"version {jamdict_data.__version__} [OK]"
else:
data_pkg = "Not installed"
output.print(f"jamdict-data : {data_pkg}")
if args.config:
_config_path = args.config + " [Custom]"
if not os.path.isfile(args.config):
Expand Down
8 changes: 4 additions & 4 deletions jamdict/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,19 @@ def __init__(self, db_file=None, kd2_file=None,
self.jmnedict_xml_file = jmnedict_xml_file if jmnedict_xml_file else config.get_file('JMNEDICT_XML') if auto_config else None

self.db_file = db_file if db_file else config.get_file('JAMDICT_DB') if auto_config else None
if not self.db_file or not os.path.isfile(self.db_file):
if not self.db_file or (self.db_file != ':memory:' and not os.path.isfile(self.db_file)):
if _JAMDICT_DATA_AVAILABLE:
self.db_file = jamdict_data.JAMDICT_DB_PATH
elif self.jmd_xml_file and os.path.isfile(self.jmd_xml_file):
getLogger().warning("JAMDICT_DB could NOT be found. Searching will be extremely slow. Please run `python3 -m jamdict import` first")
self.kd2_file = kd2_file if kd2_file else self.db_file if auto_config else None
if not self.kd2_file or not os.path.isfile(self.kd2_file):
if not self.kd2_file or (self.kd2_file != ':memory:' and not os.path.isfile(self.kd2_file)):
if _JAMDICT_DATA_AVAILABLE:
self.kd2_file = None # jamdict_data.JAMDICT_DB_PATH
elif self.kd2_xml_file and os.path.isfile(self.kd2_xml_file):
getLogger().warning("Kanjidic2 database could NOT be found. Searching will be extremely slow. Please run `python3 -m jamdict import` first")
self.jmnedict_file = jmnedict_file if jmnedict_file else self.db_file if auto_config else None
if not self.jmnedict_file or not os.path.isfile(self.jmnedict_file):
if not self.jmnedict_file or (self.jmnedict_file != ':memory:' and not os.path.isfile(self.jmnedict_file)):
if _JAMDICT_DATA_AVAILABLE:
self.jmnedict_file = None # jamdict_data.JAMDICT_DB_PATH
elif self.jmnedict_xml_file and os.path.isfile(self.jmnedict_xml_file):
Expand Down Expand Up @@ -384,7 +384,7 @@ def has_jmne(self, ctx=None):
''' Check if current database has jmne support '''
if ctx is None:
ctx = self.__make_db_ctx()
m = ctx.meta.select_single('key=?', ('jmnedict.version',))
m = ctx.meta.select_single('key=?', ('jmnedict.version',)) if ctx is not None else None
return m is not None and len(m.value) > 0

def is_available(self):
Expand Down
11 changes: 2 additions & 9 deletions jamdict_demo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
"""
Jamdict demo application
Latest version can be found at https://github.com/neocl/jamdict
Expand All @@ -17,14 +17,7 @@
References:
JMDict website:
http://www.csse.monash.edu.au/~jwb/edict.html
Python documentation:
https://docs.python.org/
PEP 257 - Python Docstring Conventions:
https://www.python.org/dev/peps/pep-0257/
@author: Le Tuan Anh <[email protected]>
@license: MIT
'''
"""

# Copyright (c) 2016, Le Tuan Anh <[email protected]>
#
Expand Down
23 changes: 3 additions & 20 deletions jamdol-flask.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
"""
jamdol - JAMDict OnLine (REST server)
Latest version can be found at https://github.com/neocl/jamdict
References:
Python documentation:
https://docs.python.org/
PEP 257 - Python Docstring Conventions:
https://www.python.org/dev/peps/pep-0257/
@author: Le Tuan Anh <[email protected]>
'''
"""

# Copyright (c) 2017, Le Tuan Anh <[email protected]>
#
Expand All @@ -34,16 +26,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

__author__ = "Le Tuan Anh <[email protected]>"
__copyright__ = "Copyright 2017, jamdict"
__credits__ = []
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "Le Tuan Anh"
__email__ = "<[email protected]>"
__status__ = "Prototype"

########################################################################

import json
import logging
Expand All @@ -55,6 +37,7 @@
from chirptext.cli import setup_logging

from jamdict import Jamdict
from jamdict import __version__

# ---------------------------------------------------------------------
# CONFIGURATION
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def read(*filenames, **kwargs):
"Bug Tracker": "https://github.com/neocl/jamdict/issues",
"Source Code": "https://github.com/neocl/jamdict/"
},
keywords="nlp",
keywords=['dictionary', 'japanese', 'kanji', 'japanese-language', 'jmdict', 'japanese-study', 'kanjidic2', 'japanese-dictionary', 'jamdict'],
license=pkg_info['__license__'],
author=pkg_info['__author__'],
tests_require=requirements,
Expand All @@ -61,8 +61,9 @@ def read(*filenames, **kwargs):
test_suite='test',
# Reference: https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=['Programming Language :: Python',
'Development Status :: 2 - Pre-Alpha',
'Development Status :: {}'.format(pkg_info['__status__']),
'Natural Language :: Japanese',
'Natural Language :: English',
'Environment :: Plugins',
'Intended Audience :: Developers',
'License :: OSI Approved :: {}'.format(pkg_info['__license__']),
Expand Down
111 changes: 45 additions & 66 deletions test/test_jamdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ def test_jamdict_xml(self):
print("Test Jamdict search in XML files")
jam = Jamdict(jmd_xml_file=MINI_JMD, kd2_xml_file=MINI_KD2, auto_config=False)
result = jam.lookup('おみやげ')
self.assertEqual(len(result.entries), 1)
self.assertEqual(len(result.chars), 2)
self.assertEqual(1, len(result.entries))
self.assertEqual(2, len(result.chars))
self.assertEqual({c.literal for c in result.chars}, {'土', '産'})


Expand Down Expand Up @@ -179,70 +179,49 @@ def test_home_dir(self):
os.environ['JAMDICT_HOME'] = _orig_home


class TestJamdictSQLite(unittest.TestCase):

def test_jamdict_sqlite_all(self):
if os.path.isfile(TEST_DB):
os.unlink(TEST_DB)
jam = Jamdict(db_file=TEST_DB, kd2_file=TEST_DB, jmnedict_file=TEST_DB, jmd_xml_file=MINI_JMD, kd2_xml_file=MINI_KD2, jmnedict_xml_file=MINI_JMNE)
# Lookup using XML
result = jam.jmdict_xml.lookup('おみやげ')
getLogger().debug("Results: {}".format(result))
# Lookup using SQLite
jam.import_data()
# test lookup
result = jam.lookup('おみやげ')
self.assertIsNotNone(result.entries)
self.assertEqual(len(result.entries), 1)
self.assertEqual(len(result.chars), 2)
self.assertEqual({c.literal for c in result.chars}, {'土', '産'})

def test_memory_mode(self):
if not os.path.isfile(TEST_DB):
jam = Jamdict(db_file=TEST_DB, kd2_file=TEST_DB, jmnedict_file=TEST_DB, jmd_xml_file=MINI_JMD, kd2_xml_file=MINI_KD2, jmnedict_xml_file=MINI_JMNE)
jam.import_data()
print("Test reading DB into RAM")
ram_jam = Jamdict(TEST_DB, memory_mode=True)
print("1st lookup")
result = ram_jam.lookup('おみやげ')
self.assertIsNotNone(result.entries)
self.assertEqual(len(result.entries), 1)
self.assertEqual(len(result.chars), 2)
self.assertEqual({c.literal for c in result.chars}, {'土', '産'})
print("2nd lookup")
result = ram_jam.lookup('おみやげ')
self.assertIsNotNone(result.entries)
self.assertEqual(len(result.entries), 1)
self.assertEqual(len(result.chars), 2)
self.assertEqual({c.literal for c in result.chars}, {'土', '産'})

def test_real_lookup(self):
# test real lookup
from chirptext.leutile import Timer
t = Timer()
ram_jam = Jamdict(memory_mode=True)
print("1st lookup")
t.start('Load DB into RAM')
result = ram_jam.lookup('おみやげ')
t.stop('Load DB into RAM')
print(t)
self.assertIsNotNone(result.entries)
self.assertEqual(len(result.entries), 1)
self.assertEqual(3, len(result.chars))
print(result.chars)
self.assertEqual({c.literal for c in result.chars}, {'土', '産', '御'})
print("2nd lookup")
result = ram_jam.lookup('おみやげ')
self.assertIsNotNone(result.entries)
self.assertEqual(len(result.entries), 1)
self.assertEqual(3, len(result.chars))
self.assertEqual({c.literal for c in result.chars}, {'土', '産', '御'})
print("3rd lookup")
result = ram_jam.lookup('おみやげ')
self.assertIsNotNone(result.entries)
self.assertEqual(len(result.entries), 1)
self.assertEqual(3, len(result.chars))
self.assertEqual({c.literal for c in result.chars}, {'土', '産', '御'})
class TestJamdictSQLite(unittest.TestCase):

def test_jamdict_sqlite_all(self):
if os.path.isfile(TEST_DB):
os.unlink(TEST_DB)
jam = Jamdict(db_file=TEST_DB, kd2_file=TEST_DB, jmnedict_file=TEST_DB,
jmd_xml_file=MINI_JMD, kd2_xml_file=MINI_KD2, jmnedict_xml_file=MINI_JMNE)
# Lookup using XML
result = jam.jmdict_xml.lookup('おみやげ')
getLogger().debug("Results: {}".format(result))
# Lookup using SQLite
jam.import_data()
# test lookup
result = jam.lookup('おみやげ')
self.assertIsNotNone(result.entries)
self.assertEqual(len(result.entries), 1)
self.assertEqual(len(result.chars), 2)
self.assertEqual({c.literal for c in result.chars}, {'土', '産'})

def test_memory_mode(self):
if not os.path.isfile(TEST_DB):
jam = Jamdict(db_file=TEST_DB, kd2_file=TEST_DB, jmnedict_file=TEST_DB, jmd_xml_file=MINI_JMD, kd2_xml_file=MINI_KD2, jmnedict_xml_file=MINI_JMNE)
jam.import_data()
print("Test reading DB into RAM")
ram_jam = Jamdict(TEST_DB, memory_mode=True)
print("1st lookup")
result = ram_jam.lookup('おみやげ')
self.assertIsNotNone(result.entries)
self.assertEqual(len(result.entries), 1)
self.assertEqual(len(result.chars), 2)
self.assertEqual({c.literal for c in result.chars}, {'土', '産'})
print("2nd lookup")
result = ram_jam.lookup('おみやげ')
self.assertIsNotNone(result.entries)
self.assertEqual(len(result.entries), 1)
self.assertEqual(len(result.chars), 2)
self.assertEqual({c.literal for c in result.chars}, {'土', '産'})
print("3rd lookup")
result = ram_jam.lookup('おみやげ')
self.assertIsNotNone(result.entries)
self.assertEqual(len(result.entries), 1)
self.assertEqual(2, len(result.chars))
self.assertEqual({c.literal for c in result.chars}, {'土', '産'})


########################################################################
Expand Down

0 comments on commit 886b2c2

Please sign in to comment.