为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

阿晨1998 2021-12-22 17:56:39
我正在使用 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 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

我试图解决这个逻辑。


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

        }

    }

}

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


查看完整回答
反对 回复 2021-12-22
?
慕标5832272

TA贡献1966条经验 获得超4个赞

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

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

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


查看完整回答
反对 回复 2021-12-22
  • 2 回答
  • 0 关注
  • 373 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号