User9999
User9999

Reputation: 87

How to retrieve regex search alphanumeric text in a PDF document using java Apache Lucene?

** I want to search alphanumeric text(Invoice No. F0000004511) from PDF document using regex from Java . how can I achive this ? for example the PDF first page is like this:

Sales - Invoice T.I.N. No. 02020600021 Fax No. +91-1792-232268 Invoice No. F0000004511

In PDF second page invoice no changes to F0000004512 and third and fourth page with same number. I need search and split the pdf page according to invoice no. I'm using APACHE LUCENE 3.4.0 for indexing and searching pdf. below code for indexing 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 :(");
        }
    }
}

below code for search in index. here I'm directly searching through regex. but is it possible to search using regex values throughout all pdf and read the Invoice No.. Finally i need split pdf according to Invoice No. i need to return Invoice no value from regex and split tht pdf. (SOurce pdf have 60 pages with unique and repeated Invoice No.)

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");
    } 
}

Upvotes: 1

Views: 345

Answers (1)

Stephan
Stephan

Reputation: 43053

Solution proposed by femtoRgon:

Well, you appear to be using the QueryParser to generate your queries in Lucene version 3.4. Regex support was not added to the QueryParser until, I believe, version 4.0. To search with a regex, you would need to manually construct a RegexQuery.

Upvotes: 0

Related Questions