Skip to content

Commit

Permalink
Auto complete pour AllowedMorph sur valeurs comme sur label (#41)
Browse files Browse the repository at this point in the history
* Tentative fix for #40 : Adding the ability to search by label instead or readable on Morph

* Added test to cover use of autocomplete on value as well as label in allowedMorph
  • Loading branch information
PonteIneptique authored and Jean-Baptiste-Camps committed Feb 23, 2018
1 parent a5a53fe commit a944b4b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
58 changes: 34 additions & 24 deletions app/models/linguistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,67 +351,77 @@ def get_like(corpus_id, form, group_by, type_like="lemma", allowed_list=False):
:return: BaseQuery
"""
normalised = unidecode.unidecode(form)
split = False
retrieve_fields = []
if allowed_list is False:
if type_like == "POS":
cls = WordToken
type_like = WordToken.POS
retrieve_field = WordToken.POS,
query_fields = [WordToken.POS]
retrieve_fields = [WordToken.POS]
elif type_like == "morph":
cls = WordToken
type_like = WordToken.morph
retrieve_field = WordToken.morph,
query_fields = [WordToken.morph]
retrieve_fields = [WordToken.morph]
else:
cls = WordToken
# If the normalisation is the same as the original form, we look in normalised label
if normalised == form:
type_like = WordToken.label_uniform
query_fields = [WordToken.label_uniform]
# If there is accents however, we look into original accentued value
else:
type_like = WordToken.lemma
retrieve_field = WordToken.lemma,
query_fields = [WordToken.lemma]
retrieve_fields = [WordToken.lemma]
else:
if type_like == "POS":
cls = AllowedPOS
type_like = AllowedPOS.label
retrieve_field = AllowedPOS.label,
query_fields = [AllowedPOS.label]
retrieve_fields = [AllowedPOS.label]
elif type_like == "morph":
cls = AllowedMorph
type_like = [AllowedMorph.readable]
retrieve_field = (AllowedMorph.label, AllowedMorph.readable)
split = True
query_fields = [AllowedMorph.readable, AllowedMorph.label]
retrieve_fields = [AllowedMorph.label, AllowedMorph.readable]
else:
cls = AllowedLemma
if normalised == form:
type_like = AllowedLemma.label_uniform
query_fields = [AllowedLemma.label_uniform]
# If there is accents however, we look into original accentued value
else:
type_like = AllowedLemma.label
retrieve_field = AllowedLemma.label,
query_fields = [AllowedLemma.label]
retrieve_fields = [AllowedLemma.label]

query = cls.query.with_entities(*retrieve_fields)

query = cls.query.with_entities(*retrieve_field)
if form is None:
query = query.filter(
db.and_(
cls.corpus == corpus_id
db.and_(
cls.corpus == corpus_id
)
)
)
elif isinstance(type_like, list):
elif split:
form = form.split()
query = query.filter(
db.and_(
cls.corpus == corpus_id,
*[type_like[0].ilike("%{}%".format(fsplitted)) for fsplitted in form.split()]
db.or_(*[
query_field.ilike("%{}%".format(fsplitted))
for fsplitted in form
for query_field in query_fields
])
)
)
else:
query = query.filter(
db.and_(
cls.corpus == corpus_id,
type_like.ilike("{}%".format(form))
*[
query_field.ilike("{}%".format(form))
for query_field in query_fields
]
)
)
if group_by is True:
if isinstance(type_like, list):
return query.group_by(type_like[0])
return query.group_by(type_like)
return query.group_by(retrieve_fields[0])
return query

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/test_selenium/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ def edith_nth_row_value(
td.click(), td.clear(), td.send_keys(value)

if autocomplete_selector is not None:
time.sleep(1.5)
time.sleep(0.5)
self.driver.find_element_by_css_selector(autocomplete_selector).click()
time.sleep(0.5)
# Save
row.find_element_by_class_name("save").click()
# It's safer to wait for the AJAX call to be completed
time.sleep(1)
time.sleep(0.5)

return self.db.session.query(WordToken).get(int(id_row)), \
self.driver.find_element_by_id("token_" + id_row + "_row").\
Expand Down
12 changes: 11 additions & 1 deletion tests/test_selenium/test_token_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,21 @@ def test_edit_morph_with_allowed(self):
"masc sing", id_row="3", corpus_id="1", value_type="morph",
autocomplete_selector=".autocomplete-suggestion[data-val='NOMB.=s|GENRE=m|CAS=n']"
)
self.assertEqual(token.lemma, "martin", "Lemma should have been changed")
self.assertEqual(token.lemma, "martin", "Lemma should not have been changed")
self.assertEqual(token.POS, "NOMpro", "POS should not have been changed")
self.assertEqual(token.morph, "NOMB.=s|GENRE=m|CAS=n", "Morph should not have been changed")
self.assertEqual(status_text, "(Saved) Save")

# With auto complete based on value and not label
token, status_text, row = self.edith_nth_row_value(
"NOMB.=s GENRE=m", id_row="4", corpus_id="1", value_type="morph",
autocomplete_selector=".autocomplete-suggestion[data-val='NOMB.=s|GENRE=m|CAS=n']"
)
self.assertEqual(token.lemma, "mout", "Lemma should not have been changed")
self.assertEqual(token.POS, "ADVgen", "POS should not have been changed")
self.assertEqual(token.morph, "NOMB.=s|GENRE=m|CAS=n", "Morph should not have been changed")
self.assertEqual(status_text, "(Saved) Save")


class TestTokensEditFloovant(TokenEditBase):
CORPUS = "floovant"
Expand Down

0 comments on commit a944b4b

Please sign in to comment.