iCodeLikeImDrunk
iCodeLikeImDrunk

Reputation: 17806

In need of a clear example on how to get the word count of DOC and DOCX files

I am able to read a DOC file and get its word count, BUT it is wrong.

My code:

 public class WordCounter {
    public static void main(String[] args) throws Throwable {
        processDOC();
    }

    private static void processDOC() throws Throwable {
        File file = new File("/Users/yjiang/Desktop/whatever.doc");
        File file2 = new File("/Users/yjiang/Desktop/Test.docx");
        File file3 = new File("/Users/yjiang/Desktop/QB Tests 4-14-2014.xls");
        File file4 = new File("/Users/yjiang/Desktop/QB Tests 4-14-2014.xlsx");

        try {
            FileInputStream fs = new FileInputStream(file);
            POIFSFileSystem poifsFileSystem = new POIFSFileSystem(fs);
            DirectoryEntry directoryEntry = poifsFileSystem.getRoot();
            DocumentEntry documentEntry = (DocumentEntry) directoryEntry.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            DocumentInputStream dis = new DocumentInputStream(documentEntry);
            PropertySet ps = new PropertySet(dis);
            SummaryInformation si = new SummaryInformation(ps);

            System.out.println(si.getWordCount());
        } catch (Exception e) {
            e.printStackTrace();
        }


        try {
            HWPFDocument hwpfDocument = new HWPFDocument(new FileInputStream(file));
            System.out.println(hwpfDocument.getDocProperties().getCWords()); // actually 71 words using word count in MSWord, returned 57.
            System.out.println(hwpfDocument.getDocProperties().getCWordsFtnEnd());
            XWPFDocument xwpfDocument = new XWPFDocument(new FileInputStream(file2)); // actually 71 words using word count in MSWord, returned 57.
            System.out.println(xwpfDocument.getProperties().getExtendedProperties().getUnderlyingProperties().getWords());



            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

"whatever.doc" has 71 words, when I run this, it returns only 57.

enter image description here

Seems I cannot use the same method to read DOCX files, when I run it I get the following:

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

Could provide an example?

Upvotes: 1

Views: 997

Answers (1)

Pierre P.
Pierre P.

Reputation: 93

I've also found that the built-in word counters give strange counts, but text extraction seems to be more reliable, so I use this solution:

public long getWordCount(File file) throws IOException {
    POITextExtractor textExtractor;
    if (file.getName().endsWith(".docx")) {
        XWPFDocument doc = new XWPFDocument(new FileInputStream(file));
        textExtractor = new XWPFWordExtractor(doc);
    }
    else if (file.getName().endsWith(".doc")) {
        textExtractor = new WordExtractor(new FileInputStream(file));
    }
    else {
        throw new IllegalArgumentException("Not a MS Word file.");
    }

    return Arrays.stream(textExtractor.getText().split("\\s+"))
     .filter(s -> s.matches("^.*[\\p{L}\\p{N}].*$"))
     .count();
}

The regex at the bottom can be adjusted if needed, but overall this one has proved fairly resilient.

Upvotes: 1

Related Questions