【发布时间】:2020-03-06 08:35:23
【问题描述】:
我现在正在尝试制作类似迷你搜索引擎的东西。我的目标是在哈希图中索引一堆文件,但首先我需要执行一些操作,包括降低大写字母、删除所有不必要的单词以及删除除 a-z/A-Z 之外的所有字符。现在我的实现看起来像这样:
String article = "";
for (File file : dir.listFiles()) { //for each file (001.txt, 002.txt...)
Scanner s = null;
try {
s = new Scanner(file);
while (s.hasNext())
article += s.next().toLowerCase(Locale.ROOT) + " "; //converting all characters to lower case
article = currentWord.replaceAll(delimiters.get()," "); //removing punctuations (?, -, !, * etc...)
String splittedWords = article.split(" "); //splitting each word into a string array
for(int i = 0; i < splittedWords.length; i++) {
s = new Scanner(stopwords);
boolean flag = true;
while(s.hasNextLine())
if (splittedWords[i].equals(s.nextLine())) { //comparing each word with all the stop words (words like a, the, already, these etc...) taken from another big txt file and removing them, because we dont need to fill our map with unnecessary words, to provide faster search times later on
flag = false;
break;
}
if(flag) map.put(splittedWords[i], file.getName()); //if current word in splittedWords array does not match any stop word, put it in the hashmap
}
s.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
s.close();
System.out.println(file);
}
这只是我代码中的一个块,它可能包含缺失的部分,我用 cmets 粗略地解释了我的算法。使用 .contains 方法检查 stopWords 是否包含任何 currentWord,尽管它是一种更快的方法,但它不会映射像“death”这样的单词,因为它包含停用词列表中的“at”。 我尽我所能让它更有效,但我没有太大进展。每个文件包含大约。考虑到我有一万个文件,每个约 300 个字需要约 3 秒来索引,这并不理想。关于如何改进我的算法以使其运行更快的任何想法?
【问题讨论】:
-
您正在读取每个源文件的停用词文件。您可以读取一次停用词文件并使用
Set将停用词存储在内存中。
标签: java string eclipse indexing hashmap