计算pdf文件java中每个单词的出现次数

时间:2018-11-01 04:17:41

标签: java pdfbox

我正在使用PDFbox制作一个Java程序,该程序可以读取任何pdf文件并计算每个单词在文件中出现的次数,但是由于某种原因,当我运行该程序时什么也没出现,我希望它可以打印出每个单词以及该单词在其旁边的出现。提前致谢。 这是我的代码:

package lab8;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import java.util.Scanner;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class Extractor {


public static void main(String[] args) throws FileNotFoundException {
    Map<String, Integer> frequencies = new TreeMap<String, Integer>();
    PDDocument pd;
    File input = new File("C:\\Users\\Ammar\\Desktop\\Application.pdf"); 
    Scanner in = new Scanner(input);
    try {
        pd = PDDocument.load(input);
        PDFTextStripper stripper = new PDFTextStripper();
        stripper.setEndPage(20);
        String text = stripper.getText(pd);

        while (in.hasNext()) {
            String word = clean(in.next());

            if (word != "") {
                Integer count = frequencies.get(word);



                if (count == null) {
                    count = 1;
                } else {
                    count = count + 1;
                }

                frequencies.put(word, count);
            }
        }

        for (String key : frequencies.keySet()) {
            System.out.println(key + ": " + frequencies.get(key));
        }

        if (pd != null) {
            pd.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
   }

    private static String clean(String s) {
    String r = "";
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (Character.isLetter(c)) {
            r = r + c;
        }
    }
    return r.toLowerCase();
   }

  }

2 个答案:

答案 0 :(得分:2)

我试图解决逻辑。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class Extractor {

    public static void main(String[] args) throws FileNotFoundException {
        Map<String, Integer> wordFrequencies = new TreeMap<String, Integer>();
        Map<Character, Integer> charFrequencies = new TreeMap<Character, Integer>();
        PDDocument pd;
        File input = new File("C:\\Users\\Ammar\\Desktop\\Application.pdf");
        try {
            pd = PDDocument.load(input);
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setEndPage(20);
            String text = stripper.getText(pd);
            for(int i=0; i<text.length(); i++)
            {
                char c = text.charAt(i);
                int count = charFrequencies.get(c) != null ? (charFrequencies.get(c)) + 1 : 1;
                charFrequencies.put(c, count);
            }
            String[] texts = text.split(" ");
            for (String txt : texts) {
                int count = wordFrequencies.get(txt) != null ? (wordFrequencies.get(txt)) + 1 : 1;
                wordFrequencies.put(txt, count);

            }

            System.out.println("Printing the number of words");
            for (String key : wordFrequencies.keySet()) {
                System.out.println(key + ": " + wordFrequencies.get(key));
            }

            System.out.println("Printing the number of characters");
            for (char charKey : charFrequencies.keySet()) {
                System.out.println(charKey + ": " + charFrequencies.get(charKey));
            }

            if (pd != null) {
                pd.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

尝试此代码。如果仍然有问题,您将无法解决。我可以尝试解决。

答案 1 :(得分:0)

在您的代码中,您也可以通过传递字符串即使用StringTokenizer的对象

StringTokenizer st = new StringTokenizer(stripper.getText(pd));

在while循环st.hasMoreTokens()中并渲染每个单词String word = clean(st.nextToken());,这也很好。