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

counting the number of occurences of each word in a pdf file java

我正在使用 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();
   }

  }

我已经尝试解决了这个逻辑。

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();
        }
    }
}

试试这个代码。如果还有问题,您无法解决。我可以尝试解决。

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

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

在 while 循环中 st.hasMoreTokens() 并渲染每个单词 String word = clean(st.nextToken()); 这也工作正常。