Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 506003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:39:22+00:00 2026-05-13T06:39:22+00:00

I am looking for a simple java class that can compute tf-idf calculation. I

  • 0

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 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:39:22+00:00Added an answer on May 13, 2026 at 6:39 am

    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:

    1. 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.
    2. For each line, call line.split("\\s") – that will split your line on whitespace and give you an array of all of the words.
    3. 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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 311k
  • Answers 311k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's no difference at all. The compiler converts the second… May 13, 2026 at 10:17 pm
  • Editorial Team
    Editorial Team added an answer Usually by using a tokeniser. The draft HTML5 specification has… May 13, 2026 at 10:17 pm
  • Editorial Team
    Editorial Team added an answer Doing any time of local network scan with Silverlight 3… May 13, 2026 at 10:17 pm

Related Questions

For a particular segment of Java code, I'd like to measure: Execution time (most
I am writing a number of small, simple applications which share a common structure
I asked a question some time ago on java 2d pathfinding... Pathfinding 2D Java
Suppose I have an interface: public interface FooInterface { public void someMethod(); } and
I am working on a Reverse Engineering school project, which requires to translate manipulate

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.