gpt4 book ai didi

java - 无法调用某些变量

转载 作者:行者123 更新时间:2023-11-29 07:20:32 25 4
gpt4 key购买 nike

//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;

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);
}

}

我无法计算 TFIDF,因为编译器说 wordCount 没有初始化为变量。我无法从上面的代码中调用它。任何指导?谢谢。

最佳答案

wordCount 是在 for 循环中声明的局部变量。一旦循环结束,它就会超出范围,无法使用。 totalCount 也存在同样的问题。将它放在 for 循环之前;

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

关于java - 无法调用某些变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5274379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com