语言检测无法按预期工作

时间:2013-10-24 12:58:35

标签: java language-detection

我正在使用https://code.google.com/p/language-detection java库来检测给定文本的语言。使用的配置文件与库一起提供。然而,结果有时会出乎意料地与预期不同。代码中可能有什么问题,或者我应该重新生成配置文件?

我尝试过“ld.detect(”en“);”评论和取消评论。空白是否影响语言检测?

    LanguageDetect ld = new LanguageDetect();
    ld.init("C:\\James\\languageTest\\profiles");
    //ld.detect("en");

    String textCurrentLine;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("C:\\James\\failcases.txt"));

        while ((textCurrentLine = br.readLine()) != null) {
           System.out.println(ld.detect(textCurrentLine));

        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null) {
                br.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

以下是我得到的几句话

Communication - en
Timing - tl
none - it
user - it
No - pt
Yes - fr
user - no
generated - da
Diagnostic - it
not supported - en
supported - en
Bus Speed - en
Protocol - it

1 个答案:

答案 0 :(得分:1)

正如图书馆的FAQ所说:

  

langdetect可以处理短文吗?

     

这个库要求检测文本几乎有一定的长度   10-20个字。

     

对于包含1-10个单词的非常短的文本,它可能会返回错误的语言。

您正在尝试使用单字或双字文本,这不是此库构建的用例,因此您将得到错误的结果。

对于没有上下文的单个单词,您可以尝试将它们与您要定位的语言的词典进行匹配。

相关问题