Skip to content

Commit

Permalink
Added a Similar Query Feature to the Get Command (#6)
Browse files Browse the repository at this point in the history
* Created a similar queries function

* Created a nice similar queries UI

* Incremented the version

* Cleaned up testing code
  • Loading branch information
jrg94 authored Mar 22, 2022
1 parent cd79029 commit 97f37e5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
12 changes: 11 additions & 1 deletion code_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from dotenv import load_dotenv


__version__ = "0.1.1"
__version__ = "0.2.0"


# Global variables
Expand All @@ -28,6 +28,7 @@
load_dotenv()
queries = json.load(open("queries.json"))
keyword_mapping = generate_keyword_mapping(queries)
generate_similar_queries(queries, keyword_mapping)


# Discord bot code
Expand Down Expand Up @@ -102,6 +103,15 @@ async def _get(ctx, index: int):
value=queries[index].get("response"),
inline=False
)
similar_queries = queries[index].get("similar_queries", [])[:3]
if similar_queries:
embed.add_field(
name=f"Similar Queries",
value="\n".join(f"• ID-{i}: {create_md_link(queries[i].get('resource'), queries[i].get('query'))}"
for i in similar_queries
),
inline=True
)

await ctx.send(embed=embed)

Expand Down
15 changes: 15 additions & 0 deletions code_bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ def search(keyword_to_queries: dict, keywords: list) -> list:
return best_matches


def generate_similar_queries(queries: list, keyword_to_queries: dict) -> None:
"""
Generates a list of similar queries.
:param queries: a list of queries
:param keyword_to_queries: a mapping of keywords to query indices
"""
for i, query in enumerate(queries):
if i > 0:
keywords = generate_keywords(query["query"])
top_ids = search(keyword_to_queries, keywords)
top_ids.remove(i)
query["similar_queries"] = top_ids


def create_md_link(url: string, text: string) -> string:
"""
Creates a markdown link.
Expand Down
4 changes: 4 additions & 0 deletions test_code_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ def test_generate_keyword_mapping():
}
keyword_mapping = generate_keyword_mapping([test_query])
assert keyword_mapping == expected_mapping

def test_generate_similar_queries():
generate_similar_queries(queries, keyword_mapping)
assert 1 not in queries[1]["similar_queries"]

0 comments on commit 97f37e5

Please sign in to comment.