Java使用PDFBox提取PDF文本并統(tǒng)計(jì)關(guān)鍵詞出現(xiàn)的次數(shù)
1. 基本知識(shí)
Apache PDFBox 是一個(gè)開源的 Java PDF 操作庫(kù),支持:
讀取 PDF 文件內(nèi)容(包括文字、圖片、元數(shù)據(jù))
創(chuàng)建和修改 PDF 文檔
提取文本內(nèi)容用于搜索、分析等操作
Maven相關(guān)的依賴:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.29</version>
</dependency>
需下載 在進(jìn)行統(tǒng)計(jì):
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.File;
import java.io.IOException;
public class PDFWordCounter {
public static void main(String[] args) {
String pdfPath = "sample.pdf"; // 替換為你的 PDF 文件路徑
String keyword = "Java"; // 要統(tǒng)計(jì)的詞語
try {
// 加載 PDF 文檔
PDDocument document = PDDocument.load(new File(pdfPath));
// 使用 PDFTextStripper 提取文本
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
document.close(); // 記得關(guān)閉文檔資源
// 轉(zhuǎn)小寫處理,方便忽略大小寫
String lowerText = text.toLowerCase();
String lowerKeyword = keyword.toLowerCase();
// 調(diào)用詞頻統(tǒng)計(jì)函數(shù)
int count = countOccurrences(lowerText, lowerKeyword);
System.out.println("詞語 \"" + keyword + "\" 出現(xiàn)次數(shù): " + count);
} catch (IOException e) {
e.printStackTrace();
}
}
// 使用 indexOf 遍歷匹配詞語出現(xiàn)次數(shù)
private static int countOccurrences(String text, String word) {
int count = 0;
int index = 0;
while ((index = text.indexOf(word, index)) != -1) {
count++;
index += word.length();
}
return count;
}
}
上述的Demo詳細(xì)分析下核心知識(shí):
PDDocument.load(File)
用于加載 PDF 文件到內(nèi)存中
PDFBox 使用 PDDocument 表示整個(gè) PDF 對(duì)象,使用完后必須調(diào)用 close() 釋放資源PDFTextStripper
PDFBox 中用于提取文字的核心類,會(huì)盡可能“以閱讀順序”提取文本,適用于純文字 PDF 文件。對(duì)于圖像型掃描件則無效(需 OCR)大小寫不敏感統(tǒng)計(jì)
實(shí)際應(yīng)用中搜索關(guān)鍵詞通常需要忽略大小寫,因此我們先統(tǒng)一將文本和關(guān)鍵詞轉(zhuǎn)換為小寫indexOf 實(shí)現(xiàn)詞頻統(tǒng)計(jì)
這是最基礎(chǔ)也最直觀的統(tǒng)計(jì)方法,效率較高,但不夠精確
如果需要更精確(只統(tǒng)計(jì)完整單詞),可以使用正則:
Pattern pattern = Pattern.compile("\\b" + Pattern.quote(word) + "\\b", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
int count = 0;
while (matcher.find()) {
count++;
}
2. 在線URL
2.1 英文
此處的Demo需要注意一個(gè)點(diǎn):
| 注意點(diǎn) | 說明 |
|---|---|
| PDF 文件是否公開訪問 | 不能訪問受密碼或登錄保護(hù)的 PDF |
| 文件大小 | 不建議下載和分析過大文件,可能導(dǎo)致內(nèi)存問題 |
| 中文 PDF | 若是掃描圖片形式的中文 PDF,則 PDFBox 無法直接提取文本(需 OCR) |
| 編碼問題 | 若中文顯示為亂碼,可能是 PDF 沒有內(nèi)嵌字體 |
思路:
通過 URL.openStream() 獲取在線 PDF 的輸入流
使用 PDFBox 的 PDDocument.load(InputStream) 讀取 PDF
用 PDFTextStripper 提取文本
用字符串方法或正則統(tǒng)計(jì)關(guān)鍵詞頻率
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.InputStream;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class OnlinePDFKeywordCounter {
public static void main(String[] args) {
String pdfUrl = "https://www.example.com/sample.pdf"; // 你的在線 PDF 鏈接
String keyword = "Java"; // 需要統(tǒng)計(jì)的關(guān)鍵詞
try (InputStream inputStream = new URL(pdfUrl).openStream();
PDDocument document = PDDocument.load(inputStream)) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
// 使用正則匹配單詞邊界(忽略大小寫)
Pattern pattern = Pattern.compile("\\b" + Pattern.quote(keyword) + "\\b", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
int count = 0;
while (matcher.find()) {
count++;
}
System.out.println("詞語 \"" + keyword + "\" 出現(xiàn)在在線 PDF 中的次數(shù)為: " + count);
} catch (Exception e) {
System.err.println("處理 PDF 時(shí)出錯(cuò): " + e.getMessage());
e.printStackTrace();
}
}
}
2.2 混合
| 方法 | 適用場(chǎng)景 | 是否支持中文 |
|---|---|---|
indexOf | 中英文都適用 | ? |
Pattern + \\b | 僅限英文單詞匹配 | ? 中文不支持 |
正則表達(dá)式 \\b...\\b(表示“單詞邊界”)并不適用于中文
統(tǒng)計(jì)在想的URL PDF的詞頻:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.InputStream;
import java.net.URL;
public class OnlinePDFKeywordCounter {
public static void main(String[] args) {
String pdfUrl = "https://www.xxxx.pdf";
String keyword = "管理層"; // 要統(tǒng)計(jì)的中文關(guān)鍵詞
try (InputStream inputStream = new URL(pdfUrl).openStream();
PDDocument document = PDDocument.load(inputStream)) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
// 直接用 indexOf 不區(qū)分大小寫(對(duì)于中文沒必要轉(zhuǎn)小寫)
int count = countOccurrences(text, keyword);
System.out.println("詞語 \"" + keyword + "\" 出現(xiàn)次數(shù)為: " + count);
} catch (Exception e) {
System.err.println("處理 PDF 時(shí)出錯(cuò): " + e.getMessage());
e.printStackTrace();
}
}
// 簡(jiǎn)單統(tǒng)計(jì)子串出現(xiàn)次數(shù)(適用于中文)
private static int countOccurrences(String text, String keyword) {
int count = 0;
int index = 0;
while ((index = text.indexOf(keyword, index)) != -1) {
count++;
index += keyword.length();
}
return count;
}
}
截圖如下:

以上就是Java使用PDFBox提取PDF文本并統(tǒng)計(jì)關(guān)鍵詞出現(xiàn)的次數(shù)的詳細(xì)內(nèi)容,更多關(guān)于Java PDFBox提取PDF文本并統(tǒng)計(jì)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Springboot整合Flowable6.x導(dǎo)出bpmn20的步驟詳解
這篇文章主要介紹了Springboot整合Flowable6.x導(dǎo)出bpmn20,Flowable流程引擎可用于部署B(yǎng)PMN 2.0流程定義,可以十分靈活地加入你的應(yīng)用/服務(wù)/構(gòu)架,本文給出兩種從flowable導(dǎo)出流程定義bpmn20.xml的方式,需要的朋友可以參考下2023-04-04
關(guān)于解決iReport4.1.1無法正常啟動(dòng)或者閃退或者JDK8不兼容的問題
在安裝使用iReport的過程中遇到一個(gè)問題,我的iReport始終不能打開,困擾了我好久。接下來通過本文給大家介紹iReport4.1.1無法正常啟動(dòng)或者閃退或者JDK8不兼容的問題,需要的朋友可以參考下2018-09-09
SpringBoot設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的參考價(jià)值,需要的可以參考一下2022-06-06
IDEA配置SpringBoot熱啟動(dòng),以及熱啟動(dòng)失效問題
這篇文章主要介紹了IDEA配置SpringBoot熱啟動(dòng),以及熱啟動(dòng)失效問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
詳解Spring Boot使用redis實(shí)現(xiàn)數(shù)據(jù)緩存
本篇文章主要介紹了詳解Spring Boot使用redis實(shí)現(xiàn)數(shù)據(jù)緩存,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04

