Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

French verb negation #44

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading