gpt4 book ai didi

java - 如何总结总值(value)?

转载 作者:行者123 更新时间:2023-12-01 15:54:45 24 4
gpt4 key购买 nike

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



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

}

这是示例输出:

输入的词:你好吗

<小时/>

|文件 = abc0.txt |

如何 --> 字数 = |4|总计数 = |957|词频 = | 0.0042 | 0.0042

--> IDF = 0.5642714398516419

--> TF/IDF = 0.0023585013159943234

是 --> 字数 = |7|总计数 = |957|词频 = | 0.0073 |

--> IDF = 0.1962946357308887

--> TF/IDF = 0.00143580193324579

你 --> 字数 = |10|总计数 = |957|词频 = | 0.0104 | 0.0104

--> IDF = 0.1962946357308887

--> TF/IDF = 0.002051145618922557

我如何总结每个文本文件的整个 3 TF/IDF?

最佳答案

假设您只想显示运行总计,那么在 for 循环 之前添加如下内容:

double runningTfIDF = 0;

然后在计算当前的 TF/IDF 后立即添加该行

runningTfIDF += TFIDF;

然后,在 for 循环 之后,您可以添加一行来打印 runningTfIDF。

编辑以包含更完整的答案

HashMap<String, BigDecimal> runningTdIDF = new HashMap<String, Double>();
HashMap<String, BigDecimal> wordCount = new HashMap<String, Double>();
HashMap<String, BigDecimal> frequency = new HashMap<String, Double>();
HashMap<String, BigDecimal> inverseTF = new HashMap<String, Double>();
for (int i = 0; i < array2.length; i++) {

totalCount = 0;
wordCountVal = 0;

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

}

BigDecimal wordCount(array2[i],new BigDecimal(wordCountVal));

BigDecimal frequencyVal = new BigDecimal( (double) wordCount / totalCount));
frequency.put(array2[i],frequencyVal);

BigDecimal inverseTFVal = new BigDecimal(Math.log10((float) numDoc
/ (numofDoc[i])));
inverseTF.put(array2[i], inverseTFVal);


BigDecaim TFIDF =new BigDecima( (( wordCount / totalCount) * inverseTF));
runningTfIDF.put(array2[i], TFIDF);

}

for(String word : wordCount.keySet()){
System.out.print(word + " --> word count "
+ "\t |"+wordCount.get(word)+"|");
System.out.print(" Total count = " + "\t " + "|"
+ totalCount + "|");
System.out.printf(" Term Frequency = | %8.4f |",
frequency.get(word));

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

System.out.println(" --> IDF = " + inverseTF.get(word));

System.out.println(" --> TF/IDF = " + runningTfIDF.get(word) + "\n");
}

}

这并不是迄今为止最干净的实现,但简而言之,如果您想从第一个可能的开始显示总计,您需要存储每个单词的信息,并在创建总计后循环显示单词结果。这有道理吗?

关于java - 如何总结总值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5298489/

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