Python 如何找到输入实体与数据库实体之间的相似性
我正试图创建一个聊天机器人与拉萨nlu,将有助于酒店搜索。我创建了一个小的sqlite数据库,其中包含一些餐馆的名称和其他描述。这是我数据库的结构Python 如何找到输入实体与数据库实体之间的相似性,python,sqlite,chatbot,rasa-nlu,Python,Sqlite,Chatbot,Rasa Nlu,我正试图创建一个聊天机器人与拉萨nlu,将有助于酒店搜索。我创建了一个小的sqlite数据库,其中包含一些餐馆的名称和其他描述。这是我数据库的结构 Name Cuisine Price ambience location rating Flower Drum chinese high 2 south 5 Little Italy italian high 2 south
Name Cuisine Price ambience location rating
Flower Drum chinese high 2 south 5
Little Italy italian high 2 south 2
Quattro mexican low 2 center 3
Domino's Pizza fast food mid 0 east 3
我对翻译进行了一些像这样的定制培训
## intent:hotel_search
- I'm looking for a [Mexican](cuisine) restaurant in the [North](location) of town
- Which is the [best](rating) restaurant in the city
- Which restaurant has the most [rating](rating) in the city
- I am looking for a [burger](dish) joint in the [south](location) of the city
- I am trying to find an [expensive](price) [Indian](cuisine) restaurant in the [east](location) of the city
这是培训口译员的代码
def train(data, config_file, model_dir):
training_data = load_data(data)
trainer = Trainer(config.load(config_file))
trainer.train(training_data)
model_directory = trainer.persist(model_dir, fixed_model_name = 'chat')
这是从sqlite数据库中查找酒店的代码
def find_hotels(params):
# Create the base query
query = 'SELECT * FROM hotels'
# Add filter clauses for each of the parameters
if len(params) > 0:
filters = ["{}=?".format(k) for k in params]
query += " WHERE " + " and ".join(filters)
# Create the tuple of values
t = tuple(params.values())
# Open connection to DB
conn = sqlite3.connect('hotels.sqlite')
# Create a cursor
c = conn.cursor()
# Execute the query
c.execute(query, t)
# Return the results
return c.fetchall()
这是用于响应输入消息的代码
# Define respond()
def respond(message):
# responses
responses = ["I'm sorry :( I couldn't find anything like that",
'{} is a great hotel!',
'{} or {} would work!',
'{} is one option, but I know others too :)']
# Extract the entities
entities = interpreter.parse(message)["entities"]
# Initialize an empty params dictionary
params = {}
# Fill the dictionary with entities
for ent in entities:
params[ent["entity"]] = str(ent["value"])
print("\n\nparams: {}\n\n".format(params))
# Find hotels that match the dictionary
results = find_hotels(params)
print("\n\nresults: {}\n\n".format(results))
# Get the names of the hotels and index of the response
names = [r[0] for r in results]
n = min(len(results),3)
# Select the nth element of the responses array
return responses[n].format(*names)
但是当我用这个例子测试解释器时
我想在城市南部找一家昂贵的中餐馆
这就是我得到的结果
params: {'price': 'expensive', 'cuisine': 'chinese', 'location': 'south'}
results: []
I'm sorry :( I couldn't find anything like that
如果我从输入问题中删除昂贵的单词,我会得到这样一个正确的结果
我想在城市南部找一家中国餐馆
bot能够识别所有实体,但无法从数据库中选择正确的数据,因为数据库中的价格列中没有以名称“昂贵”输入的数据。如何训练机器人将单词“昂贵”识别为“高”您可以在nlu.md中添加同义词。将此项添加到您的文件中,“昂贵”将映射到高:
## synonym:high
- expensive
谢谢。代码起作用了。但我不知道为什么这个问题被低估了。我有一个真正的怀疑。
## synonym:high
- expensive