File size: 6,809 Bytes
e4b9a7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | # -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""
Check if it is Thai text
"""
import string
from typing import Tuple
from pythainlp import (
thai_above_vowels,
thai_below_vowels,
thai_consonants,
thai_digits,
thai_follow_vowels,
thai_lead_vowels,
thai_punctuations,
thai_signs,
thai_tonemarks,
thai_vowels,
)
_DEFAULT_IGNORE_CHARS = string.whitespace + string.digits + string.punctuation
_TH_FIRST_CHAR_ASCII = 3584
_TH_LAST_CHAR_ASCII = 3711
def isthaichar(ch: str) -> bool:
"""Check if a character is a Thai character.
:param ch: input character
:type ch: str
:return: True if ch is a Thai character, otherwise False.
:rtype: bool
:Example:
::
from pythainlp.util import isthaichar
isthaichar("ก") # THAI CHARACTER KO KAI
# output: True
isthaichar("๕") # THAI DIGIT FIVE
# output: True
"""
ch_val = ord(ch)
if _TH_FIRST_CHAR_ASCII <= ch_val <= _TH_LAST_CHAR_ASCII:
return True
return False
def isthai(text: str, ignore_chars: str = ".") -> bool:
"""Check if every character in a string is a Thai character.
:param text: input text
:type text: str
:param ignore_chars: characters to be ignored, defaults to "."
:type ignore_chars: str, optional
:return: True if every character in the input string is Thai,
otherwise False.
:rtype: bool
:Example:
::
from pythainlp.util import isthai
isthai("กาลเวลา")
# output: True
isthai("กาลเวลา.")
# output: True
isthai("กาล-เวลา")
# output: False
isthai("กาล-เวลา +66", ignore_chars="01234567890+-.,")
# output: True
"""
if not ignore_chars:
ignore_chars = ""
for ch in text:
if ch not in ignore_chars and not isthaichar(ch):
return False
return True
def countthai(text: str, ignore_chars: str = _DEFAULT_IGNORE_CHARS) -> float:
"""Find proportion of Thai characters in a given text
:param text: input text
:type text: str
:param ignore_chars: characters to be ignored, defaults to whitespace,\\
digits, and punctuation marks.
:type ignore_chars: str, optional
:return: proportion of Thai characters in the text (percentage)
:rtype: float
:Example:
::
from pythainlp.util import countthai
countthai("ไทยเอ็นแอลพี 3.0")
# output: 100.0
countthai("PyThaiNLP 3.0")
# output: 0.0
countthai("ใช้งาน PyThaiNLP 3.0")
# output: 40.0
countthai("ใช้งาน PyThaiNLP 3.0", ignore_chars="")
# output: 30.0
"""
if not text or not isinstance(text, str):
return 0.0
if not ignore_chars:
ignore_chars = ""
num_thai = 0
num_ignore = 0
for ch in text:
if ch in ignore_chars:
num_ignore += 1
elif isthaichar(ch):
num_thai += 1
num_count = len(text) - num_ignore
if num_count == 0:
return 0.0
return (num_thai / num_count) * 100
def display_thai_char(ch: str) -> str:
"""Prefix an underscore (_) to a high-position vowel or a tone mark,
to ease readability.
:param ch: input character
:type ch: str
:return: "_" + ch
:rtype: str
:Example:
::
from pythainlp.util import display_thai_char
display_thai_char("้")
# output: "_้"
"""
if (
ch in thai_above_vowels
or ch in thai_tonemarks
or ch in "\u0e33\u0e4c\u0e4d\u0e4e"
):
# last condition is Sra Aum, Thanthakhat, Nikhahit, Yamakkan
return "_" + ch
else:
return ch
def thai_word_tone_detector(word: str) -> Tuple[str, str]:
"""
Thai tone detector for word.
It uses pythainlp.transliterate.pronunciate for converting word to\
pronunciation.
:param str word: Thai word.
:return: Thai pronunciation with tones in each syllable.\
(l, m, h, r, f or empty if it cannot be detected)
:rtype: Tuple[str, str]
:Example:
::
from pythainlp.util import thai_word_tone_detector
print(thai_word_tone_detector("คนดี"))
# output: [('คน', 'm'), ('ดี', 'm')]
print(thai_word_tone_detector("มือถือ"))
# output: [('มือ', 'm'), ('ถือ', 'r')]
"""
from ..transliterate import pronunciate
from ..util.syllable import tone_detector
_pronunciate = pronunciate(word).split("-")
return [(i, tone_detector(i.replace("หฺ", "ห"))) for i in _pronunciate]
def count_thai_chars(text: str) -> dict:
"""
Count Thai characters by type
This function will give you numbers of Thai characters by type\
(consonants, vowels, lead_vowels, follow_vowels, above_vowels,\
below_vowels, tonemarks, signs, thai_digits, punctuations, non_thai)
:param str text: Text
:return: Dict with numbers of Thai characters by type
:rtype: dict
:Example:
::
from pythainlp.util import count_thai_chars
count_thai_chars("ทดสอบภาษาไทย")
# output: {
# 'vowels': 3,
# 'lead_vowels': 1,
# 'follow_vowels': 2,
# 'above_vowels': 0,
# 'below_vowels': 0,
# 'consonants': 9,
# 'tonemarks': 0,
# 'signs': 0,
# 'thai_digits': 0,
# 'punctuations': 0,
# 'non_thai': 0
# }
"""
_dict = {
"vowels": 0,
"lead_vowels": 0,
"follow_vowels": 0,
"above_vowels": 0,
"below_vowels": 0,
"consonants": 0,
"tonemarks": 0,
"signs": 0,
"thai_digits": 0,
"punctuations": 0,
"non_thai": 0,
}
for c in text:
if c in thai_vowels:
_dict["vowels"] += 1
if c in thai_lead_vowels:
_dict["lead_vowels"] += 1
elif c in thai_follow_vowels:
_dict["follow_vowels"] += 1
elif c in thai_above_vowels:
_dict["above_vowels"] += 1
elif c in thai_below_vowels:
_dict["below_vowels"] += 1
elif c in thai_consonants:
_dict["consonants"] += 1
elif c in thai_tonemarks:
_dict["tonemarks"] += 1
elif c in thai_signs:
_dict["signs"] += 1
elif c in thai_digits:
_dict["thai_digits"] += 1
elif c in thai_punctuations:
_dict["punctuations"] += 1
else:
_dict["non_thai"] += 1
return _dict
|