# English to propositional logical statement converter
# Made by Varghese K James
# 3122245001192
# B.E. CSE, SSNCE

class EnglishToLogic:
    def __init__(self) -> None:
        self.propositions = {}

    def add_proposition(self, phrase, symbol):
        self.propositions[self.clean_text(phrase)] = symbol

    def clean_text(self, text):
        text = text.lower()

        for word in [" the ", " a ", " an "]:
            text = text.replace(word, "")
        
        text = text.replace(".", "").replace(",", "")
        return text.strip()
    
    def convert(self, sentance):
        text = self.clean_text(sentance)

        # Biconditional
        text = text.replace("if and only if", "<->")

        # Conditional
        if text.startswith("if ") and " then " in text:
            text = text[3:] 
            text = text.replace(" then ", " -> ")

        # and, or, not
        text = text.replace(" or ", " v ")
        text = text.replace(" and ", " ^ ")
        text = text.replace(" not ", " ~")
        if text.startswith("not "): 
            text = text.replace("not ", "~", 1)

        # proposition conversion
        for phrase, symbol in self.propositions.items():
            if phrase in text:
                text = text.replace(phrase, symbol)

        return text.replace(" ", "")
    
e2l = EnglishToLogic()

e2l.add_proposition("All men are mortal", "A")
e2l.add_proposition("varghese is a man", "B")

sentances = [
    "All men are mortal and varghese is a man",
    "All men are mortal or varghese is a man",
    "not all men are mortal",
    "varghese is a man if and only if all men are mortal.",
    "varghese is a man if and only if not all men are mortal.",
    "if not all men are mortal then varghese is a man."
]

for s in sentances:
    print(f"\nInput: {s}")
    print(f"Output: {e2l.convert(s)}")



