playing with nltk to make a chatbot

I had an old bit, from a project in class called 'rabbithole' that I used to have the computer return a made up sentence for a user in the game when they entered data that did not fit any of my if/elif clauses.
It was a bit of a mashup text based game.... Anyhoo,  I decided I wanted to explore A.I. and someone I know suggested looking at 'ELIZA':

# someone made a javascript web version of her!
http://www.masswerk.at/elizabot/

I think the name at the bottom of that link is the author/writer, N. Landsteiner

wickipedia:
https://en.wikipedia.org/wiki/ELIZA

my little mess around with masswerk link:





























Anyhoo, here's a bit of code where I'm trying to figure out how to use this nltk, and wordnet to process my nouns and verbs so I can return sentences to the user when mine can't find a scripted answer:  (fyi:  there is a bit of work to nltk, it's not just a simple import to get the wordnet and stuff that processes for it) (import into (nltk) python shell, and it'll tell you,  like nltk.download('wordnet')) when it encounters something it needs to run.

pics of it in action:
                    the find_NN_VV() *top                   find_synonyms()*bottom



                           




























code:



#!usr/bin/python
# -*- coding: utf-8 -*-

from nltk.corpus import wordnet
from random import randint
import nltk as nltk


def run_synonyms():
search_for = input(">>>")
while search_for != 'QUIT':

alist = []
for syn in wordnet.synsets(search_for):
for l in syn.lemmas():
alist.append(l.name())
if len(alist) > 0:
length = len(alist) - 1
x = randint(0, length)
#print(x)
synx = alist[x]
print(f"possible matches = {length + 1}")
print(f"Are you looking for the word similar too : {synx} ?")
else:
print("word not found in wordnet")
search_for = input(">>>")


def find_NN_VV():
search_for = input("...>>>")
while search_for != 'EXIT':
nounlist = []
verblist = []

tokens = nltk.word_tokenize(search_for)
#print(tokens)
tags = nltk.pos_tag(tokens)
#print(tags)
for item in tags:
#print(item[1][0])
if item[1][0] == 'N':
nounlist.append(item[0])
if item[1][0] == 'V':
verblist.append(item[0])
print("nouns = ")
print(nounlist)
print("verbs = ")
print(verblist)
search_for = input("...>>>")

#find_NN_VV()
#run_synonyms()




















Comments

Popular posts from this blog

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame

playing with color in powershell python