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

  • SEARCH
  • Home
  • 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 4080908
In Process

The Archive Base Latest Questions

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

int queryVector = 1; double similarity = 0.0; int wordPower; String[][] arrays = new

  • 0
int queryVector = 1;
    double similarity = 0.0;
    int wordPower;
    String[][] arrays = new String[filename][2];
    int row;
    int col;


    for (a = 0; a < filename; a++) {
        int totalwordPower = 0;
        int totalWords = 0;
        try {
            System.out
                    .println(" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  ");
            System.out.println("\n");
            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 " + "|" + wordCount + "|");
                    System.out.print("  Total count = " + "\t " + "|"
                            + totalCount + "|");
                    System.out.printf("  Term Frequency =  | %8.4f |",
                            (double) wordCount / totalCount);

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

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

                    double TFIDF = (((double) wordCount / totalCount) * inverseTF);
                    System.out.println("    --> TF/IDF = " + TFIDF + "\n");

                    totalWords += wordCount;

                    wordPower = (int) Math.pow(wordCount, 2);

                    totalwordPower += wordPower;

                    System.out.println("Document Vector : " + wordPower);

                    similarity = (totalWords * queryVector)
                            / ((Math.sqrt((totalwordPower)) * (Math
                                    .sqrt(((queryVector * 3))))));



                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("File is not found");
        }
        System.out.println("The total query frequency for this file is "
                + totalWords);
        System.out.println("The total document vector : " + totalwordPower);

        System.out.println("The similarity is " + similarity);
    }
}

}

Hi i wanted to sort the SIMILARITY SCORE calculated from the code above. This is an example output of 2 text files. I have total of 10 text files together.

The word inputted : how are you


| File = abc0.txt |
how –> Word count = |0| Total count = |1289| Term Frequency = | 0.0000 |
–> IDF = 1.0413926851582251
–> TF/IDF = 0.0

Document Vector : 0
are –> Word count = |0| Total count = |1289| Term Frequency = | 0.0000 |
–> IDF = 0.43933269383026263
–> TF/IDF = 0.0

Document Vector : 0
you –> Word count = |0| Total count = |1289| Term Frequency = | 0.0000 |
–> IDF = 0.1962946357308887
–> TF/IDF = 0.0

Document Vector : 0
The total query frequency for this file is 0
The total document vector : 0
The SIMILARITY is NaN


The word inputted : how are you


| File = abc1.txt |
how –> Word count = |0| Total count = |426| Term Frequency = | 0.0000 |
–> IDF = 1.0413926851582251
–> TF/IDF = 0.0

Document Vector : 0
are –> Word count = |0| Total count = |426| Term Frequency = | 0.0000 |
–> IDF = 0.43933269383026263
–> TF/IDF = 0.0

Document Vector : 0
you –> Word count = |3| Total count = |426| Term Frequency = | 0.0070 |
–> IDF = 0.1962946357308887
–> TF/IDF = 0.0013823565896541458

Document Vector : 9
The total query frequency for this file is 3
The total document vector : 9
The SIMILARITY is 0.5773502691896257

Note : This is example run of two text files. I have a total of 10 text files.

How to sort the SIMILARITY score from the highest to the lowest? Any advices?

  • 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-20T17:59:28+00:00Added an answer on May 20, 2026 at 5:59 pm

    Add the SIMILARITY scores to a list and sort using library method. It sorts in ascending order, you can read it from the end.

    ArrayList<Double> arrayList = new ArrayList<Double>();
    Collections.sort(arrayList);
    

    Or you can declare a Comparator and use it like below.

    ArrayList<Double> arrayList = new ArrayList<Double>();
    Comparator<Double> comparator = Collections.reverseOrder();
    Collections.sort(arrayList,comparator);
    

    HTH

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

Sidebar

Related Questions

int placement[] = new int[8]; int placeQueen(int row) { if(row == 8) { printBoard();
int main(void) { char tmp, arr[100]; int i, k; printf(Enter a string: ); scanf_s(%s,
int somefunction(bool a) { try { if(a) throw Error(msg); return 2; } catch (Error
int main(void) { std::string foo(foo); } My understanding is that the above code uses
int length = strlen(src); char *structSpace = malloc(sizeof(String) + length + 1); String *string
int velMperMin = 667; int distM = 70; double movT = (distM/velMperMin)*60; movtT must
int i =132; byte b =(byte)i; System.out.println(b); Mindboggling. Why is the output -124 ?
int? test; try { test = (int?) Int32.Parse (7); } catch {} if (test
int numcells = 2; foreach (System.IO.FileInfo fi in fileQuery) { Label1.Text = fileList.Count().ToString(); TableRow
int countConsonant(string str, int consonant) { int length = str.length(); consonant = 0; for

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.