Skip to content

Commit

Permalink
Merge pull request #23 from letuananh/dev
Browse files Browse the repository at this point in the history
Make sure that yawlib works with latest chirptext on PyPI
  • Loading branch information
letuananh authored Jul 19, 2018
2 parents e1fcefd + 5f0b232 commit 9d2d7b9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
6 changes: 0 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
[submodule "modules/puchikarui"]
path = modules/puchikarui
url = https://github.com/letuananh/puchikarui
[submodule "modules/chirptext"]
path = modules/chirptext
url = https://github.com/letuananh/chirptext
1 change: 0 additions & 1 deletion modules/chirptext
Submodule chirptext deleted from 3c42e7
1 change: 0 additions & 1 deletion modules/puchikarui
Submodule puchikarui deleted from cf6546
2 changes: 1 addition & 1 deletion test/test_gwnsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import logging
import unittest
from chirptext import Counter, header
from chirptext.io import CSV
from chirptext.chio import CSV
from chirptext import texttaglib as ttl
from yawlib import YLConfig
from yawlib import WordnetException
Expand Down
2 changes: 1 addition & 1 deletion yawlib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import logging

from chirptext import AppConfig
from chirptext.io import read_file, write_file
from chirptext.chio import read_file, write_file


MY_DIR = os.path.dirname(__file__)
Expand Down
9 changes: 8 additions & 1 deletion yawlib/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ def __init__(self, offset, pos):
self.pos = pos

@staticmethod
def from_string(synsetid):
def from_string(synsetid, **kwargs):
''' Parse a synsetID string to SynsetID object. When failed, return default argument is provided or an exception will be raised '''
if synsetid is None:
if 'default' in kwargs:
return kwargs['default']
raise InvalidSynsetID("synsetid cannot be None")
m = SynsetID.WNSQL_FORMAT.match(str(synsetid))
if m:
Expand All @@ -106,6 +109,8 @@ def from_string(synsetid):
pos = m.group('pos')
return SynsetID(offset, pos)
else:
if 'default' in kwargs:
return kwargs['default']
raise InvalidSynsetID("Invalid synsetid format (provided: {})".format(synsetid))

def to_canonical(self):
Expand All @@ -127,6 +132,8 @@ def __hash__(self):

def __eq__(self, other):
# make sure that the other instance is a SynsetID object
if other and isinstance(other, Synset):
other = other.ID
if other and not isinstance(other, SynsetID):
other = SynsetID.from_string(str(other))
return other is not None and self.offset == other.offset and self.pos == other.pos
Expand Down
41 changes: 23 additions & 18 deletions yawlib/yawol/django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,16 @@
import logging
import django
from django.http import HttpResponse, Http404
from yawlib import YLConfig
from yawlib import SynsetID, SynsetCollection
from yawlib import WordnetSQL as WSQL
from yawlib.omwsql import OMWSQL
from yawlib.helpers import get_omw, get_wn

# ---------------------------------------------------------------------
# CONFIGURATION
# ---------------------------------------------------------------------
logger = logging.getLogger(__name__)
wsql = WSQL(YLConfig.WNSQL30_PATH)
omwsql = OMWSQL(YLConfig.OMW_DB) if os.path.isfile(YLConfig.OMW_DB) else None
wsql = get_wn()
omwsql = get_omw()
print("OMW: {}".format(omwsql))


def jsonp(func):
Expand Down Expand Up @@ -105,8 +104,8 @@ def search(request, query):
Mapping: /yawol/search/<query>
'''
# assume that query is a synset?
try:
sid = SynsetID.from_string(query.strip())
sid = SynsetID.from_string(query.strip(), default=None)
if sid:
ss = wsql.get_synset(sid)
if ss is None and omwsql is not None:
# try to search by OMW
Expand All @@ -115,22 +114,28 @@ def search(request, query):
print("OMW SS", ss)
if ss is not None:
return SynsetCollection().add(ss).to_json()
except:
logger.exception("Cannot find by synsetID")
pass
# try to search by lemma
synsets = wsql.search(lemma=query)
if synsets is not None and len(synsets) > 0:
if synsets:
logger.info("Query: {} - Results: {}".format(query, synsets))
return synsets.to_json()
else:
# try to search by sensekey
try:
ss = wsql.get_by_key(query)
except:
ss = None
if ss:
return SynsetCollection().add(ss).to_json()
if not synsets and omwsql is not None:
print("Try to search {} in OMW".format(query))
synsets = omwsql.search(lemma=query)
if synsets:
logger.info("Query: {} - Results: {}".format(query, synsets))
return synsets.to_json()
else:
logger.warning("Not found {} in OMW".format(query))
else:
# try to search by sensekey
try:
ss = wsql.get_by_key(query)
except:
ss = None
if ss:
return SynsetCollection().add(ss).to_json()
# invalid query
raise Http404('Invalid query')

Expand Down

0 comments on commit 9d2d7b9

Please sign in to comment.