2016-06-26 62 views
0

當我嘗試在Sublime中運行這個代碼時,它只是打印[在0.2秒內完成]。 我在做什麼錯?是不是假設在控制檯上運行chatbot?我發現這個代碼herenltk.chat.util的源代碼不運行? - 初學者

我知道這是一個基本的問題,但我是新的python和nltk。

# Natural Language Toolkit: Chatbot Utilities 
# 
# Copyright (C) 2001-2016 NLTK Project 
# Authors: Steven Bird <[email protected]> 
# URL: <http://nltk.org/> 
# For license information, see LICENSE.TXT 

# Based on an Eliza implementation by Joe Strout <[email protected]>, 
# Jeff Epler <[email protected]> and Jez Higgins <[email protected]>. 
from __future__ import print_function 

import re 
import random 
from nltk import compat 

reflections = { 
"i am"  : "you are", 
"i was"  : "you were", 
"i"   : "you", 
"i'm"  : "you are", 
"i'd"  : "you would", 
"i've"  : "you have", 
"i'll"  : "you will", 
"my"   : "your", 
"you are" : "I am", 
"you were" : "I was", 
"you've"  : "I have", 
"you'll"  : "I will", 
"your"  : "my", 
"yours"  : "mine", 
"you"  : "me", 
"me"   : "you" 
} 


class Chat(object): 
    def __init__(self, pairs, reflections={}): 
     """ 
     Initialize the chatbot. Pairs is a list of patterns and responses. Each 
    pattern is a regular expression matching the user's statement or question, 
    e.g. r'I like (.*)'. For each such pattern a list of possible responses 
    is given, e.g. ['Why do you like %1', 'Did you ever dislike %1']. Material 
    which is matched by parenthesized sections of the patterns (e.g. .*) is mapped to 
    the numbered positions in the responses, e.g. %1. 

    :type pairs: list of tuple 
    :param pairs: The patterns and responses 
    :type reflections: dict 
    :param reflections: A mapping between first and second person expressions 
    :rtype: None 
    """ 

    self._pairs = [(re.compile(x, re.IGNORECASE),y) for (x,y) in pairs] 
    self._reflections = reflections 
    self._regex = self._compile_reflections() 


def _compile_reflections(self): 
    sorted_refl = sorted(self._reflections.keys(), key=len, 
      reverse=True) 
    return re.compile(r"\b({0})\b".format("|".join(map(re.escape, 
     sorted_refl))), re.IGNORECASE) 

def _substitute(self, str): 
    """ 
    Substitute words in the string, according to the specified reflections, 
    e.g. "I'm" -> "you are" 

    :type str: str 
    :param str: The string to be mapped 
    :rtype: str 
    """ 

    return self._regex.sub(lambda mo: 
      self._reflections[mo.string[mo.start():mo.end()]], 
       str.lower()) 

def _wildcards(self, response, match): 
    pos = response.find('%') 
    while pos >= 0: 
     num = int(response[pos+1:pos+2]) 
     response = response[:pos] + \ 
      self._substitute(match.group(num)) + \ 
      response[pos+2:] 
     pos = response.find('%') 
    return response 

def respond(self, str): 
    """ 
    Generate a response to the user input. 

    :type str: str 
    :param str: The string to be mapped 
    :rtype: str 
    """ 

    # check each pattern 
    for (pattern, response) in self._pairs: 
     match = pattern.match(str) 

     # did the pattern match? 
     if match: 
      resp = random.choice(response) # pick a random response 
      resp = self._wildcards(resp, match) # process wildcards 

      # fix munged punctuation at the end 
      if resp[-2:] == '?.': resp = resp[:-2] + '.' 
      if resp[-2:] == '??': resp = resp[:-2] + '?' 
      return resp 

# Hold a conversation with a chatbot 


def converse(self, quit="quit"): 
    input = "" 
    while input != quit: 
     input = quit 
     try: input = compat.raw_input(">") 
     except EOFError: 
      print(input) 
     if input: 
      while input[-1] in "!.": input = input[:-1] 
      print(self.respond(input)) 

回答

1

您顯示的文件中包含chatbot程序所用的實用軟件。您不直接將該文件作爲python腳本運行。

轉到this nltk.org page並下載聊天機器人程序,如nltk.chat.eliza,然後用python運行它。

我假定您已經爲您的系統安裝了所需的nltk軟件。 我在Linux終端仿真器上測試了eliza聊天機器人,它按預期工作。對不起,我不知道Sublime,不能幫助你使用該工具。