user238384 - 1 year ago 135

Java Question

I am looking for a simple java class that can compute tf-idf calculation. I want to do similarity test on 2 documents. I found so many BIG API who used tf-idf class. I do not want to use a big jar file, just to do my simple test. Please help !

Or atlest if some one can tell me how to find TF? and IDF? I will calculate the results :)

OR

If you can tell me some good java tutorial for this.

Please do not tell me for looking google, I already did for 3 days and couldn't find any thing :(

Please also do not refer me to Lucene :(

Answer

Term Frequency is the square root of the number of times a term occurs in a particular document.

Inverse Document Frequency is (the log of (the total number of documents divided by the number of documents containing the term)) plus one in case the term occurs zero times -- if it does, obviously don't try to divide by zero.

If it isn't clear from that answer, there is a TF per term per document, and an IDF per term.

And then TF-IDF(term, document) = TF(term, document) * IDF(term)

Finally, you use the vector space model to compare documents, where each term is a new dimension and the "length" of the part of the vector pointing in that dimension is the TF-IDF calculation. Each document is a vector, so compute the two vectors and then compute the distance between them.

So to do this in Java, read the file in one line at a time with a FileReader or something, and split on spaces or whatever other delimiters you want to use - each word is a term. Count the number of times each term appears in each file, and the number of files each term appears in. Then you have everything you need to do the above calculations.

And since I have nothing else to do, I looked up the vector distance formula. Here you go:

```
D=sqrt((x2-x1)^2+(y2-y1)^2+...+(n2-n1)^2)
```

For this purpose, x1 is the TF-IDF for term x in document 1.

Edit: in response to your question about how to count the words in a document:

- Read the file in line by line with a reader, like
`new BufferedReader(new FileReader(filename))`

- you can call`BufferedReader.readLine()`

in a while loop, checking for null each time. - For each line, call
`line.split("\\s")`

- that will split your line on whitespace and give you an array of all of the words. - For each word, add 1 to the word's count for the current document. This could be done using a
`HashMap`

.

Now, after computing D for each document, you will have X values where X is the number of documents. To compare all documents against each other is to do only X^2 comparisons - this shouldn't take particularly long for 10,000. Remember that two documents are MORE similar if the absolute value of the difference between their D values is lower. So then you could compute the difference between the Ds of every pair of documents and store that in a priority queue or some other sorted structure such that the most similar documents bubble up to the top. Make sense?