Comments (6)
Indeed, it feels like a hack, because threading should be done at the (your) application level, not in this library. This lib should not have to care about the threading use case of the caller application.
The same way you have to make HTTP requests in a background thread, you need to make chatter-bot-api calls in a background thread.
from chatter-bot-api.
Now that I'm more coherent, I can actually make a recommendation.
I would still recommend making an interface that allows the user to create a "listener" for the event. In my case, where there's thread jumping, it allows for an easy access point back to the main thread, and in most cases, I'd imagine the user would want networking off the main thread anyway. Any time you're working with asynchronous you'll want a listener of some sort.
You could make this an inline interface in the base interface like this:
public interface ThoughtListener() {
public void onThoughtCompleted(String thought);
}
Give your bots a data member: thoughtListener, and make a mutator for it.
Then instead of having think(String text) return a String, make it void, and call:
thoughtListener.onThoughtCompleted(think(thought).getText);
Then the user implements onThoughtCompleted() with something like:
bot1.setOnThoughtListener(new OnThoughtListener() {
void onThoughtCompleted(String text) {
// do things with text
}
});
This makes it easier to run it on a separate thread, which arguably all networking should be done on, and should have little impact on other users once they create the listener. For reference, this is a standard design pattern with listeners on Android.
I can pull up a PR some time later with an example some time when I get a spare couple of minutes. Worst case scenario, there's this post people working on Android or working asynchronously can see so they know how to deal with it without hacking something together like I did on an hours of sleep. 😆
from chatter-bot-api.
The listener pattern you talk about, will not make call magically asynchronous.
Consider this actual code:
public String think(String text) throws Exception {
ChatterBotThought thought = new ChatterBotThought();
thought.setText(text);
return think(thought).getText();
}
Then, transformed into this one you suggest:
...
private ThoughtListener listener;
public void setListener(ThoughtListener listener) { this.listener = listener; }
...
public void think(String text) throws Exception {
ChatterBotThought thought = new ChatterBotThought();
thought.setText(text);
listener.onThoughtCompleted(think(thought).getText());
}
The listener.onThoughtCompleted()
call will be executed on whatever thread the void think(String text)
method was call on.
from chatter-bot-api.
Why not just use the tools provided by Android?
I am not an Android guru, but a little search on Google, and I found you can do something like this:
public void onClick(View v) {
// start a background thread
new Thread(new Runnable() {
public void run() {
// call chatterbot on the background thread
final String responseText = botSession.think(myRequestTextView.getText());
// queue a runnable on the main thread
myResponseTextView.post(new Runnable() {
public void run() {
// update the ui with the chatterbot response on the main thread
myResponseTextView.setText(responseText);
}
});
}
}).start();
}
EDIT: Here is the link where I found inspiration for this snippet.
from chatter-bot-api.
Ah, good call. Everything looks like a nail that I can just hit with a listener after working with nothing but Android all weekend.
This definitely doesn't need any changes, and I still need some sleep. Thanks for being patient with me.
from chatter-bot-api.
Thanks to you for your comments and your interest in my project.
Do not hesitate if you have other questions.
from chatter-bot-api.
Related Issues (20)
- always has a b' or a b" HOT 5
- Getting 404 error from API HOT 8
- Cleverbot down HOT 2
- Pandora bot down HOT 1
- not support chinese question? HOT 1
- Default constructor cannot handle exception type Exception thrown by implicit super constructor. Must define an explicit constructor HOT 4
- Cleverbot Webservice Link 404's HOT 13
- Cannot connect to the webserver of cleverbot HOT 11
- Change language of reply. HOT 3
- webservice error? HOT 8
- UC variable changed again HOT 5
- Hang on startup HOT 4
- CleverBot is not longer working
- org.xml.sax.SAXParseException; Premature end of file. HOT 3
- Any update on this? HOT 4
- The element type "hr" error -- JAVA HOT 3
- Java version wont work anymore HOT 1
- Issue running chatterbotapitest.py
- Cleverbot - HTTP/1.1 401 Unauthorized HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chatter-bot-api.