Skip to content

Commit

Permalink
describe query worked, unabomber hates people
Browse files Browse the repository at this point in the history
  • Loading branch information
interrogator committed Nov 27, 2019
1 parent 765c20f commit 9a93932
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
35 changes: 16 additions & 19 deletions explorer/parts/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dash.exceptions import PreventUpdate

from .helpers import (_cast_query, _get_specs_and_corpus, _translate_relative,
_tuple_or_list, _update_datatable)
_tuple_or_list, _update_datatable, _special_search)
from .main import CORPORA, INITIAL_TABLES, app
from .strings import (_make_search_name, _make_table_name, _search_error,
_table_error)
Expand All @@ -24,10 +24,14 @@ def _correct_placeholder(value, **kwargs):
"""
More accurate placeholder text when doing dependencies
"""
disable_gram = value in {"t", "d", "describe"}
if value in {"describe", "d"}:
return "Enter depgrep query...", disable_gram
return "Enter regular expression search query...", disable_gram
default = "Enter regular expression search query..."
mapped = {
"t": "Enter Tgrep2 query...",
"d": "Enter depgrep query",
"describe": "Enter depgrep query (e.g. l\"man\")"
}
disable_gram = value in mapped
return mapped.get(value, default), disable_gram


@app.expanded_callback(
Expand Down Expand Up @@ -227,22 +231,15 @@ def _new_search(
found_results = True

if not exists:
# the expected callback. run a search and update dataset view and search history
if col == "t":
df = corpus.tgrep(search_string, inverse=skip)
elif col == "d":
try:
df = corpus.depgrep(search_string, inverse=skip)
except Exception as error:
# todo: handle invalid queries properly...
# we need to give hepful message back to user...
print(f"DEPGREP ERROR: {type(error)}: {error}")
# after which, either we return previous, or return none:
df = df.iloc[:0, :0]
# ngram stuff
# todo: more cleanup for this, it's ugly!
# tricky searches
if col in {"t", "d", "describe"}:
df, msg = _special_search(corpus, col, search_string, skip)
# do ngramming stuff
if gram_select:
df = getattr(corpus.near, col)(search_string, distance=gram_select)
else:
# skip/just searches
elif col not in {"t", "d", "describe"}:
search = _cast_query(search_string, col)
method = "just" if not skip else "skip"
try:
Expand Down
17 changes: 17 additions & 0 deletions explorer/parts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,20 @@ def _get_corpora_meta(corpora_file):
return dict()
with open(corpora_file, "r") as fo:
return json.loads(fo.read())


def _special_search(df, col, search_string, skip):
"""
Perform nonstandard search types (tgrep, depgrep, describe)
todo: proper error handling etc
"""
mapped = dict(t="tgrep", d="depgrep", describe="describe")
try:
# note, describe inverse is nonfunctional!
matches = getattr(df, mapped[col])(search_string, inverse=skip)
return matches, None
except Exception as error:
msg = f"search error for {col} ({search_string}): {type(error)}: {error}"
print(msg)
return df.iloc[:0, :0], msg
2 changes: 1 addition & 1 deletion explorer/parts/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _build_dataset_space(df, config):
cols = _get_cols(df, config["add_governor"])
extra = [
("Dependencies", "d"),
("Descriptors", "describe")
("Describe thing", "describe")
]
grams = [
("Match (default)", 0),
Expand Down

0 comments on commit 9a93932

Please sign in to comment.