Created
June 24, 2022 08:11
-
-
Save kusal1990/8c9fdfc9b0c4a0a84c2b62821383ee0d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_context_for_each_candidate(question,options_list): | |
""" Searches the user query and finds the best matches using elasticsearch.""" | |
#query = input("Enter query: ") | |
"""This function will return a context after joining a question | |
and its options separately. | |
For example: question + option_1 | |
question + option_2 | |
question + option_3 | |
question + option_4""" | |
option_1_docs = [] | |
option_2_docs = [] | |
option_3_docs = [] | |
option_4_docs = [] | |
option_5_docs = [] | |
for j in range(len(options_list)): | |
query=question+options_list[j] | |
search_start = time.time() | |
search = {"size": 8,"query": {"match": {"body": query}}} | |
#print(search) | |
response = es.search(index='corpus2', body=json.dumps(search)) | |
search_time = time.time() - search_start | |
#print("{} total hits.".format(response["hits"]["total"]["value"])) | |
print("search time: {:.2f} ms".format(search_time * 1000)) | |
sentences = [] | |
for hit in response["hits"]["hits"]: | |
sentence = hit['_source'] | |
out=list(sentence.values()) | |
out = reduce(operator.concat,out) | |
if j == 0: | |
option_1_docs.append(out) | |
elif j == 1: | |
option_2_docs.append(out) | |
elif j == 2: | |
option_3_docs.append(out) | |
elif j == 3: | |
option_4_docs.append(out) | |
elif j == 4: | |
option_5_docs.append(out) | |
return pd.Series([option_1_docs, option_2_docs, option_3_docs, option_4_docs, option_5_docs]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ok