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
1 change: 1 addition & 0 deletions docker_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ pandas==0.24
tltk==1.3.8
OSKut==1.3
nlpo3==1.2.2
thai-nner==0.3
2 changes: 2 additions & 0 deletions docs/api/tag.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ Modules
.. autofunction:: chunk_parse
.. autoclass:: NER
:members:
.. autoclass:: NNER
:members:
.. autoclass:: pythainlp.tag.thainer.ThaiNameTagger
:members: get_ner
.. autofunction:: pythainlp.tag.tltk.get_ner
Expand Down
3 changes: 2 additions & 1 deletion pythainlp/tag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"tag_provinces",
"chunk_parse",
"NER",
"NNER",
]

from pythainlp.tag.locations import tag_provinces
from pythainlp.tag.pos_tag import pos_tag, pos_tag_sents
from pythainlp.tag._tag_perceptron import PerceptronTagger
from pythainlp.tag.chunk import chunk_parse
from pythainlp.tag.named_entity import NER
from pythainlp.tag.named_entity import NER, NNER
68 changes: 68 additions & 0 deletions pythainlp/tag/named_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,71 @@ def tag(
return self.engine.get_ner(text, tag=tag)
else:
return self.engine.get_ner(text, tag=tag, pos=pos)


class NNER:
"""
Nested Named Entity Recognition

:param str engine: Nested Named entity recognizer engine
:param str corpus: corpus

**Options for engine**
* *thai_nner* - Thai NER engine
"""
def __init__(self, engine: str = "thai_nner") -> None:
self.load_engine(engine)

def load_engine(self, engine: str = "thai_nner") -> None:
from pythainlp.tag.thai_nner import Thai_NNER
self.engine = Thai_NNER()

def tag(self, text) -> Tuple[List[str], List[dict]]:
"""
This function tags nested named-entitiy.

:param str text: text in Thai to be tagged

:return: a list of tuple associated with tokenized word, NNER tag.
:rtype: Tuple[List[str], List[dict]]

:Example:

>>> from pythainlp.tag.named_entity import NNER
>>> nner = NNER()
>>> nner.tag("แมวทำอะไรตอนห้าโมงเช้า")
([
'<s>',
'',
'แมว',
'ทํา',
'',
'อะไร',
'ตอน',
'',
'ห้า',
'',
'โมง',
'',
'เช้า',
'</s>'
],
[
{
'text': ['', 'ห้า'],
'span': [7, 9],
'entity_type': 'cardinal'
},
{
'text': ['', 'ห้า', '', 'โมง'],
'span': [7, 11],
'entity_type': 'time'
},
{
'text': ['', 'โมง'],
'span': [9, 11],
'entity_type': 'unit'
}
])
"""
return self.engine.tag(text)
14 changes: 14 additions & 0 deletions pythainlp/tag/thai_nner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import List, Tuple
from thai_nner import NNER
from pythainlp.corpus import get_corpus_path


class Thai_NNER:
def __init__(
self,
path_model=get_corpus_path('thai_nner', '1.0')
) -> None:
self.model = NNER(path_model=path_model)

def tag(self, text) -> Tuple[List[str], List[dict]]:
return self.model.get_tag(text)
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"numpy>=1.16.1",
"onnxruntime>=1.10.0"
],
"thai_nner": ["thai_nner"],
"full": [
"PyYAML>=5.3.1",
"attacut>=1.0.4",
Expand All @@ -106,6 +107,7 @@
"oskut>=1.3",
"nlpo3>=1.2.2",
"onnxruntime>=1.10.0",
"thai_nner"
],
}

Expand Down
5 changes: 5 additions & 0 deletions tests/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
unigram,
tltk,
NER,
NNER,
)
from pythainlp.tag.locations import tag_provinces
from pythainlp.tag.thainer import ThaiNameTagger
Expand Down Expand Up @@ -370,3 +371,7 @@ def test_NER_class(self):
self.assertIsNotNone(ner.tag("แมวทำอะไรตอนห้าโมงเช้า", tag=True))
with self.assertRaises(ValueError):
NER(engine="thainer", corpus="cat")

def test_NNER_class(self):
nner = NNER()
self.assertIsNotNone(nner.tag("แมวทำอะไรตอนห้าโมงเช้า"))