0

我正在尝试使用程序 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.**
4

1 回答 1

4

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

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

创建文件夹“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 文件从 assets 文件夹提取到外部存储:

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();
于 2014-11-17T12:43:14.853 回答