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

Add the number of tokens per sentence feature #2

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
6 changes: 6 additions & 0 deletions expats/feature/text_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def extract(self, _input):
return np.array([len(_input)])


@Feature.register
class NumberOfTokenPerSentFeature(Feature[spacy.tokens.doc.Doc]):
def extract(self, _input):
return np.array([len(_input) / len(list(_input.sents))])


@Feature.register
class AverageTokenLengthFeature(Feature[spacy.tokens.doc.Doc]):
def extract(self, _input):
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import spacy
import pytest


@pytest.fixture(scope="session")
def spacy_en():
yield spacy.load("en_core_web_sm")
45 changes: 26 additions & 19 deletions tests/feature/test_text_basics.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,60 @@
from typing import List

import numpy as np
import spacy
import pytest

from expats.feature.text_basics import (
NumberOfTokenFeature,
NumberOfTokenPerSentFeature,
AverageTokenLengthFeature,
UnigramLikelihoodFeature,
)


def _create_spacy_doc(words: List[str]) -> spacy.tokens.doc.Doc:
return spacy.tokens.doc.Doc(spacy.vocab.Vocab(), words=words)
@pytest.mark.parametrize(
"text, expected_value",
[
("i am here", 3),
]
)
def test_number_of_token_feature(spacy_en, text, expected_value):
doc = spacy_en(text)
feature = NumberOfTokenFeature()
np.testing.assert_array_equal(feature.extract(doc), np.array([expected_value]))


@pytest.mark.parametrize(
"words, expected_value",
"text, expected_value",
[
(["i", "am", "here"], 3),
("This is foo. That is foo bar.", (9 / 2)),
]
)
def test_number_of_token_feature(words, expected_value):
doc = _create_spacy_doc(words)
feature = NumberOfTokenFeature()
def test_number_of_token_per_sent_feature(spacy_en, text, expected_value):
doc = spacy_en(text)
feature = NumberOfTokenPerSentFeature()
np.testing.assert_array_equal(feature.extract(doc), np.array([expected_value]))


@pytest.mark.parametrize(
"words, expected_value",
"text, expected_value",
[
(["i", "am", "here"], 7 / 3),
(["a", "ab", "b"], 4 / 3)
("i am here", 7 / 3),
("a ab b", 4 / 3)
]
)
def test_average_token_length_feature(words, expected_value):
doc = _create_spacy_doc(words)
def test_average_token_length_feature(spacy_en, text, expected_value):
doc = spacy_en(text)
feature = AverageTokenLengthFeature()
np.testing.assert_array_equal(feature.extract(doc), np.array([expected_value]))


@pytest.mark.parametrize(
"words, word2freq, expected_value",
"text, word2freq, expected_value",
[
(["i", "am"], {"i": 4, "am": 3, "is": 2}, (np.log(4 / 9) + np.log(3 / 9)) / 2),
(["i", "are"], {"i": 4, "am": 3, "is": 2}, (np.log(4 / 9) + np.log(1 / 9)) / 2), # NOTE: OOV case
("i am", {"i": 4, "am": 3, "is": 2}, (np.log(4 / 9) + np.log(3 / 9)) / 2),
("i are", {"i": 4, "am": 3, "is": 2}, (np.log(4 / 9) + np.log(1 / 9)) / 2), # NOTE: OOV case
]
)
def test_unigram_likelihood_feature(words, word2freq, expected_value):
doc = _create_spacy_doc(words)
def test_unigram_likelihood_feature(spacy_en, text, word2freq, expected_value):
doc = spacy_en(text)
feature = UnigramLikelihoodFeature(word2freq)
np.testing.assert_array_equal(feature.extract(doc), np.array([expected_value]))