Skip to content

Instantly share code, notes, and snippets.

@alexandre
Last active August 9, 2018 13:44
Show Gist options
  • Save alexandre/2d5e62bd2ecdc2b86ecd7e046a7c0c3a to your computer and use it in GitHub Desktop.
Save alexandre/2d5e62bd2ecdc2b86ecd7e046a7c0c3a to your computer and use it in GitHub Desktop.
A ideia era montar uma tabela verdade, mas eu preciso dormir...
# https://spacy.io/
# pip install spacy
#
#
import collections
import spacy
nlp = spacy.load('pt')
def get_first_verb(sentence: str) -> str:
return next(item.text for item in nlp(sentence.capitalize()) if item.pos_ == 'VERB')
def get_first_verb_index(sentence: str) -> str:
return sentence.split().index(get_first_verb(sentence))
def get_negative_form(sentence: str) -> str:
if sentence.split()[get_first_verb_index(sentence) - 1].upper() == 'NÃO':
return sentence # already negative?
return ' '.join(
sentence.split()[:get_first_verb_index(sentence)] +
[('NÃO ' if sentence.isupper() else 'não ' + get_first_verb(sentence))] +
sentence.split()[(get_first_verb_index(sentence) + 1):]
)
def get_positive_form(sentence: str) -> str:
if sentence.split()[get_first_verb_index(sentence) - 1].upper() != 'NÃO':
return sentence # looks positive for me...
return ' '.join(sentence.split()[:get_first_verb_index(sentence) - 1] + sentence.split()[get_first_verb_index(sentence):])
def get_sentence_sentiment(sentence: str) -> float:
return collections.Counter(sentence.upper().split()).get('NÃO', 0) * -0.1
def is_sentence_negative(sentence: str) -> bool:
return get_sentence_sentiment(sentence) < 0
def invert_sentence(sentence: str) -> str:
if is_sentence_negative(sentence):
return get_positive_form(sentence)
return get_negative_form(sentence)
class Proposition:
"""Docstring for Proposition. """
def __init__(self, sentence: str, value: bool) -> None:
"""TODO: to be defined1. """
self._sentence = sentence
self._value = value
def __repr__(self):
return 'Proposition({}, {})'.format(self._sentence, self._value)
def __str__(self):
'({}),({})'.format(self._sentence, self._value)
def __invert__(self) -> 'Proposition':
"""The negation of a proposition"""
return Proposition(invert_sentence(self._sentence), not self._value)
def __or__(self, other) -> 'Proposition':
if not self._value and not other._value:
return Proposition('', False)
return self if self._value else other
def __xor__(self, other):
return Proposition('', self._value and other._value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment