我现在正在尝试制作类似小型搜索引擎的产品。我的目标是在哈希图中索引一堆文件,但首先我需要执行一些操作,包括降低资本,删除所有不必要的单词以及删除除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);
}
这只是我的代码的一个块,它可能包含缺少的部分,我用注释粗略地解释了我的算法。使用.contains方法检查stopWords是否包含任何currentWord,尽管这是一种较快的方法,但它不会映射“死亡”之类的单词,因为它包含停用词列表中的“ at”。 我试图尽最大努力使它更有效,但是我进步不大。每个文件包含大约大约300个单词每个索引都需要大约3秒才能完成索引,考虑到我有一万个文件,这是不理想的。关于如何改善算法以使其运行更快的任何想法?