-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
1 changed file
with
52 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,52 @@ | ||
import pandas as pd | ||
from sentence_transformers import SentenceTransformer | ||
|
||
from fmtr.tools.logging_tools import logger | ||
|
||
|
||
class SemanticManager: | ||
""" | ||
Base semantic similarity manager | ||
""" | ||
|
||
REPO_ID = 'distiluse-base-multilingual-cased-v1' | ||
|
||
def __init__(self, data: pd.Series): | ||
logger.info(f"Loading model from {self.REPO_ID}") | ||
self.model = SentenceTransformer(self.REPO_ID) | ||
self.data = data | ||
logger.info(f"Vectorising {len(data)} texts using {self.model.device}...") | ||
self.embs = self.vectorise() | ||
|
||
def vectorise(self): | ||
""" | ||
Vectorise the corpus | ||
""" | ||
embs = self.model.encode(self.data.tolist()) | ||
return embs | ||
|
||
def get_sims(self, string: str): | ||
""" | ||
Get similarities between query string and corpus | ||
""" | ||
logger.info(f'Getting similarities for search term: "{string}"...') | ||
embs_query = self.model.encode([string]) | ||
sims = self.model.similarity(self.embs, embs_query).squeeze().numpy() | ||
return sims | ||
|
||
def get_matches(self, string: str, top_n: int = 20): | ||
""" | ||
Get the Top N matches between query string and corpus | ||
""" | ||
sims = self.get_sims(string) | ||
args = sims.argsort()[::-1] | ||
matches = self.data.iloc[args][:top_n] | ||
return matches |