首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搜索句子并使用icepdf获取行号。

搜索句子并使用icepdf获取行号。
EN

Stack Overflow用户
提问于 2013-08-22 05:09:14
回答 1查看 209关注 0票数 1

我尝试用icepdf.And搜索句子,得到了正确的结果大多数time.But现在面临的问题是

  • 我在搜索句子时失败了,句子以一行开头,在下一行结束,。找到同样的答案有什么办法吗?我试着拆分这些句子,并搜索它们separately.But,这可能会带来更多的问题。
  • 最后,有什么方法可以让我知道行号,我在其中得到匹配的搜索键。请帮帮忙。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-03 23:35:22

循环遍历文档中的所有行并创建一个句子列表。每个句子都可以是一个WordText对象的列表。然后搜索你为找到你的句子而创建的列表。

下面是一些示例代码(到目前为止我还没有检查)来构建WordText对象列表。

代码语言:javascript
复制
ArrayList<ArrayList<WordText>> Sentences = new ArrayList<ArrayList<WordText>>;
ArrayList<WordText> currentSentence = new ArrayList<WordText>;
Document document = new Document();

// Build sentences
for (int pageNumber = 0, max = document.getNumberOfPages(); 
     pageNumber < max;     pageNumber++) {
  PageText pageText = document.getPageText(pageNumber);
  ArrayList<LineText> pageLines = pageText.getPageLines();
  for (LineText pageLine : pageLines) {
    ArrayList<WordText> words = pageLine.getWords();
    for (WordText word : words) {
      // If this is a word, and the last word was not a space, 
      // start a new sentence
      if(!word.getText().equals(" ") && currentSentence.size() > 0
         !currentSentence.get(currentSentence.size() - 1).getText().equals(" ")) {
        sentences.add(currentSentence);
        currentSentence = new ArrayList<WordText>;
      }
      // Add word to current sentnece
      currentSentence.add(word);
    }
   // Add the last sentence in
   sentences.add(currentSentence);
  }
}

如果需要对WordText列表进行排序,可以比较WordText对象y和x值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18372084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档