|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Copyright 2025 Telefónica Innovación Digital, S.L. |
| 4 | +This file is part of Toolium. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +""" |
| 18 | + |
| 19 | +import logging |
| 20 | +from functools import lru_cache |
| 21 | + |
| 22 | +# AI library imports must be optional to allow installing Toolium without `ai` extra dependency |
| 23 | +try: |
| 24 | + import spacy |
| 25 | +except ImportError: |
| 26 | + spacy = None |
| 27 | + |
| 28 | + |
| 29 | +# Configure logger |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +@lru_cache(maxsize=8) |
| 34 | +def get_spacy_model(model_name): |
| 35 | + """ |
| 36 | + get spaCy model. |
| 37 | + This method uses lru cache to get spaCy model to improve performance. |
| 38 | +
|
| 39 | + :param model_name: spaCy model name |
| 40 | + :return: spaCy model |
| 41 | + """ |
| 42 | + if spacy is None: |
| 43 | + return None |
| 44 | + return spacy.load(model_name) |
| 45 | + |
| 46 | + |
| 47 | +def is_negator(tok): |
| 48 | + """ |
| 49 | + Check if a token is a negator using Universal Dependencies guidelines |
| 50 | + Note: some languages may have different negation markers. That's why we use UD guidelines. |
| 51 | +
|
| 52 | + :param tok: spaCy token |
| 53 | + """ |
| 54 | + # Universal Dependencies negation detection (e.g., Spanish "no", "nunca", etc.) |
| 55 | + if tok.dep_ == "neg": |
| 56 | + return True |
| 57 | + # Some languages use Polarity=Neg for negation words (e.g., Spanish "no", "sin", etc.) |
| 58 | + if "Neg" in tok.morph.get("Polarity"): |
| 59 | + return True |
| 60 | + # Some languages use PronType=Neg for negation words (e.g., Spanish "nunca", "nadie", etc.) |
| 61 | + if "Neg" in tok.morph.get("PronType"): |
| 62 | + return True |
| 63 | + return False |
| 64 | + |
| 65 | + |
| 66 | +def preprocess_with_ud_negation(text, nlp): |
| 67 | + """ |
| 68 | + Preprocess text using Universal Dependencies negation handling. |
| 69 | + It tags negated words with "NEG_" prefix and replaces negators with "NEGATOR" token. |
| 70 | + Stop words are removed. |
| 71 | +
|
| 72 | + :param text: input text |
| 73 | + :param nlp: spaCy language model |
| 74 | + """ |
| 75 | + doc = nlp(text) |
| 76 | + # 1) Negators indexes |
| 77 | + neg_idxs = {t.i for t in doc if is_negator(t)} |
| 78 | + # 2) Negated heads indexes |
| 79 | + negated_heads = set() |
| 80 | + for i in neg_idxs: |
| 81 | + head = doc[i].head |
| 82 | + if head.is_alpha and not head.is_stop: |
| 83 | + negated_heads.add(head.i) |
| 84 | + |
| 85 | + toks = [] |
| 86 | + for t in doc: |
| 87 | + if not t.is_alpha: |
| 88 | + continue |
| 89 | + # Keep negators as is |
| 90 | + if is_negator(t): |
| 91 | + toks.append("NEGATOR") |
| 92 | + continue |
| 93 | + if t.is_stop: |
| 94 | + continue |
| 95 | + |
| 96 | + lemma = t.lemma_.lower() |
| 97 | + if t.i in negated_heads: |
| 98 | + toks.append("NEG_" + lemma) |
| 99 | + else: |
| 100 | + toks.append(lemma) |
| 101 | + return " ".join(toks) |
0 commit comments