Skip to content

Commit

Permalink
Merge pull request #44 from ollylucl/french_verb_negation
Browse files Browse the repository at this point in the history
French verb negation
  • Loading branch information
woodthom2 committed Jun 5, 2024
2 parents b85d050 + f0c211b commit f8b4482
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 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 @@ -72,6 +93,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 negate(text: str, language: str) -> str:
"""
Expand All @@ -85,6 +115,8 @@ def negate(text: str, language: str) -> str:

if language == "pt":
changes = get_change_pt(doc)
elif language == "fr":
return get_change_fr(text)
else:
changes = get_change_en(doc)

Expand Down
4 changes: 4 additions & 0 deletions tests/test_negator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def test_simple_example_pt(self):
text = "eu me sinto deprimido"
self.assertEqual("não eu me sinto deprimido", negate(text, "pt"))

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 f8b4482

Please sign in to comment.