Skip to content

Commit 9f29282

Browse files
committed
Fix issue #313
Resolved functional logic defect and ensured proper cleanup. Signed-off-by: Adraca <abhishek.k@adraca.io>
1 parent 2918069 commit 9f29282

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

query_expander.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import json
2+
import argparse
3+
from typing import List, Dict
4+
5+
# Load synonyms from synonyms.json
6+
def load_synonyms(file_path: str) -> Dict[str, List[str]]:
7+
with open(file_path, 'r') as file:
8+
return json.load(file)
9+
10+
# Expand query terms with synonyms
11+
def expand_query(query: str, synonyms: Dict[str, List[str]], top_n: int = 2) -> str:
12+
expanded_terms = []
13+
for term in query.split():
14+
if term in synonyms:
15+
# Add the original term and its top-n synonyms
16+
expanded_terms.append(term)
17+
expanded_terms.extend(synonyms[term][:top_n])
18+
else:
19+
expanded_terms.append(term)
20+
return " OR ".join(expanded_terms)
21+
22+
# Explain mode to show the expansion
23+
def explain_expansion(query: str, synonyms: Dict[str, List[str]], top_n: int = 2) -> str:
24+
expanded_terms = []
25+
for term in query.split():
26+
if term in synonyms:
27+
# Add the original term and its top-n synonyms
28+
expanded_terms.append(f"{term}{term}, " + ", ".join(synonyms[term][:top_n]))
29+
else:
30+
expanded_terms.append(term)
31+
return "\n".join(expanded_terms)
32+
33+
# Main function to process the query
34+
def main():
35+
parser = argparse.ArgumentParser(description="Expand user queries with synonyms before BM25 search.")
36+
parser.add_argument("query", type=str, help="The user query to be expanded")
37+
parser.add_argument("--no-expand", action="store_true", help="Disable query expansion")
38+
parser.add_argument("--explain", action="store_true", help="Show the expansion in explain mode")
39+
parser.add_argument("--synonyms", type=str, default="synonyms.json", help="Path to the synonyms JSON file")
40+
args = parser.parse_args()
41+
42+
# Load synonyms
43+
synonyms = load_synonyms(args.synonyms)
44+
45+
if args.no_expand:
46+
print(f"Query (no expansion): {args.query}")
47+
else:
48+
if args.explain:
49+
print("Explain mode:")
50+
print(explain_expansion(args.query, synonyms))
51+
else:
52+
expanded_query = expand_query(args.query, synonyms)
53+
print(f"Expanded Query: {expanded_query}")
54+
55+
if __name__ == "__main__":
56+
main()

0 commit comments

Comments
 (0)