Search code examples
pythondataframenltkinteractivechatterbot

python: (calling all experts out there to help) how do i write a chatbot which can commands and execute a python function?


i was going through chatterbot, NLTK libraries writeups but i just could not find a way where i provide an english like command and the response can be a python function.

For example: i could say "Get me all sales numbers for August in the Delhi", it should pick words from it and do a dataframe query to fetch me the data. and similar intelligence.

In summary, i will speak to data in english and it should convert into relevant dataframe command to get me the data.

any thoughts ?


Solution

  • Well one suggestion is to use NLP Linguistic Features

    For ease, i will be using spacy

     import spacy
     nlp = spacy.load('en_core_web_md')
     doc = nlp('Get me all sales numbers for August in the Delhi')
     for token in doc:
        print(token.text,token.dep_,token.pos_)
    

    Output

    | 'Word'  | 'DEPENDENY' | 'POS'  |
    ----------------------------------
    |'Get'    |   'ROOT'    | 'AUX'  |
    |'me',    |  'dative'   | 'PRON' |
    |'all'    |   'det'     | 'DET'  |
    |'sales'  | 'compound'  | 'NOUN' |
    |'numbers'|   'dobj'    | 'NOUN' |
    |'for'    |   'prep'    | 'ADP'  |
    |'August' |   'pobj'    | 'PROPN'|
    |'in'     |   'prep'    | 'ADP'  |
    |'the'    |   'det'     | 'DET'  |
    |'Delhi'  |   'pobj'    | 'PROPN'|
    

    Here you can use these attributes extracted such as pos tagging and dependency to get your desired value e.g

    df[DEPENDENY.compound].mean()
    

    You can devise more NLP techniques such as entity recognition and based on that you can move further to create queries. This may take an in-depth thinking for logic formulation but u need to explore more and write some rules.