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

implemented hapax legomena index #53

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
27 changes: 27 additions & 0 deletions src/TRUNAJOD/ttr.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,30 @@ def d_estimate(
y = ttrs ** 2
d = np.linalg.lstsq(A, y, rcond=None)[0]
return d[0]


def hapax_legomena_index(doc: Doc) -> int:
"""Hapax Legomena Index from a text.

Hapax Legomena Index is the number of words occuring once in a text.

:param doc: Processed spaCy Doc
:type doc: Doc
:return: Texts' Hapex Legomena Index
:rtype: int
"""
word_counter = 0
word_dupe_counter = 0
words = {}
for token in doc:
if is_word(token.pos_):
word_counter += 1
if str(token.pos_) not in words:
words[str(token.pos_)] = 1
else:
words[str(token.pos_)] += 1

for key, value in words.items():
if int(value) > 1:
word_dupe_counter += int(value)
return word_counter - word_dupe_counter
16 changes: 16 additions & 0 deletions tests/ttr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,19 @@ def test_d_estimate():

np.random.seed(0)
assert ttr.d_estimate(doc) == 119.4468681409897


def test_hapax_legomena_index():
"""Test hapax_legomena_index."""
Token = namedtuple("Token", "lemma_ pos_")
doc = [
Token("hola", "hola"),
Token("hola", "hola"),
Token("chao", "chao"),
Token("hola", "hola"),
Token("perro", "perro"),
Token("hola", "hola"),
]

answer = 2
assert ttr.hapax_legomena_index(doc) == answer