Skip to content

Commit

Permalink
Various small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderVertegaal committed Aug 23, 2024
1 parent 2bc240b commit 515f9f1
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions backend/aethel_db/views/sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class AethelSampleDataResult:
name: str
phrases: list[AethelSampleDataPhrase] = field(default_factory=list)

def highlight_phrase_at_index(self, index: int) -> None:
self.phrases[index].highlight = True

def serialize(self):
return asdict(self)

Expand All @@ -34,7 +37,7 @@ class AethelSampleDataResponse:
results: list[AethelSampleDataResult] = field(default_factory=list)
error: str | None = None

def get_or_create_sample(self, sample: Sample) -> AethelSampleDataResult:
def get_or_create_result(self, sample: Sample) -> AethelSampleDataResult:
"""
Return an existing result with the same sample.name if it exists. Else create a new one.
"""
Expand All @@ -51,11 +54,6 @@ def get_or_create_sample(self, sample: Sample) -> AethelSampleDataResult:
self.results.append(new_result)
return new_result

def highlight_phrase(
self, sample: AethelSampleDataResult, highlighted_phrase_index: int
) -> None:
sample.phrases[highlighted_phrase_index].highlight = True

def json_response(self) -> JsonResponse:
return JsonResponse(
{
Expand All @@ -71,11 +69,14 @@ def get(self, request: HttpRequest) -> JsonResponse:
type_input = self.request.query_params.get("type", None)
word_input = self.request.query_params.get("word", None)

if word_input:
word_input = json.loads(word_input)

response_object = AethelSampleDataResponse()

# Not expected.
if not type_input or not word_input:
return response_object.json_response()

word_input = json.loads(word_input)

for sample in dataset.samples:
for phrase_index, phrase in enumerate(sample.lexical_phrases):
word_match = word_input and match_word_with_phrase_exact(
Expand All @@ -86,10 +87,8 @@ def get(self, request: HttpRequest) -> JsonResponse:
if not (word_match and type_match):
continue

aethel_sample = response_object.get_or_create_sample(sample=sample)
aethel_sample = response_object.get_or_create_result(sample=sample)

response_object.highlight_phrase(
sample=aethel_sample, highlighted_phrase_index=phrase_index
)
aethel_sample.highlight_phrase_at_index(index=phrase_index)

return response_object.json_response()

0 comments on commit 515f9f1

Please sign in to comment.