Java使用PDFBox提取PDF文本并統(tǒng)計關(guān)鍵詞出現(xiàn)的次數(shù)
1. 基本知識
Apache PDFBox 是一個開源的 Java PDF 操作庫,支持:
讀取 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>
需下載 在進行統(tǒng)計:
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)計的詞語 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)計函數(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詳細分析下核心知識:
PDDocument.load(File)
用于加載 PDF 文件到內(nèi)存中
PDFBox 使用 PDDocument 表示整個 PDF 對象,使用完后必須調(diào)用 close() 釋放資源PDFTextStripper
PDFBox 中用于提取文字的核心類,會盡可能“以閱讀順序”提取文本,適用于純文字 PDF 文件。對于圖像型掃描件則無效(需 OCR)大小寫不敏感統(tǒng)計
實際應(yīng)用中搜索關(guān)鍵詞通常需要忽略大小寫,因此我們先統(tǒng)一將文本和關(guān)鍵詞轉(zhuǎn)換為小寫indexOf 實現(xiàn)詞頻統(tǒng)計
這是最基礎(chǔ)也最直觀的統(tǒng)計方法,效率較高,但不夠精確
如果需要更精確(只統(tǒng)計完整單詞),可以使用正則:
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需要注意一個點:
注意點 | 說明 |
---|---|
PDF 文件是否公開訪問 | 不能訪問受密碼或登錄保護的 PDF |
文件大小 | 不建議下載和分析過大文件,可能導(dǎo)致內(nèi)存問題 |
中文 PDF | 若是掃描圖片形式的中文 PDF,則 PDFBox 無法直接提取文本(需 OCR) |
編碼問題 | 若中文顯示為亂碼,可能是 PDF 沒有內(nèi)嵌字體 |
思路:
通過 URL.openStream() 獲取在線 PDF 的輸入流
使用 PDFBox 的 PDDocument.load(InputStream) 讀取 PDF
用 PDFTextStripper 提取文本
用字符串方法或正則統(tǒng)計關(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)計的關(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 時出錯: " + e.getMessage()); e.printStackTrace(); } } }
2.2 混合
方法 | 適用場景 | 是否支持中文 |
---|---|---|
indexOf | 中英文都適用 | ? |
Pattern + \\b | 僅限英文單詞匹配 | ? 中文不支持 |
正則表達式 \\b...\\b
(表示“單詞邊界”)并不適用于中文
統(tǒng)計在想的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)計的中文關(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ū)分大小寫(對于中文沒必要轉(zhuǎn)小寫) int count = countOccurrences(text, keyword); System.out.println("詞語 \"" + keyword + "\" 出現(xiàn)次數(shù)為: " + count); } catch (Exception e) { System.err.println("處理 PDF 時出錯: " + e.getMessage()); e.printStackTrace(); } } // 簡單統(tǒng)計子串出現(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)計關(guān)鍵詞出現(xiàn)的次數(shù)的詳細內(nèi)容,更多關(guān)于Java PDFBox提取PDF文本并統(tǒng)計的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringMVC中的ResourceUrlProviderExposingInterceptor詳解
這篇文章主要介紹了SpringMVC中的ResourceUrlProviderExposingInterceptor詳解,ResourceUrlProviderExposingInterceptor是Spring MVC的一個HandlerInterceptor,用于向請求添加一個屬性,需要的朋友可以參考下2023-12-12java代碼規(guī)范之不合理命名與重復(fù)代碼示例詳解
這篇文章主要為大家介紹了java代碼規(guī)范之不合理命名與重復(fù)代碼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09Java實戰(zhàn)之圖書管理系統(tǒng)的實現(xiàn)
這篇文章主要介紹了如何利用Java語言編寫一個圖書管理系統(tǒng),文中采用的技術(shù)有Springboot、SpringMVC、MyBatis、ThymeLeaf 等,需要的可以參考一下2022-03-03Java 實戰(zhàn)范例之線上婚紗攝影預(yù)定系統(tǒng)的實現(xiàn)
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+javaweb+SSM+springboot+mysql實現(xiàn)一個線上婚紗攝影預(yù)定系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11深入淺析springsecurity入門登錄授權(quán)
SpringSecurity為我們提供了基于注解的權(quán)限控制方案,這也是我們項目中主要采用的方式,我們可以使用注解去指定訪問對應(yīng)的資源所需的權(quán)限,這篇文章主要介紹了springsecurity入門登錄授權(quán),需要的朋友可以參考下2024-05-05spring實現(xiàn)靜態(tài)注入(類或者屬性)操作示例
這篇文章主要為大家介紹了spring實現(xiàn)靜態(tài)注入(類或者屬性)操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07BufferedReader中read()方法和readLine()方法的使用
這篇文章主要介紹了BufferedReader中read()方法和readLine()方法的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04