【问题标题】:counting the number of occurences of each word in a pdf file java计算pdf文件java中每个单词的出现次数
【发布时间】:2019-04-05 07:34:07
【问题描述】:

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

  }

【问题讨论】:

    标签: java pdfbox


    【解决方案1】:

    我已经尝试解决逻辑。

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

    试试这个代码。如果仍然存在一些问题并且您无法解决。我可以尝试解决。

    【讨论】:

    • 你能帮忙吗?我还需要计算字数和字符数
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多