Skip to content

Instantly share code, notes, and snippets.

View jcabot's full-sized avatar

Jordi Cabot jcabot

View GitHub Profile
@app.route('/import_wp')
def import_data_blog():
# Connect to Elasticsearch (locally installed)
document_store = ElasticsearchDocumentStore(host="localhost", username="", password="", index="blogposts")
# convert files to dicts containing documents that can be indexed to our datastore
dicts = convert_sql_to_dicts(True)
# You can optionally supply a cleaning function that is applied to each doc (e.g. to remove footers)
# It must take a str as input, and return a str.
# Now, let's write the docs to our DB.
@jcabot
jcabot / BotForHaystack.java
Created December 20, 2021 05:35
Chatbot that tries to answer unexpected questions by trying to find an answer on a set of documents
/*
* We send the query to haystack hoping for an answer
*/
val defaultFallback = fallbackState()
.body(context -> {
String question = context.getIntent().getMatchedInput();
if (question != null) {
HttpResponse<JsonNode> response = null;
ReactPlatform reactPlatform = new ReactPlatform();
ReactEventProvider reactEventProvider = reactPlatform.getReactEventProvider();
ReactIntentProvider reactIntentProvider = reactPlatform.getReactIntentProvider();
awaitingInput
.next()
.when(intentIs(greetings)).moveTo(handleWelcome)
.when(intentIs(AnyOtherThing)).moveTo(handleAnyOtherThing);
handleWelcome
.body(context -> reactPlatform.reply(context, "Hello World!"))
.next()
/*
* A transition that is automatically navigated: in this case once we have answered the user we
val greetings = intent("Greetings")
.trainingSentence("Hi")
.trainingSentence("Hello")
.trainingSentence("Good morning")
.trainingSentence("Good afternoon");
@jcabot
jcabot / Giphy-state.java
Created November 7, 2020 16:52
Core state for the Giphy bot
handleCanYou
.body(context ->
{
String request = (String) context.getIntent().getValue("request");
if(isNull(request) || request.isEmpty()) {
reactPlatform.reply(context, "Sorry, I didn't get it, could you rephrase?");
}
else {
String url = giphyPlatform.getGif(context, request);
reactPlatform.reply(context, "Sure! \n![look](" + url + ")");
@jcabot
jcabot / Giphy-intent.java
Created November 7, 2020 16:43
Main intent for the Giphy bot
val canYou = intent("CanYou")
.trainingSentence("Can you dance?")
.trainingSentence("Have you seen Scarface?")
.trainingSentence("Do you know Jim Carrey?")
.trainingSentence("Any thoughts about politics?")
.trainingSentence("What do you think about testing?")
.trainingSentence("dance?")
.trainingSentence("Can you dance")
.trainingSentence("Have you seen Scarface")
printWeather
.body(context -> {
String cityName = (String) context.getIntent().getValue("cityName");
Map<String, Object> queryParameters = new HashMap<>();
queryParameters.put("q", cityName);
ApiResponse<JsonElement> response = restPlatform.getJsonRequest(context, "http://api" +
".openweathermap.org/data/2.5/weather", queryParameters, Collections.emptyMap(), Collections.emptyMap());
if (response.getStatus() == 200) {
long temp = Math.round(response.getBody().getAsJsonObject().get("main").getAsJsonObject().get("temp").getAsDouble());
long tempMin =
awaitingInput
.next()
.when(intentIs(howIsTheWeather)).moveTo(printWeather);
@jcabot
jcabot / WeatherBot_intent.java
Created October 25, 2020 18:42
Weather bot built with the Xatkit platform
val howIsTheWeather = intent("HowIsTheWeather")
.trainingSentence("How is the weather today in CITY?")
.trainingSentence("What is the forecast for today in CITY?")
.parameter("cityName").fromFragment("CITY").entity(city());