Source code for pythainlp.tokenize.deepcut

# -*- 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.
"""
Wrapper for deepcut Thai word segmentation. deepcut is a
Thai word segmentation library using 1D Convolution Neural Network.

User need to install deepcut (and its dependency: tensorflow) by themselves.

:See Also:
    * `GitHub repository <https://github.com/rkcosmos/deepcut>`_
"""

from typing import List, Union

try:
    from deepcut import tokenize
except ImportError:
    raise ImportError("Please install deepcut by pip install deepcut")
from pythainlp.util import Trie


[docs]def segment( text: str, custom_dict: Union[Trie, List[str], str] = None ) -> List[str]: if not text or not isinstance(text, str): return [] if custom_dict: if isinstance(custom_dict, Trie): custom_dict = list(custom_dict) return tokenize(text, custom_dict) return tokenize(text)