Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在系统上运行聊天机器人_Python_Chatbot - Fatal编程技术网

Python 在系统上运行聊天机器人

Python 在系统上运行聊天机器人,python,chatbot,Python,Chatbot,这是为创建聊天机器人而编写的代码。当我尝试在cmd上运行此代码时。通过使用命令python chatbot.py,它返回一个错误,表示语法无效。 是否有任何方法可以删除此错误并在我的系统上运行此代码 它给出错误:文件“chatbot.py”,第1行语法:无效语法您运行的是什么版本的Python,在什么环境下运行?我在Windows下的Python 3.70b4上运行了这段代码,除了第52行之外,它运行得很好: H=原始输入('H:')。条带() 您必须更改为: H=输入('H:')。条带() 这

这是为创建聊天机器人而编写的代码。当我尝试在cmd上运行此代码时。通过使用命令python chatbot.py,它返回一个错误,表示语法无效。 是否有任何方法可以删除此错误并在我的系统上运行此代码


它给出错误:文件“chatbot.py”,第1行语法:无效语法

您运行的是什么版本的Python,在什么环境下运行?我在Windows下的Python 3.70b4上运行了这段代码,除了第52行之外,它运行得很好: H=原始输入('H:')。条带() 您必须更改为: H=输入('H:')。条带()
这可能与您的问题没有直接关系,但您发布的代码在我的环境中运行良好,在我做了一次更改(当然安装了所需的任何库或模块)。

请发布错误代码。它指向哪里?通常这些错误都带有行号。使用文本编辑器转到该行,并四处查看。如果您需要我们的帮助,那么您需要复制实际的错误(完整的和完整的),并编辑您的问题以粘贴它(除了格式化之外,没有任何编辑)。在编辑问题以显示错误的同时,还要标出错误所在的行,例如注释。它给出错误:文件“chatbot.py”,第1行语法:无效语法错误代码请…它给出错误:文件“chatbot.py”,第1行语法:无效语法
import re
import sqlite3
from collections import Counter
from string import punctuation
from math import sqrt

# initialize the connection to the database
connection = sqlite3.connect('chatbot.sqlite')
cursor = connection.cursor()

# create the tables needed by the program
create_table_request_list = [
    'CREATE TABLE words(word TEXT UNIQUE)',
    'CREATE TABLE sentences(sentence TEXT UNIQUE, used INT NOT NULL DEFAULT 0)',
    'CREATE TABLE associations (word_id INT NOT NULL, sentence_id INT NOT NULL, weight REAL NOT NULL)',
]
for create_table_request in create_table_request_list:
    try:
        cursor.execute(create_table_request)
    except:
        pass

def get_id(entityName, text):
    """Retrieve an entity's unique ID from the database, given its associated text.
    If the row is not already present, it is inserted.
    The entity can either be a sentence or a word."""
    tableName = entityName + 's'
    columnName = entityName
    cursor.execute('SELECT rowid FROM ' + tableName + ' WHERE ' + columnName + ' = ?', (text,))
    row = cursor.fetchone()
    if row:
        return row[0]
    else:
        cursor.execute('INSERT INTO ' + tableName + ' (' + columnName + ') VALUES (?)', (text,))
        return cursor.lastrowid

def get_words(text):
    """Retrieve the words present in a given string of text.
    The return value is a list of tuples where the first member is a lowercase word,
    and the second member the number of time it is present in the text."""
    wordsRegexpString = '(?:\w+|[' + re.escape(punctuation) + ']+)'
    wordsRegexp = re.compile(wordsRegexpString)
    wordsList = wordsRegexp.findall(text.lower())
    return Counter(wordsList).items()


B = 'Hello!'
while True:
    # output bot's message
    print('B: ' + B)
    # ask for user input; if blank line, exit the loop
    H = raw_input('H: ').strip()
    if H == '':
        break
    # store the association between the bot's message words and the user's response
    words = get_words(B)
    words_length = sum([n * len(word) for word, n in words])
    sentence_id = get_id('sentence', H)
    for word, n in words:
        word_id = get_id('word', word)
        weight = sqrt(n / float(words_length))
        cursor.execute('INSERT INTO associations VALUES (?, ?, ?)', (word_id, sentence_id, weight))
    connection.commit()
    # retrieve the most likely answer from the database
    cursor.execute('CREATE TEMPORARY TABLE results(sentence_id INT, sentence TEXT, weight REAL)')
    words = get_words(H)
    words_length = sum([n * len(word) for word, n in words])
    for word, n in words:
        weight = sqrt(n / float(words_length))
        cursor.execute('INSERT INTO results SELECT associations.sentence_id, sentences.sentence, ?*associations.weight/(4+sentences.used) FROM words INNER JOIN associations ON associations.word_id=words.rowid INNER JOIN sentences ON sentences.rowid=associations.sentence_id WHERE words.word=?', (weight, word,))
    # if matches were found, give the best one
    cursor.execute('SELECT sentence_id, sentence, SUM(weight) AS sum_weight FROM results GROUP BY sentence_id ORDER BY sum_weight DESC LIMIT 1')
    row = cursor.fetchone()
    cursor.execute('DROP TABLE results')
    # otherwise, just randomly pick one of the least used sentences
    if row is None:
        cursor.execute('SELECT rowid, sentence FROM sentences WHERE used = (SELECT MIN(used) FROM sentences) ORDER BY RANDOM() LIMIT 1')
        row = cursor.fetchone()
    # tell the database the sentence has been used once more, and prepare the sentence
    B = row[1]
    cursor.execute('UPDATE sentences SET used=used+1 WHERE rowid=?', (row[0],))