-
Notifications
You must be signed in to change notification settings - Fork 66
feat(ai): improve spacy similarity #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7fe2a7d
improve spacy similarity
manuelporrasojeda c41cdf4
move to file
manuelporrasojeda 3461b53
fix lint
manuelporrasojeda d93ec02
model update
manuelporrasojeda c179131
en_core_web_md
manuelporrasojeda 3ad2237
split in files
manuelporrasojeda 623e5c4
fix typo
manuelporrasojeda 16cbeff
move toolium import
manuelporrasojeda ff9d5b9
improve tests
manuelporrasojeda 8efb49a
no spacy import at text_similarity file
manuelporrasojeda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Copyright 2025 Telefónica Innovación Digital, S.L. | ||
| This file is part of Toolium. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| """ | ||
|
|
||
| import logging | ||
| from functools import lru_cache | ||
|
|
||
| # AI library imports must be optional to allow installing Toolium without `ai` extra dependency | ||
| try: | ||
| import spacy | ||
| except ImportError: | ||
| spacy = None | ||
|
|
||
|
|
||
| # Configure logger | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @lru_cache(maxsize=8) | ||
| def get_spacy_model(model_name): | ||
| """ | ||
| get spaCy model. | ||
| This method uses lru cache to get spaCy model to improve performance. | ||
|
|
||
| :param model_name: spaCy model name | ||
| :return: spaCy model | ||
| """ | ||
| return spacy.load(model_name) | ||
|
|
||
|
|
||
| def is_negator(tok): | ||
| """ | ||
| Check if a token is a negator using Universal Dependencies guidelines | ||
| Note: some languages may have different negation markers. That's why we use UD guidelines. | ||
|
|
||
| :param tok: spaCy token | ||
| """ | ||
| # Universal Dependencies negation detection (e.g., Spanish "no", "nunca", etc.) | ||
| if tok.dep_ == "neg": | ||
| return True | ||
| # Some languages use Polarity=Neg for negation words (e.g., Spanish "no", "sin", etc.) | ||
| if "Neg" in tok.morph.get("Polarity"): | ||
| return True | ||
| # Some languages use PronType=Neg for negation words (e.g., Spanish "nunca", "nadie", etc.) | ||
| if "Neg" in tok.morph.get("PronType"): | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def preprocess_with_ud_negation(text, nlp): | ||
| """ | ||
| Preprocess text using Universal Dependencies negation handling. | ||
| It tags negated words with "NEG_" prefix and replaces negators with "NEGATOR" token. | ||
| Stop words are removed. | ||
|
|
||
| :param text: input text | ||
| :param nlp: spaCy language model | ||
| """ | ||
| doc = nlp(text) | ||
| # 1) Negators indexes | ||
| neg_idxs = {t.i for t in doc if is_negator(t)} | ||
| # 2) Negated heads indexes | ||
| negated_heads = set() | ||
| for i in neg_idxs: | ||
| head = doc[i].head | ||
| if head.is_alpha and not head.is_stop: | ||
| negated_heads.add(head.i) | ||
|
|
||
| toks = [] | ||
| for t in doc: | ||
| if not t.is_alpha: | ||
| continue | ||
| # Keep negators as is | ||
| if is_negator(t): | ||
| toks.append("NEGATOR") | ||
| continue | ||
| if t.is_stop: | ||
| continue | ||
|
|
||
| lemma = t.lemma_.lower() | ||
| if t.i in negated_heads: | ||
| toks.append("NEG_" + lemma) | ||
| else: | ||
| toks.append(lemma) | ||
| return " ".join(toks) | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.