import spacy
from spacy.lang.en.stop_words import STOP_WORDS
import string

nlp = spacy.load('en_core_web_sm')

def keywordExrtraction(topic, speech):
    Topic = nlp(topic)
    Content = nlp(speech)
    stopwords = list(STOP_WORDS)

    punctuation = string.punctuation + '\n'


    # This is a loop for count words apart from stop words and frequencies.
    word_frequencies = {}
    for word in Content:
        if word.text.lower() not in stopwords:
            if word.text.lower() not in punctuation:
                if word.text not in word_frequencies.keys():
                    word_frequencies[word.text] = 1
                else:
                    word_frequencies[word.text] += 1

    # print(word_frequencies)

    topicWords = []

    for words in Topic:
        topicWords.append(words.text)
    # print(topicWords)


    keyWords = []

    print("Extracted Key Words:")
    for word in word_frequencies.keys():
        if word_frequencies[word] >= 3:
            keyWords.append(word)
            print(word)

    return {
        "message": keyWords,
        "score": 50/100
    }

# keywordExrtraction("dog", "The dog is a pet animal. A dog has sharp teeth so that it can eat flesh very easily, it has four legs, two ears, two eyes, a tail, a mouth, and a nose. It is a very clever animal and is very useful in catching thieves. It runs very fast, barks loudly and attacks the strangers. A dog saves the life of the master from danger. One can find dogs everywhere in the world. Dogs are a very faithful animal. It has a sharp mind and a strong sense of hearing smelling the things. It also has many qualities like swimming in the water, jumping from anywhere, good smelling sense.")
