Add a count_words() function to the break iterator

This commit is contained in:
Kovid Goyal 2016-01-07 09:32:06 +05:30
parent 780625e18e
commit e5b741c7ea

View File

@ -39,3 +39,10 @@ def index_of(needle, haystack, lang='en'):
it.set_text(haystack) it.set_text(haystack)
return it.index(needle) 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())