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 4055036
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:39:03+00:00 2026-05-20T14:39:03+00:00

// Calculating term frequency int filename = 11; String[] fileName = new String[filename]; int

  • 0
// Calculating term frequency
int filename = 11;
String[] fileName = new String[filename];
int a = 0;
int totalCount = 0;
int wordCount = 0;


// Count inverse document frequency

System.out.println("Please enter the required word  :");
Scanner scan2 = new Scanner(System.in);
String word2 = scan2.nextLine();
String[] array2 = word2.split(" ");
int numofDoc;

for (int b = 0; b < array2.length; b++) {

    numofDoc = 0;

    for (int i = 0; i < filename; i++) {

        try {

            BufferedReader in = new BufferedReader(new FileReader(
                           "C:\\Users\\user\\fypworkspace\\TextRenderer\\abc"
                           + i + ".txt"));

            int matchedWord = 0;

            Scanner s2 = new Scanner(in);

            {

                while (s2.hasNext()) {
                    if (s2.next().equals(array2[b]))
                        matchedWord++;
                }

            }
            if (matchedWord > 0)
                numofDoc++;

        } catch (IOException e) {
            System.out.println("File not found.");
        }

    }
    System.out.println(array2[b]
                       + " --> This number of files that contain the term  "
                       + numofDoc);


    //calculate TF-IDF
    for (a = 0; a < filename; a++) {

        try {
            System.out.println("The word inputted : " + word2);
            File file =
                new File("C:\\Users\\user\\fypworkspace\\TextRenderer\\abc"
                         + a + ".txt");
            System.out.println(" _________________");

            System.out.print("| File = abc" + a + ".txt | \t\t \n");

            for (int i = 0; i < array2.length; i++) {

                totalCount = 0;
                wordCount = 0;

                Scanner s = new Scanner(file);
                {
                    while (s.hasNext()) {
                        totalCount++;
                        if (s.next().equals(array2[i]))
                            wordCount++;

                    }

                    System.out.print(array2[i] + " --> Word count =  "
                                     + "\t\t " + "|" + wordCount + "|");
                    System.out.print("  Total count = " + "\t\t " + "|"
                                     + totalCount + "|");
                    System.out.printf("  Term Frequency =  | %8.4f |",
                                      (double) wordCount / totalCount);

                    System.out.println("\t ");

                    double inverseTF = Math.log10((float) numDoc / numofDoc);
                    System.out.println("    --> IDF " +  inverseTF );

                    double TFIDF = (((double) wordCount / totalCount) * inverseTF );
                    System.out.println("    --> TF/IDF " + TFIDF);
                }
            }
        } catch (FileNotFoundException e) { 
            System.out.println("File is not found");
        }
    }
}

When i input a string, lets say ‘how’, the code will search for the number of files that contain the string ‘how’.

For example output :

The number of files containing 'how' is 5.

Then the code will proceed to calculate the term frequency-inverse document frequency.

When i input 3 strings, such as ‘how are you’.

The output will only display for only the string ‘how’.

Example output :

Please enter the required word  :
you

you --> This number of files that contain the term  6

The word inputted : you

 _________________
| File = abc0.txt |          
you --> Word count =         |3|  Total count =          |150|  Term Frequency =  |   0.0200 |   
    --> IDF 0.2632414441876607
    --> TF/IDF 0.005264828883753215

The word inputted : you

If i input 3 strings : ‘how are you’

Please enter the required word  :
how are you
how --> This number of files that contain the term  6

<— It will only process first string which is ‘how’

The word inputted : how are you
 _________________
| File = abc0.txt |          
how --> Word count =         |0|  Total count =          |150|  Term Frequency =  |   0.0000 |   
    --> IDF Infinity
    --> TF/IDF NaN

are --> Word count =         |0|  Total count =          |150|  Term Frequency =  |   0.0000 |   
    --> IDF Infinity
    --> TF/IDF NaN

you --> Word count =         |3|  Total count =          |150|  Term Frequency =  |   0.0200 |   
    --> IDF Infinity
    --> TF/IDF Infinity

Then the rest of the string will only use ONE number of files which is 0. Each string suppose to have their own individual NUMBER OF FILES.

How to make the code to receive 3 different NUMBER OF FILES ?

  • 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-20T14:39:03+00:00Added an answer on May 20, 2026 at 2:39 pm

    In order to count the number of documents per searchterm, you could use an int array to keep the counts:

    String[] array2 = word2.split(" ");
    int[] numofDoc = new int[array2.length];
    
    for (int b = 0; b < array2.length; b++) {
    
        numofDoc[b] = 0;
    

    use the array element when counting:

                if (matchedWord > 0) {
                    numofDoc[b]++;
                }
    

    and later use the array elements to calculate:

                double inverseTF = Math.log10((float) numDoc / numofDoc[i]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

// Calculating term frequency System.out.println(Please enter the required word :); Scanner scan = new
// Calculating term frequency System.out.println(Please enter the required word :); Scanner scan = new
System.out.println(Please enter the required word :); Scanner scan2 = new Scanner(System.in); String word2 =
Without calculating them , I mean?
While calculating the hash table bucket index from the hash code of a key,
I need some help calculating Pi. I am trying to write a python program
Does anyone have a decent algorithm for calculating axis minima and maxima? When creating
What is the best approach to calculating the largest prime factor of a number?
What's the fastest and easiest to read implementation of calculating the sum of digits?
Given two integers a and b , how would I go about calculating the

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.