所以,除了修剪功能之外,一切都工作正常。不知道好不好用。
实际上我正在使用ArrayList创建倒排索引。从文件中获取行,拆分为单词(标记化)并删除标点符号。当我到达删除停用词的部分时,它与文件中提供的停用词不匹配。我认为这是因为ArrayList的每个索引的前导空格。
停用词列表:
a, is, the, of, all, and, to, can, be, as, once, for, at, am, are, has, have, had, up, his, her, in, on, no, we, do
我的策略:
String [] TempArray = new String[word.size()];
for (int i = 0; i < word.size(); i++) {
word.set(i, word.get(i).toLowerCase());
word.set(i, word.get(i).replaceAll("[^a-zA-Z]", ""));
}
for (int i = 0; i < word.size(); i++) {
TempArray[i] = word.get(i);
TempArray[i] = TempArray[i].trim();
word.set(i, TempArray[i]);
System.out.println(word.get(i));
}
输出:
[a, blunder, by, anton, chekhov, , an, illustration, for, the, story, a, blunder, by, the, author, anton, chekhov, portrait, of, ivan, lazhechnikov...]
如有任何建议,我们将不胜感激!
最佳答案
怎么样:
Arrays.stream(TempArray)
.filter(s -> s != null)
.map(String::trim)
.toArray(String[]::new);
这将创建没有前导或尾随空格的新数组。
关于java - 删除ArrayList中每个索引的前导空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55109034/