开发者

Comparing multiple strings

Hey I am new I need some help with comparing strings My Assignment is to make a chatbot, one that reads from a text file, that has possible things to input, and what the resulting output will be.

My problem is that it asks to choose the most suited one fr开发者_Go百科om the text file, easy yeh? but you also must save variables at the same time

Ok an example is one of the lines of the rules is:

you <w1> <w2> <w3> <w4> me | What makes you think I <w1> <w2> <w3> <w4> you?

You must save the <w1> and so on to a variable. AND the input can be like, "did you know that you are really nice to me" so you have to adjust the code for that as well.

And also we cant make the code just for this text file, it is supposed to adjust to anything that is put into the text file.

Can someone help me ?

This is what I'm up to:

import string
import sys
import difflib


#File path:
rules = open("rules.txt", "rU")

#Set some var's:
currentField = 0
fieldEnd = 0
questions = []
responses = []
Input = ""
run = True

#Check if we are not at the end of the file:
for line in rules:
    linem = line.split(" | ")
    question = linem[0]
    response = linem[1]

    questions.append(question.replace("\n", ""))
    responses.append(response.replace("\n", ""))

print questions
print responses

for q in questions:
    qwords.appendq.split()

while run = True:
    Input = raw_input('> ').capitalize()

    for char in Input:
        for quest in questions:
            if char in quest:
                n += 1
            else:
                if "<" in i:
                    n += 1
            closestQuestion = questions.index(q)


    print response


I would prefer pyparsing over any regex-based approach to tackle this task. It's easier to construct a readable parser even for more involved and complex grammars.


As a quick-and-stupid solution, parse input file and store entries in list. Each entry should contain dynamically-compiled "matching regex" (e.g. r'you (\w+) (\w+) (\w+) (\w+) me(?i)') and "replacement string" (e.g. r'What makes you think I \1 \2 \3 \4 you?'). For each incoming request, chat bot should match text agains regex list, find appropriate entry and then call regex.sub() for "replacement string".

But first of all, read some beginner's tutorial on Python. Your code is un-pythonic and just wrong in many ways.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜