-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6cba94
commit d4e7733
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Set, Tuple, Optional, Sequence, List | ||
|
||
|
||
def aggregate_ngram_overlaps( | ||
ngrams: Set[Tuple[str]], limit: Optional[int] = None | ||
) -> Sequence[dict]: | ||
ngram_list = list(ngrams) | ||
pipeline: List[dict] = [ | ||
{"$match": {"textId.category": {"$ne": 99}}}, | ||
{ | ||
"$project": { | ||
"_id": 0, | ||
"textId": 1, | ||
"name": 1, | ||
"stage": 1, | ||
"overlap": { | ||
"$let": { | ||
"vars": { | ||
"intersection": { | ||
"$size": {"$setIntersection": ["$ngrams", ngram_list]} | ||
}, | ||
"minLength": { | ||
"$min": [ | ||
{"$size": "$ngrams"}, | ||
{"$size": [ngram_list]}, | ||
] | ||
}, | ||
}, | ||
"in": { | ||
"$cond": [ | ||
{"$eq": ["$$minLength", 0]}, | ||
0.0, | ||
{"$divide": ["$$intersection", "$$minLength"]}, | ||
] | ||
}, | ||
} | ||
}, | ||
} | ||
}, | ||
{"$sort": {"overlap": -1}}, | ||
] | ||
|
||
if limit: | ||
pipeline.append({"$limit": limit}) | ||
|
||
return pipeline |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from ebl.common.application.ngram_matcher import aggregate_ngram_overlaps | ||
from ebl.corpus.application.text_repository import TextRepository | ||
from ebl.fragmentarium.application.fragment_repository import FragmentRepository | ||
from falcon import Request, Response | ||
|
||
from ebl.transliteration.domain.museum_number import MuseumNumber | ||
|
||
|
||
class NgramMatchResource: | ||
def __init__( | ||
self, | ||
fragment_repository: FragmentRepository, | ||
text_repository: TextRepository, | ||
): | ||
self._fragment_repository = fragment_repository | ||
self._text_repository = text_repository | ||
|
||
def on_get(self, _req: Request, resp: Response, number: str) -> None: | ||
ngrams = self._fragment_repository.get_ngrams(MuseumNumber.of(number)) | ||
resp.media = self._text_repository._chapters.aggregate( | ||
aggregate_ngram_overlaps(ngrams) | ||
) |