首页 > 解决方案 > 我的 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

标签: javafilereadertf-idf

解决方案


推荐阅读