211 Star 1.3K Fork 516

GVPofdrw / ofdrw

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
KeywordExtractorTest.java 3.04 KB
一键复制 编辑 原始数据 按行查看 历史
package org.ofdrw.reader.keyword;
import org.dom4j.DocumentException;
import org.junit.jupiter.api.Test;
import org.ofdrw.core.basicType.ST_Box;
import org.ofdrw.reader.OFDReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* 关键字抽取 调用示例
*
* @author 权观宇
* @since 2020-10-13 19:27:52
*/
class KeywordExtractorTest {
@Test
void testKeyword() throws IOException, DocumentException {
Path src = Paths.get("src/test/resources/multiKeywordInTextCode.ofd");
String keyword = "打发";
try (OFDReader reader = new OFDReader(src)) {
List<KeywordPosition> positionList = KeywordExtractor.getKeyWordPositionList(reader, keyword);
assertEquals(7, positionList.size());
}
}
/**
* 获取关键字在文档中坐标
*/
@Test
void getKeyWordPositionList() throws IOException, DocumentException {
Path src = Paths.get("src/test/resources/keyword.ofd");
String keyword = "办理";
try (OFDReader reader = new OFDReader(src)) {
List<KeywordPosition> positionList = KeywordExtractor.getKeyWordPositionList(reader, keyword);
assertEquals(positionList.size(), 1);
KeywordPosition keywordPos = positionList.get(0);
assertEquals(keywordPos.getPage(), 1);
ST_Box box = keywordPos.getBox();
for (KeywordPosition pos : positionList) {
System.out.println(pos);
}
// // 误差保持在0.1
// assertEquals("131.9", String.format("%.1f", box.getTopLeftX()));
// assertEquals("87.9", String.format("%.1f", box.getTopLeftY()));
// assertEquals("22.3", String.format("%.1f", box.getWidth()));
// assertEquals("22.3", String.format("%.1f", box.getHeight()));
}
}
/**
* 获取关键字在文档中坐标
*/
@Test
void getMultiKeyWordPositionList() throws IOException, DocumentException {
Path src = Paths.get("src/test/resources/keyword.ofd");
String[] keywords = {"办理", "不动产权"};
try (OFDReader reader = new OFDReader(src)) {
List<KeywordPosition> positionList = KeywordExtractor.getKeyWordPositionList(reader, keywords);
assertEquals(positionList.size(), 3);
for (KeywordPosition keywordPosition : positionList) {
System.out.println(keywordPosition);
}
}
}
@Test
void testKeyword2() throws IOException, DocumentException {
Path src = Paths.get("src/test/resources/keyword2.ofd");
String keyword = "马上融";
try (OFDReader reader = new OFDReader(src)) {
List<KeywordPosition> positionList = KeywordExtractor.getKeyWordPositionList(reader, keyword);
for (KeywordPosition keywordPosition : positionList) {
assertEquals(keyword, keywordPosition.getKeyword());
}
}
}
}
Java
1
https://gitee.com/ofdrw/ofdrw.git
git@gitee.com:ofdrw/ofdrw.git
ofdrw
ofdrw
ofdrw
master

搜索帮助