import spacy
import textacy

scoreForIntroduction = 40/100
nlp = spacy.load("en_core_web_sm")


def introductionFunc(speech):
    totalCharacterCount = len(speech)

    introductionCharacterCount = (15 / 100) * totalCharacterCount
    introduction = (speech[0:int(introductionCharacterCount)])
    # print(">>>Introduction<<<")
    # print(introduction)
    return {
        "message": introduction,
        "score": scoreForIntroduction
    }


def introductionBestUsesFunc(speech):

    introduction = nlp(introductionFunc(speech)["message"])
    final_words = []

    with open('content analyzing/bestPracticesForIntroduction.txt', 'r') as file:
        for line in file:
            clear_line = line.replace("\n", '').replace(",", '').replace("'", '').strip()
            word, emotion = clear_line.split(':')

            if word in introduction:
                final_words.append(word)
    return {
        "message": final_words,
        "score": scoreForIntroduction
    }


def introductionQuestions(speech):
    doc = nlp(introductionFunc(speech)["message"])

    patterns = [[{"POS": "ADV"}, {"POS": "AUX"}, {"POS": "PRON"}], [{"POS": "ADV"}, {"POS": "AUX"}, {"POS": "NOUN"}]]
    retVal = []
    verb_phrases = textacy.extract.token_matches(doc, patterns)
    for verb_phrases in verb_phrases:
        print("You used questions forms in your introduction. It is a good practice for a introduction.")
        print(f"Identified questions : {verb_phrases}")
        retVal.append(f"Identified questions : {verb_phrases}")
    return {
        "message": retVal,
        "score": scoreForIntroduction
    }

