我的java tf-idf程序没有读取它应该的文件

时间:2018-05-16 11:46:40

标签: java filereader 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

0 个答案:

没有答案