Skip to content

Instantly share code, notes, and snippets.

@mullikine
Created December 4, 2019 08:12
Show Gist options
  • Save mullikine/4cc0bf47896df882247c02f8ee382e4f to your computer and use it in GitHub Desktop.
Save mullikine/4cc0bf47896df882247c02f8ee382e4f to your computer and use it in GitHub Desktop.
Semantic similarity chatbots from plain-text files https://gist.github.com/aparrish/92c25d4f57eb7186670e03105a991508
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
# coding: utf-8
# # Semantic similarity chatbots from plain-text files
#
# By [Allison Parrish](http://www.decontextualize.com/)
#
# This needs copy, sorry :( :( :( But it works!
input_file = "gutenberg_conversations.txt"
get_ipython().system('pip install https://github.com/aparrish/semanticsimilaritychatbot/archive/master.zip')
from semanticsimilaritychatbot import SemanticSimilarityChatbot
import spacy
nlp = spacy.load('en_core_web_lg')
chatbot = SemanticSimilarityChatbot(nlp, 300)
lastline = None
for line in open(input_file):
line = line.strip()
# empty lines mean "end of conversation"
if line == "":
lastline = None
continue
if lastline is not None:
chatbot.add_pair(lastline, line)
lastline = line
chatbot.build()
chatbot.response_for("Hello there.")
get_ipython().system('pip install flask')
chatbot_html = """
<style type="text/css">#log p { margin: 5px; font-family: sans-serif; }</style>
<div id="log"
style="box-sizing: border-box;
width: 600px;
height: 32em;
border: 1px grey solid;
padding: 2px;
overflow: scroll;">
</div>
<input type="text" id="typehere" placeholder="type here!"
style="box-sizing: border-box;
width: 600px;
margin-top: 5px;">
<script>
function paraWithText(t) {
let tn = document.createTextNode(t);
let ptag = document.createElement('p');
ptag.appendChild(tn);
return ptag;
}
document.querySelector('#typehere').onchange = async function() {
let inputField = document.querySelector('#typehere');
let val = inputField.value;
inputField.value = "";
let resp = await getResp(val);
let objDiv = document.getElementById("log");
objDiv.appendChild(paraWithText('😀: ' + val));
objDiv.appendChild(paraWithText('🤖: ' + resp));
objDiv.scrollTop = objDiv.scrollHeight;
};
async function getResp(val) {
let resp = await fetch("/response.json?sentence=" +
encodeURIComponent(val));
let data = await resp.json();
return data['result'];
}
</script>
"""
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("/response.json")
def response():
sentence = request.args['sentence']
return jsonify(
{'result': chatbot.response_for(sentence)})
@app.route("/")
def home():
return chatbot_html
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment