Skip to content

Instantly share code, notes, and snippets.

@kusal1990
Created June 24, 2022 08:14
Show Gist options
  • Save kusal1990/ff5dbb3ac09dd67cc7553d25c6dd0f92 to your computer and use it in GitHub Desktop.
Save kusal1990/ff5dbb3ac09dd67cc7553d25c6dd0f92 to your computer and use it in GitHub Desktop.
def get_context(question, options):
'''
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
'''
scores = []
sentences = []
choices = []
#Dividing options into separate parts.
if '(A)' in options:
split_1 = options.split('(B)')
option_1 = split_1[0].replace('(A)','')
choices.append(option_1)
split_2 = split_1[1].split('(C)')
option_2 = split_2[0]
choices.append(option_2)
if '(D)' not in split_2[1]:
option_3 = split_2[1]
choices.append(option_3)
elif '(D)' in split_2[1]:
split_3 = split_2[1].split('(D)')
option_3 = split_3[0]
choices.append(option_3)
if '(E)' not in split_3[1]:
option_4 = split_3[1]
choices.append(option_4)
else:
split_4 = split_3[1].split('(E)')
option_4 = split_4[0]
choices.append(option_4)
option_5 = split_4[1]
choices.append(option_5)
else:
split_1 = options.split('(2)')
option_1 = split_1[0].replace('(1)','')
choices.append(option_1)
split_2 = split_1[1].split('(3)')
option_2 = split_2[0]
choices.append(option_2)
if '(4)' not in split_2[1]:
option_3 = split_2[1]
choices.append(option_3)
elif '(4)' in split_2[1]:
split_3 = split_2[1].split('(4)')
option_3 = split_3[0]
choices.append(option_3)
if '(5)' not in split_3[1]:
option_4 = split_3[1]
choices.append(option_4)
else:
split_4 = split_3[1].split('(5)')
option_4 = split_4[0]
choices.append(option_4)
option_5 = split_4[1]
choices.append(option_5)
for i in [choices]:
query=question+f'{i}'
search_start = time.time()
search = {"size":20,"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))
#Searching in the 'corpus' index.
a = es.search(index='corpus2', body=json.dumps(search))
for j in a['hits']['hits']:
sentence = j['_source']
out=list(sentence.values())
out = reduce(operator.concat,out)
sentences.append(out)
context= ' '.join(sentences) #Selecting top 20 sentences for the context.
return context
@kusal1990
Copy link
Author

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment