首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用java如何计算字数,得到与MS-Office字数统计功能相同的结果

使用java如何计算字数,得到与MS-Office字数统计功能相同的结果
EN

Stack Overflow用户
提问于 2015-08-27 09:51:35
回答 3查看 417关注 0票数 0

I/P文件: doc,docx,en-dash,em-dash

我已经使用Apache Tika (元数据属性)和Aspose wordtojava(库)实现了字数统计功能,但它们不能给我提供准确的字数统计结果。

en-dash & em-dash字数与MS-Office ex不同。2-3 4-5结果: MS-office给出字数4对于上面的例子,APache - Tika & Aspose库给出字数2

如何计算与MS-Office给出的正确字数相同的字数?

任何帮助都是非常值得感谢的。

需要快速响应。

谢谢

EN

回答 3

Stack Overflow用户

发布于 2015-08-27 10:18:43

将文档中的所有字符串提取为一个字符串。使用这个正则表达式"\n\t\r\f \p{Pd}“拆分它们,并计算拆分的字符串数组的长度。

代码语言:javascript
复制
    String allWords = "2—3 4–5";
    String[] split = allWords.split("[\n\t\r\f \\p{Pd}]");
    System.out.println(split.length);

它打印了4。希望这对你有帮助。

票数 2
EN

Stack Overflow用户

发布于 2015-08-27 13:49:59

'BuiltInDocumentProperties.Words‘属性表示Word文档中的字数估计值。当您调用“Document.updateWordCount”方法时,Aspose.Words会更新此属性。请参考以下示例代码:

代码语言:javascript
复制
Document doc = new Document(getMyDir() + "in.docx");

// Update the word, character and paragraph count of the document.
doc.updateWordCount();

// Display the updated document properties.
System.out.println("Characters: " + doc.getBuiltInDocumentProperties().getCharacters());
System.out.println("Words: " + doc.getBuiltInDocumentProperties().getWords());
System.out.println("Paragraphs: " + doc.getBuiltInDocumentProperties().getParagraphs());

霍普,这有帮助。

另外,请确保您使用的是latest version of Aspose.Words for Java,即15.7.0。

我与Aspose一起工作,作为开发人员的布道者。

票数 0
EN

Stack Overflow用户

发布于 2019-07-30 02:11:32

你可能还想看看https://github.com/maresja1/Word-Counter/blob/master/README.md,它使用Apache tika,可以处理doc,docx,rtf,pdf等。我看了代码,它实际上是一个字符计数器,可以删除重复的空格。但是可以很容易地将其更改为单词计数器。

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

https://stackoverflow.com/questions/32239427

复制
相关文章

相似问题

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