【问题标题】:aiml file does not exist! why my android project cannot connect to aiml files in assets folder?目标文件不存在!为什么我的 android 项目无法连接到 assets 文件夹中的 aiml 文件?
【发布时间】:2014-10-24 10:30:57
【问题描述】:

我正在尝试使用程序 AB、aiml 和 android studio 构建一个聊天机器人。 我把我所有的目标文件放在资产文件夹中:像这样assets/bots/alice2/aiml files(including sets, maps, aiml, aimlif and config) 然后我创建我的机器人并尝试通过这段代码连接到它:

    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);

但似乎连接到 aiml 文件时出现问题,因为无论我作为请求发送什么,我收到的唯一响应都是集成在其中的“我对此没有答案”。

我在 logcat 中看到的是,该项目一开始可以找到 aiml 文件,但后来它说它们不存在!有谁知道为什么会这样? 以下是 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.**

【问题讨论】:

    标签: android aiml


    【解决方案1】:

    这是因为资产文件夹下不能有任何子目录。

    您可以做的是,您可以保留一个压缩文件夹并在使用前将其解压缩到外部存储中。

    创建文件夹“bots”的压缩包并粘贴到资产中。

    并使用以下代码:


    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();
            }
        }
    }
    


    使用此代码将 zip 文件从资产文件夹提取到外部存储:

    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(); }
    }
    


    您的 bots 文件夹路径将是:

    String path = getExternalFilesDir(null).getAbsolutePath();
    

    【讨论】:

    • 很棒的家伙。我最近对弄清楚如何让 Program AB 也能在 Android 上运行变得很感兴趣。今天终于用上面的 zip 文件提取代码搞定了。
    • HappyBot:play.google.com/store/apps/details?id=pack.turret.happybot 试试这个。使用 ProgramAB 制作。
    • 不错的设计!所以,我在Android上探索Program AB的早期阶段github.com/bradleybossard/android-program-ab-demo你的代码是开源的吗?如果没有,只是对您加载 Program AB 库感到好奇……您是在后台任务中执行的吗?您是否为您的机器人使用了比 Alice 2.0 更简单的 AIML 定义,您的 b/c 似乎加载速度更快?
    • 感谢您的补充。我已经在一个线程中加载了 AB。因为它是 android,我已经从 aiml 文件中删除了很多东西,比如 javascript 库、联系人等,这些在 android 中不起作用。关于那个开源的东西,我会尽快上传到 github 并分享给你。
    • 非常有帮助的 Anupam!我最近也开始探索 ProgramAB,并通过你和 Bradley 的代码让它在 Android 上部分工作。有一些小问题,如加载时间等,会破坏应用程序的流程。您是否将源代码上传到 github 或其他任何我们可以查看的地方?再次感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    相关资源
    最近更新 更多