diff --git a/src/harmony/matching/negator.py b/src/harmony/matching/negator.py index fe789a9..69674b7 100644 --- a/src/harmony/matching/negator.py +++ b/src/harmony/matching/negator.py @@ -26,10 +26,31 @@ ''' import spacy +import re nlp = spacy.blank("en") +def negate_french_sentence(sentence): + # Regular expression to match verbs + verb_pattern = re.compile(r'\b(je|tu|il|elle|nous|vous|ils|elles|on)\s+([a-zA-Zéèàùûôâî]+)\b', re.IGNORECASE) + + # Function to replace the verb with negated form + def replace_verb(match): + subject = match.group(1) + verb = match.group(2) + return f"{subject} ne {verb} pas" + + # Apply the negation + negated_sentence = verb_pattern.sub(replace_verb, sentence) + + # Check if the sentence had a match, if not, return original sentence with negation applied directly + if negated_sentence == sentence: + negated_sentence = re.sub(r'(\w+)', r'ne \1 pas', sentence) + + return negated_sentence + + def get_change_en(doc) -> dict: """ Identify how to change an English sentence from positive to negative or vice versa. @@ -79,6 +100,15 @@ def get_change_pt(doc) -> dict: return result return {0: ("insert_before", "não")} +def get_change_fr(doc) -> dict: + """ + Identify how to change a French sentence from positive to negative. Note that negative to positive + is not currently supported. + :param doc: + :return: + """ + return negate_french_sentence(doc) + def get_change_es(doc) -> dict: """ @@ -172,7 +202,6 @@ def negate(text: str, language: str) -> str: if language == "pt": changes = get_change_pt(doc) - # Team Cheemu: added handling of four additional languages elif language == "es": changes = get_change_es(doc) elif language == "it": diff --git a/tests/test_negator.py b/tests/test_negator.py index 5ce1bba..9d455a5 100644 --- a/tests/test_negator.py +++ b/tests/test_negator.py @@ -82,6 +82,9 @@ def test_simple_example_it(self): def test_simple_example_fr(self): text = "je me sens deprimé" self.assertEqual("ne pas je me sens deprimé", negate(text, "fr")) + def test_simple_example_fr(self): + text = "Je suis content" + self.assertEqual("Je ne suis pas content", negate(text, "fr")) if __name__ == '__main__': unittest.main()