Skip to content

Commit

Permalink
modified implementation of interactive so that it can run in the note…
Browse files Browse the repository at this point in the history
…book, too
  • Loading branch information
AidanKelley committed Sep 21, 2020
1 parent 6595470 commit fdee954
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 86 deletions.
83 changes: 2 additions & 81 deletions sosen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,89 +132,10 @@ def describe(**kwargs):
run_describe(**kwargs)


def get_input_with_constraint(message, constraint):
while True:
user_input = input(message)
if constraint(user_input):
return user_input


def get_input_from_choices(message, choices):
def choice_constraint(choice):
return choice in choices

return get_input_with_constraint(message, choice_constraint)

def get_choice(message, choices_dict):
reverse_dict = {
value: key for key, values in choices_dict.items() for value in values
}
choices = [value for value in reverse_dict.keys()]
choice = get_input_from_choices(message, choices)

return reverse_dict[choice]

from .cli import run_interactive
@cli.command(help="run interactively")
def interactive():

search_results = None

while True:
choice = get_choice(
"Choose an action (search/describe/quit):> ",
{
"search": [
"search",
"s"
],
"describe": [
"describe",
"d"
],
"quit": [
"quit",
"q"
]
}
)

if choice == "quit":
return
elif choice == "describe":
print("Enter a space-separated list of URIs")
if search_results is not None:
print("Alternatively, enter numbers 1-20, referring to the results of the previous search")

choice = get_input_with_constraint(">", lambda x: True)

def decode_uri(uri):
try:
assert(search_results is not None)
index = int(uri)
assert(1 <= index <= 20)
return search_results[index-1]
except (ValueError, KeyError, AssertionError):
return uri

uris = [decode_uri(uri) for uri in choice.split(" ")]

run_describe(iris=uris)

elif choice == "search":
method = get_choice("Which method (description/keyword/title)?> ",
{
"description": ["description", "d"],
"keyword": ["keyword", "k"],
"title": ["title", "t"]
}
)

query = get_input_with_constraint("what is your query?> ", lambda x: True)
keywords = query.split(" ")

search_results = run_search(keywords=keywords, method=method)


run_interactive()


if __name__ == "__main__":
Expand Down
91 changes: 86 additions & 5 deletions sosen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def get_all_keywords(keywords):
return [*keywords_using_first, *other_keywords]


def run_search(keywords, method="description"):
def run_search(keywords, method="description", format="github"):
if isinstance(keywords, str):
keywords = keywords.split(" ")

Expand Down Expand Up @@ -303,11 +303,13 @@ def run_search(keywords, method="description"):
print(tabulate(
table_data,
headers=["", "result iri", "matches", "tf-idf sum"],
# tablefmt="github"
tablefmt=format
))

return [result['software']['value'] for result in results]

def run_describe(iris):

def run_describe(iris, format="github"):
graph_in = get_config()["endpoint"]
sparql = SPARQLWrapper(graph_in)

Expand Down Expand Up @@ -355,10 +357,89 @@ def run_describe(iris):

print(tabulate(
table,
headers=["property"] + [f"software {i+1}" for i in range(len(iris))],
tablefmt="github"
headers="firstrow",
tablefmt=format
))

def get_input_with_constraint(message, constraint):
while True:
user_input = input(message)
if constraint(user_input):
return user_input

def get_input_from_choices(message, choices):
def choice_constraint(choice):
return choice in choices

return get_input_with_constraint(message, choice_constraint)

def get_choice(message, choices_dict):
reverse_dict = {
value: key for key, values in choices_dict.items() for value in values
}
choices = [value for value in reverse_dict.keys()]
choice = get_input_from_choices(message, choices)

return reverse_dict[choice]

def run_interactive():
search_results = None

while True:
choice = get_choice(
"Choose an action (search/describe/quit):> ",
{
"search": [
"search",
"s"
],
"describe": [
"describe",
"d"
],
"quit": [
"quit",
"q"
]
}
)

if choice == "quit":
return
elif choice == "describe":
print("Enter a space-separated list of URIs")
if search_results is not None:
print("Alternatively, enter numbers 1-20, referring to the results of the previous search")

choice = get_input_with_constraint(">", lambda x: True)

def decode_uri(uri):
try:
assert (search_results is not None)
index = int(uri)
assert (1 <= index <= 20)
return search_results[index - 1]
except (ValueError, KeyError, AssertionError):
return uri

uris = [decode_uri(uri) for uri in choice.split(" ")]

run_describe(iris=uris)

elif choice == "search":
method = get_choice("Which method (description/keyword/title)?> ",
{
"description": ["description", "d"],
"keyword": ["keyword", "k"],
"title": ["title", "t"]
}
)

query = get_input_with_constraint("what is your query?> ", lambda x: True)
keywords = query.split(" ")

search_results = run_search(keywords=keywords, method=method)




Expand Down

0 comments on commit fdee954

Please sign in to comment.