Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions docs/api/corpus.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ countries
.. autofunction:: countries
:noindex:

find_synonym
~~~~~~~~~~~~
.. autofunction:: find_synonym
:noindex:

get_corpus
~~~~~~~~~~
.. autofunction:: get_corpus
Expand Down
2 changes: 2 additions & 0 deletions pythainlp/corpus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"corpus_path",
"countries",
"download",
"find_synonyms",
"get_corpus",
"get_corpus_as_is",
"get_corpus_db",
Expand Down Expand Up @@ -101,6 +102,7 @@ def corpus_db_path() -> str:
) # these imports must come before other pythainlp.corpus.* imports
from pythainlp.corpus.common import (
countries,
find_synonyms,
provinces,
thai_dict,
thai_family_names,
Expand Down
24 changes: 24 additions & 0 deletions pythainlp/corpus/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

__all__ = [
"countries",
"find_synonyms",
"provinces",
"thai_family_names",
"thai_female_names",
Expand Down Expand Up @@ -336,3 +337,26 @@ def thai_synonyms() -> dict:
def thai_synonym() -> dict:
warnings.warn("Deprecated: Use thai_synonyms() instead.", DeprecationWarning)
return thai_synonyms()


def find_synonyms(word) -> Union[List[str], None]:
Comment thread
wannaphong marked this conversation as resolved.
Outdated
"""
Find synonyms

:param str word: Thai word
:return: List synonyms of word or None if it isn't exist.
:rtype: Union[List[str], None]

:Example:
::

from pythainlp.corpus import find_synonyms

print(find_synonyms("หมู"))
# output: ['จรุก', 'วราห์', 'วราหะ', 'ศูกร', 'สุกร']
"""
_temp = thai_synonyms()
Comment thread
wannaphong marked this conversation as resolved.
Outdated
if word in _temp["word"]:
_idx = _temp["word"].index(word)
return _temp["synonym"][_idx]
return None
Comment thread
wannaphong marked this conversation as resolved.
Outdated
5 changes: 5 additions & 0 deletions tests/test_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
conceptnet,
countries,
download,
find_synonyms,
get_corpus_db,
get_corpus_db_detail,
get_corpus_default_db,
Expand Down Expand Up @@ -204,3 +205,7 @@ def test_zip(self):
p = get_corpus_path("test_zip")
self.assertEqual(os.path.isdir(p), True)
self.assertEqual(remove("test_zip"), True)

def test_find_synonyms(self):
self.assertIsInstance(find_synonyms("หมู"), list)
self.assertIsInstance(find_synonyms("1"), None)