【个人工作记录】

public class PDFTextExtractionTest {

    private PDFTextExtraction textExtractionService;
    private File testFile;

    @BeforeEach
    public void setUp() {
        // 初始化服务实现类
        textExtractionService = new PDFTextExtractionImpl();

        // 设置测试文件路径
        try {
            testFile = ResourceUtils.getFile("classpath:legal_data/PDF/test01.pdf");
            System.out.println("文件路径: " + testFile.getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testExtractSelectableText() throws IOException {
        // 提取可选择文本
        String text = textExtractionService.extractSelectableText(testFile);
        // 断言文本不为空
        assertNotNull(text, "Selectable text should not be null");
        // 断言文本长度大于0
        assertTrue(text.length() > 0, "Selectable text should not be empty");
        // 输出提取的文本
        System.out.println("Selectable Text:\n" + text);
    }

    @Test
    public void testExtractImageText() throws IOException, TesseractException {
        // 提取图像中的文本
        String text = textExtractionService.extractImageText(testFile);
        // 断言文本不为空
        assertNotNull(text, "Image text should not be null");
        // 断言文本长度大于0
        assertTrue(text.length() > 0, "Image text should not be empty");
        // 输出提取的文本
        System.out.println("Image Text:\n" + text);
    }

    @Test
    public void testExtractFullText() throws IOException, TesseractException {
        // 提取完整文本
        String text = textExtractionService.extractFullText(testFile);
        // 断言文本不为空
        assertNotNull(text, "Full text should not be null");
        // 断言文本长度大于0
        assertTrue(text.length() > 0, "Full text should not be empty");
        // 输出提取的文本
        System.out.println("Full Text (Selectable + Image):\n" + text);
    }
    
    @Test
    public void testExtractImageTextM() throws IOException, TesseractException, InterruptedException, ExecutionException {
        try {
            // 多线程提取图像中的文本
            String text = textExtractionService.extractImageTextM(testFile);
            // 断言文本不为空
            assertNotNull(text, "Image text (multi-threaded) should not be null");
            // 断言文本长度大于0
            assertTrue(text.length() > 0, "Image text (multi-threaded) should not be empty");
            // 输出提取的文本
            System.out.println("Image Text (multi-threaded):\n" + text);
        } catch (RuntimeException e) {
            // 处理异常情况
            System.err.println("Failed to process extractImageTextM: " + e.getMessage());
            e.printStackTrace();
        }
    }

    @Test
    public void testExtractImageTextWithoutWhitespace() throws IOException, TesseractException {
        // 提取图像中的文本并去除空白
        String text = textExtractionService.extractImageTextWithoutWhitespace(testFile);
        // 断言文本不为空
        assertNotNull(text, "Image text without whitespace should not be null");
        // 断言文本长度大于0
        assertTrue(text.length() > 0, "Image text without whitespace should not be empty");
        // 输出提取的文本
        System.out.println("Image Text without Whitespace:\n" + text);
    }
}

1. 初始化服务实现类
  
   @BeforeEach
   public void setUp() {
       // 初始化服务实现类
       textExtractionService = new PDFTextExtractionImpl();

       // 设置测试文件路径
       try {
           testFile = ResourceUtils.getFile("classpath:legal_data/PDF/test01.pdf");
           System.out.println("文件路径: " + testFile.getPath());
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
   }
 

2. 测试提取可选择文本

   @Test
   public void testExtractSelectableText() throws IOException {
       // 提取可选择文本
       String text = textExtractionService.extractSelectableText(testFile);
       // 断言文本不为空
       assertNotNull(text, "Selectable text should not be null");
       // 断言文本长度大于0
       assertTrue(text.length() > 0, "Selectable text should not be empty");
       // 输出提取的文本
       System.out.println("Selectable Text:\n" + text);
   }
 

3. 测试提取图像中的文本

   @Test
   public void testExtractImageText() throws IOException, TesseractException {
       // 提取图像中的文本
       String text = textExtractionService.extractImageText(testFile);
       // 断言文本不为空
       assertNotNull(text, "Image text should not be null");
       // 断言文本长度大于0
       assertTrue(text.length() > 0, "Image text should not be empty");
       // 输出提取的文本
       System.out.println("Image Text:\n" + text);
   }
 

4. 测试提取完整文本
   @Test
   public void testExtractFullText() throws IOException, TesseractException {
       // 提取完整文本
       String text = textExtractionService.extractFullText(testFile);
       // 断言文本不为空
       assertNotNull(text, "Full text should not be null");
       // 断言文本长度大于0
       assertTrue(text.length() > 0, "Full text should not be empty");
       // 输出提取的文本
       System.out.println("Full Text (Selectable + Image):\n" + text);
   }
  

5. 测试多线程提取图像中的文本
  
   @Test
   public void testExtractImageTextM() throws IOException, TesseractException, InterruptedException, ExecutionException {
       try {
           // 多线程提取图像中的文本
           String text = textExtractionService.extractImageTextM(testFile);
           // 断言文本不为空
           assertNotNull(text, "Image text (multi-threaded) should not be null");
           // 断言文本长度大于0
           assertTrue(text.length() > 0, "Image text (multi-threaded) should not be empty");
           // 输出提取的文本
           System.out.println("Image Text (multi-threaded):\n" + text);
       } catch (RuntimeException e) {
           // 处理异常情况
           System.err.println("Failed to process extractImageTextM: " + e.getMessage());
           e.printStackTrace();
       }
   }
   ```

6. 测试提取图像中的文本并去除空白

   @Test
   public void testExtractImageTextWithoutWhitespace() throws IOException, TesseractException {
       // 提取图像中的文本并去除空白
       String text = textExtractionService.extractImageTextWithoutWhitespace(testFile);
       // 断言文本不为空
       assertNotNull(text, "Image text without whitespace should not be null");
       // 断言文本长度大于0
       assertTrue(text.length() > 0, "Image text without whitespace should not be empty");
       // 输出提取的文本
       System.out.println("Image Text without Whitespace:\n" + text);
   }
   

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值