开发者

how do i total each file query count?

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

        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 inv开发者_StackOverflowerseTF =  Math.log10((float) numDoc
                            / (numofDoc[i]));
                    System.out.println("    --> IDF = " + inverseTF );

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





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

    }
}

This is my code to calculate the term frequency for each of the query i input inside. Now i am trying to total each query frequency for each file.

Example output :

The number of files is this folder is : 11 Please enter the query : how are you how --> This number of files that contain this term 3 are --> This number of files that contain this term 7 you --> This number of files that contain this term 7


The word inputted : how are you


| File = abc0.txt |

how --> Word count = |4| Total count = |957| Term Frequency = | 0.0042 |

--> IDF = 0.5642714398516419 --> TF/IDF = 0.0023585013159943234

are --> Word count = |7| Total count = |957| Term Frequency = | 0.0073 |

--> IDF = 0.1962946357308887 --> TF/IDF = 0.00143580193324579

you --> Word count = |10| Total count = |957| Term Frequency = | 0.0104 |

--> IDF = 0.1962946357308887 --> TF/IDF = 0.002051145618922557

Example : The total frequency is 4 + 7 + 10 = 21..


The word inputted : how are you


| File = abc1.txt |

how --> Word count = |4| Total count = |959| Term Frequency = | 0.0042 |

--> IDF = 0.5642714398516419 --> TF/IDF = 0.0023535826479734803

are --> Word count = |7| Total count = |959| Term Frequency = | 0.0073 |

--> IDF = 0.1962946357308887 --> TF/IDF = 0.0014328075600794795

you --> Word count = |10| Total count = |959| Term Frequency = | 0.0104 |

--> IDF = 0.1962946357308887 --> TF/IDF = 0.002046867942970685

How can i make it to total the 3 queries WORD COUNT for each file ?

Example : The total frequency is 4 + 7 + 10 = 21..


the totalcount must be outside of your try. initialise it before your try and print it after. There are a lot concerns about the design of your Java program, I hope you will think about that stuff, too. For the time beeing, probably that should be all you need:

for (a = 0; a < filename; a++) {
  int totalcount = 0;
  try{
    int wordcount = 0;
    for(...){
      ...
    }
    //print wordcount
    totalcount += wordcount;
  }catch(Exception e){
    ...
    return; //to ensure that there is no total count if something goes wrong.
  }
  //print totacount
}


You need to store the wordcount in an array ( for each file) or you could add it to some "sum" variable (which is initialized outside the loop)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜