Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow word tokenization override #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ print(lw.score)
print(lw.grade_level)
```

## What makes a word

Bear in mind that there is no consensus as to what is (not) a word. The default word tokenizer keeps clitics as one word
(e.g. `we've`) and splits abbreviations (e.g. `['U', '.', 'S', '.']`). To change this behavior you can pass your own
tokenizer.
**_example:_**
```python
from nltk import word_tokenize
r = Readability(text, f_tokenize_words=word_tokenize)
```

## [Contributing](CONTRIBUTING.md)

Contributions are welcome!
Expand Down
11 changes: 9 additions & 2 deletions readability/readability.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@


class Readability:
def __init__(self, text):
self._analyzer = Analyzer()
def __init__(self, text, f_tokenize_words=None):
"""
:param text: str
Input text. Consider cleaning your text before calculating readability metrics
:param f_tokenize_words: callable, default=None
Override default tokenization of words;
Example: lambda txt: NltkTokenizer('english').word_tokenize(txt)
"""
self._analyzer = Analyzer(f_tokenize_words)
self._statistics = self._analyzer.analyze(text)

def ari(self):
Expand Down
11 changes: 10 additions & 1 deletion readability/text/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ def __str__(self):


class Analyzer:
def __init__(self):
def __init__(self, f_tokenize_words=None):
"""
:param f_tokenize_words: callable, default=None
Override default tokenization of words;
Example: lambda txt: NltkTokenizer('english').word_tokenize(txt)
"""
self.f_tokenize_words = f_tokenize_words
pass

def analyze(self, text):
Expand Down Expand Up @@ -125,6 +131,9 @@ def _tokenize_sentences(self, text):
return sent_tokenize(text)

def _tokenize(self, text):
if self.f_tokenize_words:
return self.f_tokenize_words(text)

tokenizer = TweetTokenizer()
return tokenizer.tokenize(text)

Expand Down
37 changes: 37 additions & 0 deletions test/test_tokenize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest

from nltk import word_tokenize

from readability import Readability


class WordTokenizeTest(unittest.TestCase):
"""
Tests non-default word-tokenization
"""

def setUp(self):
# taken from https://en.wikipedia.org/wiki/Word#Summary
self.text = """
What makes a word?
In linguistics, a word of a spoken language can be defined as the smallest sequence of phonemes that can be
uttered in isolation with objective or practical meaning.
There have been many proposed criteria for identifying words.
However, no definition has been found to apply to all languages.
Dictionaries categorize a language's lexicon (i.e., its vocabulary) into lemmas.
These can be taken as an indication of what constitutes a "word" in the opinion of the writers of that language.
The most appropriate means of measuring the length of a word is by counting its syllables or morphemes.
When a word has multiple definitions or multiple senses, it may result in confusion in a debate or discussion.
"""

def test_nltk_treebank_tokenizer(self):
r = Readability(self.text, f_tokenize_words=word_tokenize).ari()
self.assertEqual(8.730483870967742, r.score)
self.assertEqual(['9'], r.grade_levels)
self.assertEqual([14, 15], r.ages)

def test_default_tokenizer(self):
r = Readability(self.text).ari()
self.assertEqual(8.578548387096774, r.score)
self.assertEqual(['9'], r.grade_levels)
self.assertEqual([14, 15], r.ages)