From e5b741c7ea3dfb6bcc806d760d3b7848f92fd95c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 7 Jan 2016 09:32:06 +0530 Subject: [PATCH] Add a count_words() function to the break iterator --- src/calibre/spell/break_iterator.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/calibre/spell/break_iterator.py b/src/calibre/spell/break_iterator.py index e88c15bcde..fa3aeba1c3 100644 --- a/src/calibre/spell/break_iterator.py +++ b/src/calibre/spell/break_iterator.py @@ -39,3 +39,10 @@ def index_of(needle, haystack, lang='en'): it.set_text(haystack) return it.index(needle) +def count_words(text, lang='en'): + with _lock: + it = _iterators.get(lang, None) + if it is None: + it = _iterators[lang] = _icu.BreakIterator(_icu.UBRK_WORD, lang_as_iso639_1(lang) or lang) + it.set_text(text) + return len(it.split2())