开发者

could not call certain variables

//Calculating term frequency
    System.out.println("The number of files is this folder is : " + numDoc);

    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;

    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++) {

                int totalCount = 0;
                int wordCount = 0;

               开发者_如何学C 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");

        }

    }
// 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(" ");

    for (int b = 0; b < array2.length; b++) {
        int 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);
    }

}

I could not calculate the TFIDF because the compiler says that wordCount does not initialize to a variable. I could not call it from above code. Any guidance ? Thank you.


wordCount is a local variable that is declared in for loop. Once the loop is over, it goes out of scope and cannot be used. Same is the problem with totalCount too. Place it before for loop, instead;

int wordCount = 0;
int totalCount = 0;
for (a = 0; a < filename; a++) {
   // ....
}


       for (int i = 0; i < array.length; i++) {
            int totalCount = 0;
            int wordCount = 0;

This defines totalCount and wordCount in the scope of that for-loop. You are trying to access these variables from outside the for-loop (down below). What you can do is more these declarations to the top, e.g. where you write String word = scan.nextLine();.


Because you initilialize the wordCount varibale in the location unreachbale to
double TFIDF = ((double) wordCount / totalCount)) * inverseTF);


wordCount is defined inside of your for loop and you're trying to access the variable outside of the said loop, this can't work.

You must move the variable definition somewhere else, for example at the beginning of your method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜