Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synonyms in norm search result #1194

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ config.py
# Standard locations of data and server temporary files
data/
work/

.idea/
3 changes: 3 additions & 0 deletions config_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
# Unauthorised users can create tutorials (but not edit without a login)
TUTORIALS = False

### NORMALIZATION
SHOW_SYNONYMS = False

### LOG_LEVEL
# If you are a developer you may want to turn on extensive server
# logging by enabling LOG_LEVEL = LL_DEBUG
Expand Down
50 changes: 40 additions & 10 deletions server/src/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@

REPORT_LOOKUP_TIMINGS = False

try:
from config import SHOW_SYNONYMS
except ImportError:
# unlimited
SHOW_SYNONYMS = False

# debugging
def _check_DB_version(database):
# TODO; not implemented yet for new-style SQL DBs.
Expand Down Expand Up @@ -204,17 +210,12 @@ def _format_datas(datas, scores=None, matched=None):
unique_labels.sort(lambda a,b: cmp(a[0],b[0]))
unique_labels = [a[1] for a in unique_labels]

# ID is first field, and datatype is "string" for all labels
header = [(label, "string") for label in ["ID"] + unique_labels]

if DISPLAY_SEARCH_SCORES:
header += [("score", "int")]

# construct items, sorted by score first, ID second (latter for stability)
sorted_keys = sorted(datas.keys(), lambda a,b: cmp((scores.get(b,0),b),
(scores.get(a,0),a)))

items = []
extra_labels = {}
for key in sorted_keys:
# make dict for lookup. In case of duplicates (e.g. multiple
# "synonym" entries), prefer ones that were matched.
Expand All @@ -225,19 +226,40 @@ def _format_datas(datas, scores=None, matched=None):
if label not in data_dict or (value in matched and
data_dict[label] not in matched):
data_dict[label] = value
else:
if SHOW_SYNONYMS:
syn_label = "%s Synonyms" % label
data_dict.setdefault(syn_label,[]).append(value)
extra_labels[syn_label] = True
# construct item
item = [str(key)]
for label in unique_labels:
if label in data_dict:
item.append(data_dict[label])
value = data_dict[label]
item.append(value)
else:
item.append('')

if SHOW_SYNONYMS:
for label in extra_labels:
if label in data_dict:
value = data_dict[label]
if isinstance(value, list):
value = " | ".join(value)
item.append(value)
else:
item.append('')

if DISPLAY_SEARCH_SCORES:
item += [str(scores.get(key))]

items.append(item)

# ID is first field, and datatype is "string" for all labels
header = [(label, "string") for label in ["ID"] + unique_labels + [ k for k in extra_labels ] ]

if DISPLAY_SEARCH_SCORES:
header += [("score", "int")]

return header, items

def _norm_filter_score(score, best_score=MAX_SCORE):
Expand Down Expand Up @@ -491,19 +513,23 @@ def _test():
delta = datetime.now() - start
found = False
found_rank = -1
has_synonym = False
for rank, item in enumerate(results['items']):
id_ = item[0]
if id_ == target:
found = True
found_rank = rank+1
if len(item) >= 2:
has_synonym = True
break
strdelta = str(delta).replace('0:00:0','').replace('0:00:','')
print "%s: '%s' <- '%s' rank %d/%d (%s sec)" % (' ok' if found
print "%s: '%s' <- '%s' rank %d/%d (%s sec) %s" % (' ok' if found
else 'MISS',
target, query,
found_rank,
len(results['items']),
strdelta)
strdelta,
' has Synonym(s)' if has_synonym else '')
query_count += 1
if found:
hit_count += 1
Expand All @@ -530,5 +556,9 @@ def _profile_test():
cProfile.run('_test()', 'norm.profile')

if __name__ == '__main__':
SHOW_SYNONYMS=False
_test() # normal
print ' ### SWITCH SYNONYMS ON ### '
SHOW_SYNONYMS=True
_test()
#_profile_test() # profiled