Skip to content
Merged
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
3 changes: 1 addition & 2 deletions pythainlp/spell/tltk.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
from __future__ import annotations

import gzip
from typing import Optional

from pythainlp.corpus import corpus_path
from pythainlp.tools.path import safe_path_join

_LETTERS: list[str] = [chr(i) for i in range(ord("ก"), ord("์") + 1)]
_TLTK_DICT: Optional[set[str]] = None
_TLTK_DICT: set[str] | None = None


def _get_tltk_dict() -> set[str]:
Expand Down
11 changes: 5 additions & 6 deletions pythainlp/tag/tltk.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from __future__ import annotations

import re
from typing import cast, Optional, Union

from pythainlp.corpus import corpus_path
from pythainlp.tag._tag_perceptron import PerceptronTagger
Expand All @@ -18,8 +17,8 @@
_POS_MODEL_PATH: str = safe_path_join(corpus_path(), "tltk_pos_tnc.json.gz")
_NER_MODEL_PATH: str = safe_path_join(corpus_path(), "tltk_ner.json.gz")

_TAGGER: Optional[PerceptronTagger] = None
_NER_TAGGER: Optional[CRFTagger] = None
_TAGGER: PerceptronTagger | None = None
_NER_TAGGER: CRFTagger | None = None


def _get_pos_tagger() -> PerceptronTagger:
Expand All @@ -42,13 +41,13 @@
return "SYM"
if re.match(r"[\,\;\:\.\(\)\'\"\!\?]$", w):
return "PUNCT"
if re.match(r"[0-9][0-9.,\+\-\*\/]*$", w):

Check warning on line 44 in pythainlp/tag/tltk.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use concise character class syntax '\d' instead of '[0-9]'.

See more on https://sonarcloud.io/project/issues?id=PyThaiNLP_pythainlp&issues=AaHU1WdusquJCiUqvI6M&open=AaHU1WdusquJCiUqvI6M&pullRequest=1607
return "NUM"
if re.match(r"<s/>$", w):
return "PUNCT"
if re.match(r"[\<\>a-zA-Z\/]+$", w):
return "SYM"
if re.match(r"[a-zA-Z0-9_]+$", w):

Check warning on line 50 in pythainlp/tag/tltk.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use concise character class syntax '\w' instead of '[a-zA-Z0-9_]'.

See more on https://sonarcloud.io/project/issues?id=PyThaiNLP_pythainlp&issues=AaHU1WdusquJCiUqvI6N&open=AaHU1WdusquJCiUqvI6N&pullRequest=1607
return "X"
return pos

Expand All @@ -63,9 +62,9 @@

def _word2features(
sent: list[tuple[str, str]], i: int
) -> dict[str, Union[float, str, bool]]:
) -> dict[str, float | str | bool]:
word, postag = sent[i][0], sent[i][1]
features: dict[str, Union[float, str, bool]] = {
features: dict[str, float | str | bool] = {
"bias": 1.0,
"word": word,
"postag": postag,
Expand Down Expand Up @@ -145,7 +144,7 @@
ner_labels = _get_ner_tagger().tag(features)
sent_ner = [
(_post_process(word), p, n)
for (word, p), n in zip(tagged_pos, ner_labels)
for (word, p), n in zip(tagged_pos, ner_labels, strict=True)
]
if tag:
return _iob_to_markup([(word, ner) for word, _, ner in sent_ner])
Expand Down
Loading