【发布时间】:2012-01-03 22:38:52
【问题描述】:
我在 SO 上找到了这个解决方案来检测字符串中的 n-gram: (这里:N-gram generation from a sentence)
import java.util.*;
public class Test {
public static List<String> ngrams(int n, String str) {
List<String> ngrams = new ArrayList<String>();
String[] words = str.split(" ");
for (int i = 0; i < words.length - n + 1; i++)
ngrams.add(concat(words, i, i+n));
return ngrams;
}
public static String concat(String[] words, int start, int end) {
StringBuilder sb = new StringBuilder();
for (int i = start; i < end; i++)
sb.append((i > start ? " " : "") + words[i]);
return sb.toString();
}
public static void main(String[] args) {
for (int n = 1; n <= 3; n++) {
for (String ngram : ngrams(n, "This is my car."))
System.out.println(ngram);
System.out.println();
}
}
}
=> 与毫秒相比,这段代码花费了迄今为止最长的处理时间(我的语料库检测 1-gram、2-gram、3-gram 和 4gram 需要 28 秒:4Mb 的原始文本)用于其他操作(去除停用词等)
是否有人知道 Java 中的解决方案会比上面介绍的循环解决方案更快? (我在考虑多线程,使用集合,或者可能是创造性的方法来拆分字符串......?)谢谢!
【问题讨论】:
-
您是否尝试过分析需要时间的内容?似乎创建了很多不需要的对象。是拆分需要时间还是创建 ngram 对象或将它们插入列表?
-
我会从不拆分成单独的字符串开始,然后将它们组合回来,而是扫描分隔符并只记住索引,因此对于 3gram,您可以跟踪分隔符 n、n-1、 n-2 和 n-3。 3gram 以 n-3 开始,以 n 结束。然后向前移动 n (m-3 现在是 n-2 等等。
-
谢谢@RogerLindsjö 这看起来很有希望!我用扫描仪试了一下,但我不确定你的方法是否正确。如果我跟踪最后 3 个分隔符,当我达到 n 时(在 n-3、n-2、n-1 之后),如何检索相应的 3 个单词。 AFAICS 没有扫描仪方法可以获取以前的值(如果你愿意的话,一种“scanner.previous()”!)。我没有得到什么?再次感谢!