-
Notifications
You must be signed in to change notification settings - Fork 273
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 #802 from PyThaiNLP/add-coref
Add pythainlp.coref
- Loading branch information
Showing
12 changed files
with
175 additions
and
6 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
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,10 @@ | ||
.. currentmodule:: pythainlp.coref | ||
|
||
pythainlp.coref | ||
=============== | ||
The :class:`pythainlp.coref` is Coreference Resolution for Thai. | ||
|
||
Modules | ||
------- | ||
|
||
.. autofunction:: coreference_resolution |
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,19 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2016-2023 PyThaiNLP Project | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
PyThaiNLP Coreference Resolution | ||
""" | ||
__all__ = ["coreference_resolution"] | ||
from pythainlp.coref.core import coreference_resolution |
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,38 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2016-2023 PyThaiNLP Project | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import List | ||
import spacy | ||
|
||
|
||
class FastCoref: | ||
def __init__(self, model_name, nlp=spacy.blank("th"), device:str="cpu", type:str="FCoref") -> None: | ||
if type == "FCoref": | ||
from fastcoref import FCoref as _model | ||
else: | ||
from fastcoref import LingMessCoref as _model | ||
self.model_name = model_name | ||
self.nlp = nlp | ||
self.model = _model(self.model_name,device=device,nlp=self.nlp) | ||
|
||
def _to_json(self, _predict): | ||
return { | ||
"text":_predict.text, | ||
"clusters_string":_predict.get_clusters(as_strings=True), | ||
"clusters":_predict.get_clusters(as_strings=False) | ||
} | ||
|
||
|
||
def predict(self, texts:List[str])->dict: | ||
return [self._to_json(i) for i in self.model.predict(texts=texts)] |
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,55 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2016-2023 PyThaiNLP Project | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import List | ||
model = None | ||
|
||
|
||
def coreference_resolution(texts:List[str], model_name:str="han-coref-v1.0", device:str="cpu"): | ||
""" | ||
Coreference Resolution | ||
:param List[str] texts: list texts to do coreference resolution | ||
:param str model_name: coreference resolution model | ||
:param str device: device for running coreference resolution model (cpu, cuda, and other) | ||
:return: List txets of coreference resolution | ||
:rtype: List[dict] | ||
:Options for model_name: | ||
* *han-coref-v1.0* - (default) Han-Corf: Thai oreference resolution by PyThaiNLP v1.0 | ||
:Example: | ||
:: | ||
from pythainlp.coref import coreference_resolution | ||
print( | ||
coreference_resolution( | ||
["Bill Gates ได้รับวัคซีน COVID-19 เข็มแรกแล้ว ระบุ ผมรู้สึกสบายมาก"] | ||
) | ||
) | ||
# output: | ||
# [ | ||
# {'text': 'Bill Gates ได้รับวัคซีน COVID-19 เข็มแรกแล้ว ระบุ ผมรู้สึกสบายมาก', | ||
# 'clusters_string': [['Bill Gates', 'ผม']], | ||
# 'clusters': [[(0, 10), (50, 52)]]} | ||
# ] | ||
""" | ||
global model | ||
if isinstance(texts, str): | ||
texts = [texts] | ||
if model == None and model_name=="han-coref-v1.0": | ||
from pythainlp.coref.han_coref import HanCoref | ||
model = HanCoref(device=device) | ||
return model.predict(texts) |
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,25 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2016-2023 PyThaiNLP Project | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from pythainlp.coref._fastcoref import FastCoref | ||
import spacy | ||
|
||
|
||
class HanCoref(FastCoref): | ||
def __init__(self,device:str="cpu",nlp=spacy.blank("th")) -> None: | ||
super(self.__class__, self).__init__( | ||
model_name="pythainlp/han-coref-v1.0", | ||
device=device, | ||
nlp=nlp | ||
) |
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,14 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import unittest | ||
from pythainlp.coref import coreference_resolution | ||
|
||
|
||
class TestParsePackage(unittest.TestCase): | ||
def test_coreference_resolution(self): | ||
pass | ||
# self.assertIsNotNone( | ||
# coreference_resolution( | ||
# "Bill Gates ได้รับวัคซีน COVID-19 เข็มแรกแล้ว ระบุ ผมรู้สึกสบายมาก" | ||
# ) | ||
# ) |