-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #818 from NatLibFi/feature-estnltk-analyzer
EstNLTK analyzer
- Loading branch information
Showing
6 changed files
with
110 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""EstNLTK analyzer for Annif which uses EstNLTK for lemmatization""" | ||
|
||
from __future__ import annotations | ||
|
||
import importlib | ||
|
||
from . import analyzer | ||
|
||
|
||
class EstNLTKAnalyzer(analyzer.Analyzer): | ||
name = "estnltk" | ||
|
||
@staticmethod | ||
def is_available() -> bool: | ||
# return True iff EstNLTK is installed | ||
return importlib.util.find_spec("estnltk") is not None | ||
|
||
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)) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""Unit tests for EstNLTK analyzer in Annif""" | ||
|
||
import pytest | ||
|
||
import annif.analyzer | ||
import annif.analyzer.estnltk | ||
|
||
pytestmark = pytest.mark.skipif( | ||
not annif.analyzer.estnltk.EstNLTKAnalyzer.is_available(), | ||
reason="EstNLTK is required", | ||
) | ||
|
||
|
||
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", | ||
".", | ||
] |