Skip to content

Commit

Permalink
Combine new negation contributions from hackathon
Browse files Browse the repository at this point in the history
  • Loading branch information
woodthom2 committed Jun 5, 2024
1 parent 7a32d86 commit 2d61da7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
20 changes: 1 addition & 19 deletions src/harmony/matching/negator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,6 @@
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:
"""
Expand Down Expand Up @@ -189,6 +170,7 @@ def get_change_fr(doc) -> dict:
return result
return {0: ("insert_before", "ne pas")}


def negate(text: str, language: str) -> str:
"""
Converts negative sentences to positive and vice versa.
Expand Down
15 changes: 9 additions & 6 deletions tests/test_negator.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def test_simple_example_pt(self):
def test_simple_example_pt_neg(self):
text = "não eu me sinto deprimido"
self.assertEqual(" eu me sinto deprimido", negate(text, "pt"))

def test_simple_example_es(self):
text = "mi siento deprimido"
self.assertEqual("no mi siento deprimido", negate(text, "es"))
Expand All @@ -78,13 +79,15 @@ def test_simple_example_de_neg(self):
def test_simple_example_it(self):
text = "mi sento depresso"
self.assertEqual("non mi sento depresso", negate(text, "it"))
#
# 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"))

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()

0 comments on commit 2d61da7

Please sign in to comment.