Skip to content

Commit fdee954

Browse files
committed
modified implementation of interactive so that it can run in the notebook, too
1 parent 6595470 commit fdee954

File tree

2 files changed

+88
-86
lines changed

2 files changed

+88
-86
lines changed

sosen/__main__.py

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -132,89 +132,10 @@ def describe(**kwargs):
132132
run_describe(**kwargs)
133133

134134

135-
def get_input_with_constraint(message, constraint):
136-
while True:
137-
user_input = input(message)
138-
if constraint(user_input):
139-
return user_input
140-
141-
142-
def get_input_from_choices(message, choices):
143-
def choice_constraint(choice):
144-
return choice in choices
145-
146-
return get_input_with_constraint(message, choice_constraint)
147-
148-
def get_choice(message, choices_dict):
149-
reverse_dict = {
150-
value: key for key, values in choices_dict.items() for value in values
151-
}
152-
choices = [value for value in reverse_dict.keys()]
153-
choice = get_input_from_choices(message, choices)
154-
155-
return reverse_dict[choice]
156-
135+
from .cli import run_interactive
157136
@cli.command(help="run interactively")
158137
def interactive():
159-
160-
search_results = None
161-
162-
while True:
163-
choice = get_choice(
164-
"Choose an action (search/describe/quit):> ",
165-
{
166-
"search": [
167-
"search",
168-
"s"
169-
],
170-
"describe": [
171-
"describe",
172-
"d"
173-
],
174-
"quit": [
175-
"quit",
176-
"q"
177-
]
178-
}
179-
)
180-
181-
if choice == "quit":
182-
return
183-
elif choice == "describe":
184-
print("Enter a space-separated list of URIs")
185-
if search_results is not None:
186-
print("Alternatively, enter numbers 1-20, referring to the results of the previous search")
187-
188-
choice = get_input_with_constraint(">", lambda x: True)
189-
190-
def decode_uri(uri):
191-
try:
192-
assert(search_results is not None)
193-
index = int(uri)
194-
assert(1 <= index <= 20)
195-
return search_results[index-1]
196-
except (ValueError, KeyError, AssertionError):
197-
return uri
198-
199-
uris = [decode_uri(uri) for uri in choice.split(" ")]
200-
201-
run_describe(iris=uris)
202-
203-
elif choice == "search":
204-
method = get_choice("Which method (description/keyword/title)?> ",
205-
{
206-
"description": ["description", "d"],
207-
"keyword": ["keyword", "k"],
208-
"title": ["title", "t"]
209-
}
210-
)
211-
212-
query = get_input_with_constraint("what is your query?> ", lambda x: True)
213-
keywords = query.split(" ")
214-
215-
search_results = run_search(keywords=keywords, method=method)
216-
217-
138+
run_interactive()
218139

219140

220141
if __name__ == "__main__":

sosen/cli.py

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def get_all_keywords(keywords):
275275
return [*keywords_using_first, *other_keywords]
276276

277277

278-
def run_search(keywords, method="description"):
278+
def run_search(keywords, method="description", format="github"):
279279
if isinstance(keywords, str):
280280
keywords = keywords.split(" ")
281281

@@ -303,11 +303,13 @@ def run_search(keywords, method="description"):
303303
print(tabulate(
304304
table_data,
305305
headers=["", "result iri", "matches", "tf-idf sum"],
306-
# tablefmt="github"
306+
tablefmt=format
307307
))
308308

309+
return [result['software']['value'] for result in results]
309310

310-
def run_describe(iris):
311+
312+
def run_describe(iris, format="github"):
311313
graph_in = get_config()["endpoint"]
312314
sparql = SPARQLWrapper(graph_in)
313315

@@ -355,10 +357,89 @@ def run_describe(iris):
355357

356358
print(tabulate(
357359
table,
358-
headers=["property"] + [f"software {i+1}" for i in range(len(iris))],
359-
tablefmt="github"
360+
headers="firstrow",
361+
tablefmt=format
360362
))
361363

364+
def get_input_with_constraint(message, constraint):
365+
while True:
366+
user_input = input(message)
367+
if constraint(user_input):
368+
return user_input
369+
370+
def get_input_from_choices(message, choices):
371+
def choice_constraint(choice):
372+
return choice in choices
373+
374+
return get_input_with_constraint(message, choice_constraint)
375+
376+
def get_choice(message, choices_dict):
377+
reverse_dict = {
378+
value: key for key, values in choices_dict.items() for value in values
379+
}
380+
choices = [value for value in reverse_dict.keys()]
381+
choice = get_input_from_choices(message, choices)
382+
383+
return reverse_dict[choice]
384+
385+
def run_interactive():
386+
search_results = None
387+
388+
while True:
389+
choice = get_choice(
390+
"Choose an action (search/describe/quit):> ",
391+
{
392+
"search": [
393+
"search",
394+
"s"
395+
],
396+
"describe": [
397+
"describe",
398+
"d"
399+
],
400+
"quit": [
401+
"quit",
402+
"q"
403+
]
404+
}
405+
)
406+
407+
if choice == "quit":
408+
return
409+
elif choice == "describe":
410+
print("Enter a space-separated list of URIs")
411+
if search_results is not None:
412+
print("Alternatively, enter numbers 1-20, referring to the results of the previous search")
413+
414+
choice = get_input_with_constraint(">", lambda x: True)
415+
416+
def decode_uri(uri):
417+
try:
418+
assert (search_results is not None)
419+
index = int(uri)
420+
assert (1 <= index <= 20)
421+
return search_results[index - 1]
422+
except (ValueError, KeyError, AssertionError):
423+
return uri
424+
425+
uris = [decode_uri(uri) for uri in choice.split(" ")]
426+
427+
run_describe(iris=uris)
428+
429+
elif choice == "search":
430+
method = get_choice("Which method (description/keyword/title)?> ",
431+
{
432+
"description": ["description", "d"],
433+
"keyword": ["keyword", "k"],
434+
"title": ["title", "t"]
435+
}
436+
)
437+
438+
query = get_input_with_constraint("what is your query?> ", lambda x: True)
439+
keywords = query.split(" ")
440+
441+
search_results = run_search(keywords=keywords, method=method)
442+
362443

363444

364445

0 commit comments

Comments
 (0)