java - 我的 java tf-idf 程序没有读取它应该读取的文件
问题描述
该程序应该读取一个文件,文件名将是 arquivo.txt,并且应该读取其内容并计算用户将插入的术语的 tf-idf,但它说找不到 arquivo.txt 文件,甚至认为它在程序的 src 文件中。
import java.io.File;
import java.util.Scanner;
import java.util.List;
public class ReadFile {
public static String readFile(String filename) {
StringBuffer sb = new StringBuffer();
try {
File file = new File(filename);
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
sb.append(input.nextLine() + "\n");
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return sb.toString();
}
public static void main(String[] args) {
String conteudo = readFile("arquivo.txt") ;
System.out.println(conteudo);
double idf = Math.log(1/1);
System.out.println("informe o termo");
Scanner in = new Scanner(System.in);
String term = in.nextLine();
System.out.println("informe o documento(arquivo/nome do arquivo)");
String doc = in.nextLine();
}
public class TFIDFCalculator {
public double tf(List <String> doc, String term) {
double result = 0;
for (String word : doc) {
if (term.equalsIgnoreCase(word))
result++;
}
return result / doc.size();
}
public double idf(List <List <String>> docs, String term) {
double n = 0;
for (List <String> doc : docs) {
for (String word : doc) {
if (term.equalsIgnoreCase(word)) {
n++;
break;
}
}
}
return Math.log(docs.size() / n);
}
public double tfidf(List <String> doc, List <List <String>> docs, String term) {
return tf(doc, term) * idf(docs, term);
}
}
}
尝试以不同的样式对代码进行编程,结果它不会显示运行选项,就像 intellij 找不到可运行的部分一样。
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public void main(String[] args) {
List wordList = new ArrayList();
String conteudo = readFile("arquivo.txt") ;
String[] parts = conteudo.split("_");
int i =0;
for (String part: parts) {
wordList.add(parts[i]);i++;
}
System.out.println(conteudo);
double idf = Math.log(1/1);
System.out.println("informe o termo");
Scanner in = new Scanner(System.in);
String term = in.nextLine();
System.out.println("informe o documento(arquivo/nome do arquivo)");
String doc = in.nextLine();
double actualTfidf = tfidf(wordList,wordList, "work");
System.out.println("the tfidf is "+ actualTfidf);
}
//methods
private double tfidf(List <String> doc, List <List <String>> docs, String term) {
return tf(doc, term) * idf(docs, term);
}
public static String readFile(String filename) {
StringBuffer sb = new StringBuffer();
try {
File file = new File(filename);
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
sb.append(input.nextLine() + "\n");
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return sb.toString();
}
public double idf(List <List <String>> docs, String term) {
double n = 0;
for (List <String> doc : docs) {
for (String word : doc) {
if (term.equalsIgnoreCase(word)) {
n++;
break;
}
}
}
return Math.log(docs.size() / n);
}
public double tf(List <String> doc, String term) {
double result = 0;
for (String word : doc) {
if (term.equalsIgnoreCase(word))
result++;
}
return result / doc.size();
}
}
enter code here
解决方案
推荐阅读
- javascript - 单击后退按钮时如何记住上一页的滚动位置
- java - 如何在 Android Studio 的 Expandablelistview 的子布局中使用 Listview
- angular - 如何以相同的控件名称控制每个反应形式?
- dataweave - 需要根据其他列中的值填充列中的自定义序列索引,以便在 Mulesoft 中使用 Data Weave 进行流动
- python - Opencv,Python - 如何删除日期文本周围的灰色像素
- android - Flutter Firebase 登录在发布到 Play 商店后无法正常工作,但注册工作正常
- r - R:小标题转换
- python - 在 dm 中发送问题,并将答案嵌入到频道 discord.py
- javascript - 在嵌套类中访问“上层类”方法的好方法?
- ruby-on-rails - Factory_bot,如何在孩子既属于父母又属于父母的关系中重用父工厂?