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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:28:12+00:00 2026-05-20T13:28:12+00:00

// Calculating term frequency System.out.println(Please enter the required word :); Scanner scan = new

  • 0
// Calculating term frequency
    System.out.println("Please enter the required word  :");
    Scanner scan = new Scanner(System.in);
    String word = scan.nextLine();

    String[] array = word.split(" ");
    int filename = 11;
    String[] fileName = new String[filename];
    int a = 0;
    int totalCount = 0;
    int wordCount = 0;


    for (a = 0; a < filename; a++) {

        try {
            System.out.println("The word inputted is " + word);
            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 < array.length; i++) {

                totalCount = 0;
                wordCount = 0;

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

                    }

                    System.out.print(array[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 ");

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

        }

    }

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);
        double inverseTF = Math.log10((float) numDoc / numofDoc);
        System.out.println(array2[b] + " --> IDF " +  inverseTF );
        double TFIDF = (((double) wordCount / totalCount) * inverseTF );
        System.out.println(array2[b] + " --> TFIDF " + TFIDF);
    }
}

Hi, this is my code for calculating term frequency and TF-IDF. The first code calculates the term frequency for each file of a given string. The second code is supposed to calculate TF-IDF for each file using the value from the above. But I only received one value. It’s supposed to provide TF-IDF value for each document.

Example output for term frequency :

The word input is ‘is’


| File = abc0.txt |
is —> Word count = |2| Total count = |150| Term Frequency = | 0.0133 |

The word inputted is ‘is’


| File = abc1.txt |
is —> Word count = |0| Total count = |9| Term Frequency = | 0.0000 |

The TF-IDF

is –> This number of files that contain the term 7

is –> IDF 0.1962946357308887

is –> TFIDF 0.0028607962606519654 <<< I suppose to get one value per file, means that i have 10 files, it suppose to give me 10 different values for each different file. But, it only prints one result only. Can someone point my mistake?

  • 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-20T13:28:12+00:00Added an answer on May 20, 2026 at 1:28 pm

    The part that prints the TDIDF needs to be moved inside the for loop that loops over all the files.

    ie:

        System.out.println(array2[b]
                + " --> This number of files that contain the term  "
                + numofDoc);
        double inverseTF = Math.log10((float) numDoc / numofDoc);
        System.out.println(array2[b] + " --> IDF " +  inverseTF );
        double TFIDF = (((double) wordCount / totalCount) * inverseTF );
        System.out.println(array2[b] + " --> TFIDF " + TFIDF);
    }
    

    }
    }

    • 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
System.out.println(Please enter the required word :); Scanner scan2 = new Scanner(System.in); String word2 =
// Calculating term frequency int filename = 11; String[] fileName = new String[filename]; int
So I am calculating an equation so that I can echo out a statistic.
Is it known what is the fastest Java algorithm for calculating nth term of
Calculating the area of a simple irregular polygon is trivial . However, consider the
I am calculating the Correct height of my Dynamic textView . But i am
Possible Duplicate: Calculating the Difference Between Two Java Date Instances How do I get
I am calculating something that takes some time. For that I've created an inner
I have algorithm of calculating average speed in pure python: speed = [...] avg_speed

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.