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

[WIP] EstNLTK analyzer #818

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
fi
# For Python 3.10:
if [[ ${{ matrix.python-version }} == '3.10' ]]; then
poetry install -E "fasttext spacy";
poetry install -E "fasttext spacy estnltk";
# download the small English pretrained spaCy model needed by spacy analyzer
poetry run python -m spacy download en_core_web_sm --upgrade-strategy only-if-needed
fi
Expand Down
7 changes: 7 additions & 0 deletions annif/analyzer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
except ImportError:
annif.logger.debug("voikko not available, not enabling voikko analyzer")

try:
from . import estnltk

register_analyzer(estnltk.EstNLTKAnalyzer)
except ImportError:
annif.logger.debug("EstNLTK not available, not enabling estnltk analyzer")

Check warning on line 54 in annif/analyzer/__init__.py

View check run for this annotation

Codecov / codecov/patch

annif/analyzer/__init__.py#L53-L54

Added lines #L53 - L54 were not covered by tests

try:
from . import spacy

Expand Down
24 changes: 24 additions & 0 deletions annif/analyzer/estnltk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""EstNLTK analyzer for Annif which uses EstNLTK for lemmatization"""

from __future__ import annotations

from . import analyzer


class EstNLTKAnalyzer(analyzer.Analyzer):
name = "estnltk"

def __init__(self, param: str, **kwargs) -> None:
self.param = param
super().__init__(**kwargs)

def tokenize_words(self, text: str, filter: bool = True) -> list[str]:
import estnltk

txt = estnltk.Text(text.strip())
txt.tag_layer()
return [
lemma
for lemma in [lemmas[0] for lemmas in txt.lemma]
if (not filter or self.is_valid_token(lemma))
]
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ huggingface-hub = "~0.25.1"

fasttext-wheel = { version = "0.9.2", optional = true }
voikko = { version = "0.5.*", optional = true }
estnltk = { version = "1.7.3", optional = true }
tensorflow-cpu = { version = "~2.17.0", optional = true }
lmdb = { version = "~1.5.1", optional = true }
omikuji = { version = "0.5.*", optional = true }
Expand All @@ -73,6 +74,7 @@ schemathesis = "3.*.*"
[tool.poetry.extras]
fasttext = ["fasttext-wheel"]
voikko = ["voikko"]
estnltk = ["estnltk"]
nn = ["tensorflow-cpu", "lmdb"]
omikuji = ["omikuji"]
yake = ["yake"]
Expand Down
49 changes: 49 additions & 0 deletions tests/test_analyzer_estnltk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Unit tests for EstNLTK analyzer in Annif"""

import pytest

import annif.analyzer

estnltk = pytest.importorskip("estnltk")


def test_estnltk_tokenize_words():
analyzer = annif.analyzer.get_analyzer("estnltk")
words = analyzer.tokenize_words(
"""
Aga kõik juhtus iseenesest. Ka köögis oli kõik endine.
"""
)
assert words == [
"aga",
"kõik",
"juhtuma",
"iseenesest",
"köök",
"olema",
"kõik",
"endine",
]


def test_estnltk_tokenize_words_no_filter():
analyzer = annif.analyzer.get_analyzer("estnltk")
words = analyzer.tokenize_words(
"""
Aga kõik juhtus iseenesest. Ka köögis oli kõik endine.
""",
filter=False,
)
assert words == [
"aga",
"kõik",
"juhtuma",
"iseenesest",
".",
"ka",
"köök",
"olema",
"kõik",
"endine",
".",
]