0

I am trying to build a chatbot using program AB, aiml and android studio. I put all my aiml files in assets folder : something like this assets/bots/alice2/aiml files(including sets, maps, aiml, aimlif and config) then I create my bot and try to connect to it by this code:

    String botname="alice2";
    String path = "file:///android_asset";
    Bot alice2 = new Bot(botname,path);

    Chat chatSession = new Chat(alice2);

    String request = mEdit.getText().toString();
    String response = chatSession.multisentenceRespond(request);
    ((Button)v).setText(response);

but there seems to be a problem connecting to aiml files cause whatever I send as a request the only response I receive is "I HAVE NO ANSWER FOR THAT" which is integrated in it.

What I See in the logcat is that the project can find the aiml files at first but then it says they don't exist! does anyone know why this happens? Here's what's in logcat:

8611-8611/com.example.myapplication I/System.out? Name = alice2 Path = file:///android_asset/bots/alice2
10-24 13:04:46.162    8611-8611/com.example.myapplication I/System.out? c:/ab
10-24 13:04:46.162    8611-8611/com.example.myapplication I/System.out? **file:///android_asset/bots**
10-24 13:04:46.162    8611-8611/com.example.myapplication I/System.out? file:///android_asset/bots/**alice2**
10-24 13:04:46.162    8611-8611/com.example.myapplication I/System.out? file:///android_asset/bots/alice2/**aiml**
10-24 13:04:46.162    8611-8611/com.example.myapplication I/System.out? file:///android_asset/bots/alice2/**aimlif**
10-24 13:04:46.162    8611-8611/com.example.myapplication I/System.out? file:///android_asset/bots/alice2/**config**
10-24 13:04:46.162    8611-8611/com.example.myapplication I/System.out? file:///android_asset/bots/alice2/**logs**
10-24 13:04:46.162    8611-8611/com.example.myapplication I/System.out? file:///android_asset/bots/alice2/**sets**
10-24 13:04:46.162    8611-8611/com.example.myapplication I/System.out? file:///android_asset/bots/alice2/**maps**
10-24 13:04:46.162    8611-8611/com.example.myapplication I/System.out? Preprocessor: 0 norms 0 persons 0 person2
10-24 13:04:46.162    8611-8611/com.example.myapplication I/System.out? Get Properties: file:///android_asset/bots/alice2/config/**properties.txt**
10-24 13:04:46.172    8611-8611/com.example.myapplication I/System.out? addAIMLSets: file:///android_asset/bots/alice2/**sets does not exist.**
10-24 13:04:46.172    8611-8611/com.example.myapplication I/System.out? Loaded 0 set elements.
10-24 13:04:46.172    8611-8611/com.example.myapplication I/System.out? addAIMLMaps: file:///android_asset/bots/alice2/**maps does not exist.**
10-24 13:04:46.172    8611-8611/com.example.myapplication I/System.out? Loaded 0 map elements
10-24 13:04:46.172    8611-8611/com.example.myapplication I/System.out? Read pronouns: []
10-24 13:04:46.222    8611-8611/com.example.myapplication I/System.out? AIML modified Thu Jan 01 03:30:00 GMT+03:30 1970 AIMLIF modified Thu Jan 01 03:30:00 GMT+03:30 1970
10-24 13:04:46.222    8611-8611/com.example.myapplication I/System.out? addCategoriesFromAIMLIF: file:///android_asset/bots/alice2/**aimlif does not exist.**
10-24 13:04:46.222    8611-8611/com.example.myapplication I/System.out? Loaded 0 categories in 0.002 sec
10-24 13:04:46.222    8611-8611/com.example.myapplication I/System.out? **No AIMLIF Files found.**  Looking for AIML
10-24 13:04:46.222    8611-8611/com.example.myapplication I/System.out? addCategoriesFromAIML: file:///android_asset/bots/alice2/**aiml does not exist.**
Kaiido
  • 123,334
  • 13
  • 219
  • 285
sara
  • 1
  • 3

1 Answers1

4

This is due to the fact that you cannot have any subdirectories under assets folder.

What you can do is that you may keep a zipped folder and extract it to the external storage before using.

Create a zip of the folder "bots" and paste in assets.

And use the code below:


ZipFileExtraction.java

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.util.Log;

public class ZipFileExtraction
{
    public void unZipIt(InputStream zipFile, String outputFolder)
    {
        try
        {
            //get the zip file content
            ZipInputStream zin = new ZipInputStream(zipFile);
            //get the zipped file list entry
            ZipEntry entry = null;
            int bytesRead;
            byte[] buffer = new byte[4096];

            while ((entry = zin.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    File dir = new File(outputFolder, entry.getName());
                    if (!dir.exists()) {
                        dir.mkdir();
                    }
                    Log.d("+++++++++++Zip Extractor", "[DIR] "+entry.getName());
                } else {
                    FileOutputStream fos = new FileOutputStream(outputFolder + entry.getName());
                    while ((bytesRead = zin.read(buffer)) != -1) {
                        fos.write(buffer, 0, bytesRead);
                    }
                    fos.close();
                    Log.d("+++++++++++Zip Extractor", "[FILE] "+entry.getName());
                }
            }
            zin.close();
            System.out.println("Done");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}


Use this code to extract the zip file from assets folder to external storage :

File fileExt = new File(getExternalFilesDir(null).getAbsolutePath()+"/bots");

if(!fileExt.exists())
{
    ZipFileExtraction extract = new ZipFileExtraction();

    try
    {
        extract.unZipIt(getAssets().open("bots.zip"), getExternalFilesDir(null).getAbsolutePath()+"/");
    } catch (Exception e) { e.printStackTrace(); }
}


And your path to the bots folder will be :

String path = getExternalFilesDir(null).getAbsolutePath();
Anupam Basak
  • 1,503
  • 11
  • 13
  • Awesome dude. I recently became interesting in figuring out how to get Program AB running on an Android as well. Finally got things working today using the above zip file extraction code. – Bradley Bossard Jan 25 '15 at 01:32
  • HappyBot: https://play.google.com/store/apps/details?id=pack.turret.happybot Try this. Made it using ProgramAB. – Anupam Basak Jan 27 '15 at 18:51
  • Nice design! So, I'm in the early stages of exploring Program AB on Android https://github.com/bradleybossard/android-program-ab-demo Is your code open source? If not, just curious about your loading of the Program AB library... Did you do it in a background task? Are you using a much simpler AIML definition for your bot than, say Alice 2.0, b/c yours seem to load much faster? – Bradley Bossard Jan 27 '15 at 23:15
  • Thanks for the complement. And I've loaded AB in a thread. Since it is android, I've removed a lot of things from the aiml files like the javascript library, contacts, etc which doesn't works in android. And about that open source thing, I will be uploading in github soon and share you the link. – Anupam Basak Jan 28 '15 at 04:43
  • Very helpful Anupam! I started exploring ProgramAB recently too and got it to work partially on Android with your and Bradley's code. There are a few minor problems like loading time, etc that break the flow of the app. Did you upload the source code on github or anywhere else where we can check it out? Again, thanks a lot for your help! – EatHeat May 08 '15 at 10:51
  • Welcome.. The code is not hosted anywhere. Let's see what we can do about the code. And yes it is taking too much time to start. – Anupam Basak May 08 '15 at 13:05
  • Here is the app we made: http://play.google.com/store/apps/details?id=pack.turret.happybot Have a look and share with us if you have any feedback. – Anupam Basak May 08 '15 at 13:08
  • Will this zip file concept also work for web apps? I having trouble storing the folders inside the project and accessing it. Someone help me pls – Kavipriya Mar 27 '17 at 10:59