我正在构建倒排索引,但在检查数据库时似乎无法获得正确的频率。我到处都读到您应该使用 HashMap
,但我不太确定这是否是正确的方法。有什么想法吗?
public class Tokenize {
public static void createIndex() throws Exception{
ArrayList<Dokument> dok = new QueryHandler().getDokuments();
ArrayList<String> queries = new ArrayList<String>();
ArrayList<String> queries2 = new ArrayList<String>();
HashMap<String, Integer> frek = new HashMap<String, Integer>();
for(int d = 0; d < dok.size(); d++){
String token = "";
int frekvens = 0;
try{
Dokument document = dok.get(d);
StringTokenizer st = new StringTokenizer(document.dokument());
while (st.hasMoreTokens()) {
token = st.nextToken();
token.replaceAll("[']", "");
token.replaceAll("[,]", "");
token.replaceAll("[)]", "");
token.replaceAll("[(]", "");
token.replaceAll("[.]", "");
frekvens ++;
frek.put(token, frekvens);
queries.add("INSERT IGNORE INTO termindeks (docID, term) values ("+document.docID()+", '"+token+"')");
queries2.add("INSERT IGNORE INTO invertedindeks (term, docID, termfrekvens) values ('"+token+"', "+document.docID()+", "+ frekvens+")");
}
}
catch (Exception e) {
e.printStackTrace();
System.out.println(token);
}
}
String[] ffs = new String[queries.size()];
ffs = queries.toArray(ffs);
getDB().runQueriesIgnoreException(queries.toArray(ffs));
String[] ffs2 = new String[queries2.size()];
ffs2 = queries2.toArray(ffs2);
getDB().runQueriesIgnoreException(queries2.toArray(ffs2));
}
}
最佳答案
您应该首先获取 token 的值,递增它并再次放置它。
在循环中像这样:
Integer frekvens = frek.get(token); //remove the other frekvens as it's not needed - or find a better name for this one ;)
if( frekvens == null ) { frekvens = 0 };
frekvens++;
frek.put(token, frekvens);
关于java - 如何确定每个文档中术语的术语频率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5676951/