【问题标题】:How to retrieve regex search alphanumeric text in a PDF document using java Apache Lucene?如何使用 java Apache Lucene 在 PDF 文档中检索正则表达式搜索字母数字文本?
【发布时间】:2015-08-13 12:53:45
【问题描述】:

** 我想使用 Java 中的正则表达式从 PDF 文档中搜索字母数字文本(发票编号 F0000004511)。我怎样才能做到这一点?例如PDF第一页是这样的:

销售 - 发票 T.I.N. No. 02020600021 Fax No. +91-1792-232268 Invoice No. F0000004511

在 PDF 的第二页发票中,F0000004512 没有变化,第三页和第四页的编号相同。我需要根据发票编号搜索并拆分 pdf 页面。我正在使用 APACHE LUCENE 3.4.0 来索引和搜索 pdf。以下代码用于索引 pdf**

public class Indexer {

    private final String sourceFilePath = "G:/PDFCopy";    //give the location of the source files location here
    private final String indexFilePath = "G:/searchEngine";   //give the location where you guys want to create index
    private IndexWriter writer = null;
    private File indexDirectory = null;
    private String fileContent;  //temp storer of all the text parsed from doc and pdf 


    private Indexer() throws FileNotFoundException, CorruptIndexException, IOException {
        try {
            long start = System.currentTimeMillis();
            createIndexWriter();
            checkFileValidity();
            closeIndexWriter();
            long end = System.currentTimeMillis();
            System.out.println("Total Document Indexed : " + TotalDocumentsIndexed());
            System.out.println("Total time" + (end - start) / (100 * 60));
        } catch (Exception e) {
            System.out.println("Sorry task cannot be completed");
        }
    }


    private void createIndexWriter() {
        try {
            indexDirectory = new File(indexFilePath);
            if (!indexDirectory.exists()) {
                indexDirectory.mkdir();
            }
            FSDirectory dir = FSDirectory.open(indexDirectory);
            StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_34);
            IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, analyzer);
            writer = new IndexWriter(dir, config);
        } catch (Exception ex) {
            System.out.println("Sorry cannot get the index writer");
        }
    }


    private void checkFileValidity() {

        File[] filesToIndex = new File[100]; // suppose there are 100 files at max
        filesToIndex = new File(sourceFilePath).listFiles();
        for (File file : filesToIndex) {
            try {
                //to check whenther the file is a readable file or not.
                if (!file.isDirectory()
                        && !file.isHidden()
                        && file.exists()
                        && file.canRead()
                        && file.length() > 0.0
                        && file.isFile() ) {
                    if(file.getName().endsWith(".txt")){
                        indexTextFiles(file);//if the file text file no need to parse text. 
                    System.out.println("INDEXED FILE " + file.getAbsolutePath() + " :-) ");
                    }
                    else if(file.getName().endsWith(".doc") || file.getName().endsWith(".pdf")){
                        //different methof for indexing doc and pdf file.
                       StartIndex(file);                    
                    }
                }
            } catch (Exception e) {
                System.out.println("Sorry cannot index " + file.getAbsolutePath());
            }
        }
    }



    public void StartIndex(File file) throws FileNotFoundException, CorruptIndexException, IOException {
         fileContent = null;
        try {
            Document doc = new Document();
            if (file.getName().endsWith(".doc")) {
                //call the doc file parser and get the content of doc file in txt format
                fileContent = new DocFileParser().DocFileContentParser(file.getAbsolutePath());
            }
            if (file.getName().endsWith(".pdf")) {
                //call the pdf file parser and get the content of pdf file in txt format
                fileContent = new PdfFileParser().PdfFileParser(file.getAbsolutePath());
            }
            doc.add(new Field("content", fileContent,
                    Field.Store.YES, Field.Index.ANALYZED,
                    Field.TermVector.WITH_POSITIONS_OFFSETS));
            doc.add(new Field("filename", file.getName(),
                    Field.Store.YES, Field.Index.ANALYZED));
            doc.add(new Field("fullpath", file.getAbsolutePath(),
                    Field.Store.YES, Field.Index.ANALYZED));
            if (doc != null) {
                writer.addDocument(doc);
            }
            System.out.println("Indexed" + file.getAbsolutePath());
        } catch (Exception e) {
            System.out.println("error in indexing" + (file.getAbsolutePath()));
        }
    }


    private void indexTextFiles(File file) throws CorruptIndexException, IOException {
        Document doc = new Document();
        doc.add(new Field("content", new FileReader(file)));
        doc.add(new Field("filename", file.getName(),
                Field.Store.YES, Field.Index.ANALYZED));
        doc.add(new Field("fullpath", file.getAbsolutePath(),
                Field.Store.YES, Field.Index.ANALYZED));
        if (doc != null) {
            writer.addDocument(doc);
        }
    }


    private int TotalDocumentsIndexed() {
        try {
            IndexReader reader = IndexReader.open(FSDirectory.open(indexDirectory));
            return reader.maxDoc();
        } catch (Exception ex) {
            System.out.println("Sorry no index found");
        }
        return 0;
    }


    private void closeIndexWriter() {
        try {
            writer.optimize();
            writer.close();
        } catch (Exception e) {
            System.out.println("Indexer Cannot be closed");
        }
    }

    public static void main(String arg[]) {
        try {
            new Indexer();
        } catch (Exception ex) {
            System.out.println("Cannot Start :(");
        }
    }
}

下面是在索引中搜索的代码。在这里,我直接通过正则表达式进行搜索。但是否可以在所有 pdf 中使用正则表达式值进行搜索并阅读发票编号。最后我需要根据发票编号拆分 pdf。 我需要从正则表达式返回发票没有值并拆分 tht pdf。 (来源 pdf 有 60 页,具有唯一且重复的发票编号)

public class Searcher {

    public Searcher(String searchString) {
        try {
            IndexSearcher searcher = new IndexSearcher(FSDirectory.open(
                    new File("G:/searchEngine")));
            Analyzer analyzer1 = new StandardAnalyzer(Version.LUCENE_34);
            QueryParser queryParser = new QueryParser(Version.LUCENE_34, "content", analyzer1);
            QueryParser queryParserfilename = new QueryParser(Version.LUCENE_34, "fullpath", analyzer1);
            Query query = queryParser.parse(searchString);//to search in the content
            Query queryfilename = queryParserfilename.parse(searchString);//to search the file name only        
            TopDocs hits = searcher.search(query, 10000); //for 
            ScoreDoc[] document = hits.scoreDocs;
            System.out.println("Total no of hits for content: " + hits.totalHits);


            for (int i = 0; i < document.length; i++) {
                Document doc = searcher.doc(document[i].doc);
                String filePath = doc.get("fullpath");
                System.out.println(filePath);
            }


        } catch (Exception e) {
        }

    }

    public static void main(String args[])
    {
       new Searcher("Invoice No.\\s\\w\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d");
    } 
}

【问题讨论】:

  • 好吧,您似乎正在使用 QueryParser 在 Lucene 版本 3.4 中生成查询。我相信 QueryParser 直到 4.0 版才添加正则表达式支持。要使用正则表达式进行搜索,您需要手动构造 RegexQuery

标签: java regex lucene pdfbox


【解决方案1】:

femtoRgon提出的解决方案:

嗯,您似乎正在使用 QueryParser 在 Lucene 版本 3.4 中生成查询。我相信 QueryParser 直到 4.0 版才添加正则表达式支持。要使用正则表达式进行搜索,您需要手动构造 RegexQuery

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    相关资源
    最近更新 更多