What's the best way to determine the total number of words of a file in Java?

怎甘沉沦 提交于 2019-12-01 06:29:12

Congratulations you have stumbled upon one of the biggest linguistic problems! What is a word? It is said that a word is the only word that actually means what it is. There is an entire field of linguistics devoted to words/units of meaning - Morphology.

I assume that you question pertains to counting words in English. However, creating a language-neutral word counter/parser is next to impossible due to linguistic differences. For example, one might think that just processing the groups of characters separated by white space is sufficient. However, if you look at the following example in Japanese, you will see that that approach does not work:

これは日本語の例文です。

This example contains 3 distinct words and none of them are separated by spaces. Typically, Japanese word boundaries are parsed using a dictionary-based approach and there are a number of commercial libraries available for this. Are we lucky to have spaces in English! I believe that Indic languages, Chinese and Korean also have similar problems.

If this solution is going to actually be deployed in any ways that multi-lingual input is possible, it will be important to be able to plug in different word counting methods depending upon the language being parsed.

I think the first answer was a good answer because it uses Java's knowledge of Unicode whitespace values as delimiters. It tokenizes by matching using the following regex: \p{javaWhitespace}+

Itay Maman
int count = 0;
Scanner sc = new Scanner(new File("my-text-file.txt")); 
while (sc.hasNext()) {
   ++count;
   sc.next();
}

While Perl can do this, I'd consider it overkill to link it in / call it for this kind of task (unless you already have that tested out).

If you are on a unix, wc -w filename will do the trick.

making some assumptions about what defines a 'word', one solution would be to open the file using a text stream reader and scan it, counting the number of non-contiguous whitespace characters, plus one for the end, e.g.

 this is some sample text
 this is some more sample text

the text above would have 11 words in it, counted as 9 spaces and 1 newline and 1 end-of-file

I'd initialize a word_count int to 1, then loop through each character in the file and increment word_count for every whitespace character unless the previous character was a whitespace character. (A space, tab, or newline.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!