Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
woodthom2 authored Jun 5, 2024
2 parents 0047a42 + f8b4482 commit 62be37a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/harmony/matching/negator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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":
Expand Down
3 changes: 3 additions & 0 deletions tests/test_negator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 62be37a

Please sign in to comment.