2017-10-11 2 views
1

私はちょうどコーディングに入り、質問があります。 私はSashaというチャットボット用のスクリプトを書いていますが、文章内のすべての単語が一致しているわけではないという問題を解決する方法はありません。 「日付」というよりも、日付を別にチェックするようにしたいと思っています。それをどうやってやりますか? 何か助けていただければ幸いです。入力、pythonの項目を検索して印刷するには?

Database =[ 

     ['hello sasha', 'hey there'], 

     ['what is the date today', 'it is the 13th of October 2017'], 

     ['name', 'my name is sasha'], 

     ['weather', 'it is always sunny At Essex'], 

     ] 

while 1: 
     variable = input("> ") 

     for i in range(4): 
       if Database[i][0] == variable: 
         print (Database[i][1]) 

答えて

0

あなたは

更新答えるために入力をマッピングするために辞書を使用することができます。 は、入力に一致するように正規表現を追加しますが、私はより多くのNLPの質問のように、あなたの質問だと思います。

import re 
Database ={ 

     'hello sasha': 'hey there', 

     'what is the date today':'it is the 13th of October 2017', 

     'name': 'my name is sasha', 

     'weather': 'it is always sunny At Essex', 

     } 

while 1: 
     variable = input("> ") 
     pattern= '(?:{})'.format(variable) 
     for question, answer in Database.iteritems(): 
      if re.search(pattern, question): 
       print answer 

出力:

date 
it is the 13th of October 2017 
0

A非常にルディメンタル答えは文章中の単語をチェックするために、次のようになります。

while 1: 
    variable = input("> ") 

    for i, word in enumerate(["hello", "date", "name", "weather"]): 
     if word in input.split(" "): # Gets all words from sentence 
      print(Database[i][1]) 


    in: 'blah blah blah blah date blah' 
    out: 'it is the 13th of October 2017' 
    in: "name" 
    out: "my name is sasha" 
1

あなたが何かであるかどうかを確認するには 'で' を使用することができますリストは以下のようになります。(擬似コード内)

list = ['the date is blah', 'the time is blah'] 

chat = input('What would you like to talk about') 

if chat in ['date', 'what is the date', 'tell the date']: 
    print(list[0]) 

elif chat in ['time', 'tell the time']: 
    print(list[1]) 

etc. 

あなたは辞書が何であるかを知ることが大切です。

関連する問題