[Python] 如何判斷字串中的語言

Posted in :

我想知道某一個字串裡有沒有特定的語言文字出現,解法似乎很多,厲害的人都是使用 Regular Expression. 讓我們來看看範例:

import re
text = 'เมื่อแรกเริ่ม'
title = u"Klüft skräms inför på fédéral électoral große"
abc = 'abcd'
pattern = re.compile(r"[\u0E00-\u0E7Fa-zA-Z']+")

說明:上面一行就解決判斷泰文文字的檢查。

執行結果:

print(re.search(pattern,abc))

None

說明:沒找到,因為是英文。


print(re.search(pattern,text))

<re.Match object; span=(0, 13), match=’เมื่อแรกเริ่ม’>

說明:找到了


print(re.search(pattern,title))

None

說明:沒找到,因為沒有泰文。


From What’s the complete range for Chinese characters in Unicode?, the CJK unicode ranges are:

Block                                   Range       Comment
--------------------------------------- ----------- ----------------------------------------------------
CJK Unified Ideographs                  4E00-9FFF   Common
CJK Unified Ideographs Extension A      3400-4DBF   Rare
CJK Unified Ideographs Extension B      20000-2A6DF Rare, historic
CJK Unified Ideographs Extension C      2A700–2B73F Rare, historic
CJK Unified Ideographs Extension D      2B740–2B81F Uncommon, some in current use
CJK Unified Ideographs Extension E      2B820–2CEAF Rare, historic
CJK Compatibility Ideographs            F900-FAFF   Duplicates, unifiable variants, corporate characters
CJK Compatibility Ideographs Supplement 2F800-2FA1F Unifiable variants
CJK Symbols and Punctuation             3000-303F

You probably want to allow code points from the Unicode blocks CJK Unified Ideographs and CJK Unified Ideographs Extension A.

This regex will match 0 to 9 spaces, ideographic spaces (U+3000), A-Z letters, or code points in those 2 CJK blocks.

/^[ A-Za-z\u3000\u3400-\u4DBF\u4E00-\u9FFF]{0,9}$/

The ideographs are listed in:

However, you may as well add more blocks.

Code:

function has10OrLessCJK(text) {
    return /^[ A-Za-z\u3000\u3400-\u4DBF\u4E00-\u9FFF]{0,9}$/.test(text);
}

function checkValidation(value) {
    var valid = document.getElementById("valid");
    if (has10OrLessCJK(value)) {
        valid.innerText = "Valid";
    } else {
        valid.innerText = "Invalid";
    }
}
<input type="text" 
       style="width:100%"
       oninput="checkValidation(this.value)"
       value="你的a你的a你的a">

<div id="valid">
    Valid
</div>

Unicode 編碼表

各編碼對應的字元可見此 Excel: Unicode List xls (此檔案同樣取自此網站)

  1. 【0020-007F】 Basic Latin 基本拉丁字母
  2. 【00A0-00FF】 Latin-1 Supplement 拉丁字母補充-1
  3. 【0100-017F】 Latin Extended-A 拉丁字母擴充-A
  4. 【0180-023F】 Latin Extended-B 拉丁字母擴充-B
  5. 【0250-02AF】 IPA Extensions 國際音標擴充
  6. 【02B0-02EF】 Spacing Modifier Letters 空格修飾字元
  7. 【0300-036F】 Combining Diacritical Marks 組合音標附加符號
  8. 【0370-03FF】 Greek and Coptic 希臘字母
  9. 【0400-04FF】 Cyrillic 西里爾字母
  10. 【0500-052F】 Cyrillic Supplement 西里爾字母補充
  11. 【0530-058F】 Armenian 亞美尼亞文
  12. 【0590-05FF】 Hebrew 希伯來文
  13. 【0600-06FF】 Arabic 基本阿拉伯文
  14. 【0700-074F】 Syriac 敘利亞文
  15. 【0750-077F】 Arabic Supplement 阿拉伯文補充
  16. 【0780-07BF】 Thaana 塔納文
  17. 【07C0-07FF】 N’Ko
  18. 【0900-097F】 Devanagari 天城體梵文字母
  19. 【0980-09FF】 Bengali 孟加拉國文
  20. 【0A00-0A7F】 Gurmukhi 古爾穆基文
  21. 【0A80-0AFF】 Gujarati 古吉拉特文
  22. 【0B00-0B7F】 Oriya 奧里亞文
  23. 【0B80-0BFF】 Tamil 泰米爾文
  24. 【0C00-0C7F】 Telugu 泰盧固文
  25. 【0C80-0CFF】 Kannada 卡納達文
  26. 【0D00-0D7F】 Malayalam 馬拉亞拉姆文
  27. 【0D80-0DFF】 Sinhala 僧伽羅文
  28. 【0E00-0E7F】 Thai 泰文
  29. 【0E80-0EFF】 Lao 寮國文;寮國文
  30. 【0F00-0FFF】 Tibetan 藏文
  31. 【1000-109F】 Myanmar 緬甸文
  32. 【10A0-10FF】 Georgian 喬治亞文
  33. 【1100-11FF】 Hangul Jamo 諺文字母
  34. 【1200-137F】 Ethiopic 衣索比亞文
  35. 【1380-139F】 Ethiopic Supplement 衣索比亞文補充
  36. 【13A0-13FF】 Cherokee 切羅基文
  37. 【1400-167F】 Unified Canadian Aboriginal Syllabics 加拿大土著統一音節文字
  38. 【1680-169F】 Ogham 歐甘文
  39. 【16A0-16FF】 Runic 北歐古文
  40. 【1700-171F】 Tagalog 他加祿文
  41. 【1720-173F】 Hanunoo 哈努諾文
  42. 【1740-175F】 Buhid 布希德文
  43. 【1760-177F】 Tagbanwa 塔格巴努亞文
  44. 【1780-17FF】 Khmer 高棉文
  45. 【1800-18AF】 Mongolian 蒙古文
  46. 【1900-194F】 Limbu 林布文
  47. 【1950-197F】 Tai Le 傣哪文;德巨集傣文
  48. 【1980-19DF】 New Tai Lue 新傣仂文
  49. 【19E0-19FF】 Khmer Symbols 高棉符號
  50. 【1A00-1A1F】 Buginese 布吉文
  51. 【1B00-1B7F】 Balinese 巴利文
  52. 【1D00-1D7F】 Phonetic Extensions 音標擴充
  53. 【1D80-1DBF】 Phonetic Extensions Supplement 音標擴充補充
  54. 【1DC0-1DFF】 Combining Diacritical Marks Supplement 組合音標附加符號
  55. 【1E00-1EFF】 Latin Extended Additional 拉丁字母擴充附加
  56. 【1F00-1FFF】 Greek Extended 希臘文擴充
  57. 【2000-206F】 General Punctuation 一般標點符號
  58. 【2070-209F】 Superscripts and Subscripts 下標及上標
  59. 【20A0-20CF】 Currency Symbols 貨幣符號
  60. 【20D0-20FF】 Combining Diacritical Marks for Symbols 符號用組合附加符號
  61. 【2100-214F】 Letterlike Symbols 似字母符號
  62. 【2150-218F】 Number Forms 數字形式
  63. 【2190-21FF】 Arrows 箭頭符號
  64. 【2200-22FF】 Mathematical Operators 數學運算符號
  65. 【2300-23FF】 Miscellaneous Technical 混合專門符號
  66. 【2400-243F】 Control Pictures 控制圖像
  67. 【2440-245F】 Optical Character Recognition 光學字元識別
  68. 【2460-24FF】 Enclosed Alphanumerics 括號字母數字
  69. 【2500-257F】 Box Drawing 製表符
  70. 【2580-259F】 Block Elements 區塊組件
  71. 【25A0-25FF】 Geometric Shapes 幾何形狀
  72. 【2600-26FF】 Miscellaneous Symbols 混合什錦符號
  73. 【2700-27BF】 Dingbats 什錦符號
  74. 【27C0-27EF】 Miscellaneous Mathematical Symbols-A 混合數學符號-A
  75. 【27F0-27FF】 Supplemental Arrows-A 補充性箭頭符號-A
  76. 【2800-28FF】 Braille Patterns 盲文;盲人點字
  77. 【2900-297F】 Supplemental Arrows-B 補充性箭頭符號-B
  78. 【2980-29FF】 Miscellaneous Mathematical Symbols-B 混合數學符號-B
  79. 【2A00-2AFF】 Supplemental Mathematical Operators 補充性數學運算符號
  80. 【2B00-2BFF】 Miscellaneous Symbols and Arrows 混合什錦符號和箭頭符號
  81. 【2C00-2C5F】 Glagolitic 格拉戈爾字母
  82. 【2C60-2C7F】 Latin Extended-C 拉丁字母擴充-C
  83. 【2C80-2CFF】 Coptic 科普特文
  84. 【2D00-2D2F】 Georgian Supplement 喬治亞文補充
  85. 【2D30-2D7F】 Tifinagh 提非納格字母
  86. 【2D80-2DDF】 Ethiopic Extended 衣索比亞文擴充
  87. 【2E00-2E7F】 Supplemental Punctuation 補充性標點符號
  88. 【2E80-2EFF】 CJK Radicals Supplement 中日韓部首補充
  89. 【2F00-2FDF】 Kangxi Radicals 康熙部首
  90. 【2FF0-2FFF】 Ideographic Description Characters 漢字結構描述字元
  91. 【3000-303F】 CJK Symbols and Punctuation 中日韓符號和標點
  92. 【3040-309F】 Hiragana 平假名
  93. 【30A0-30FF】 Katakana 片假名
  94. 【3100-312F】 Bopomofo 注音符號
  95. 【3130-318F】 Hangul Compatibility Jamo 諺文相容字母
  96. 【3190-319F】 Kanbun 漢文標註號
  97. 【31A0-31BF】 Bopomofo Extended 注音符號擴充
  98. 【31C0-31EF】 CJK Strokes 中日韓筆畫部件
  99. 【31F0-31FF】 Katakana Phonetic Extensions 片假名音標擴充
  100. 【3200-32FF】 Enclosed CJK Letters and Months 中日韓括號字母及月份
  101. 【3300-33FF】 CJK Compatibility 中日韓相容字元
  102. 【3400-4DBF】 CJK Unified Ideographs Extension A 中日韓統一表意文字擴充A
  103. 【4DC0-4DFF】 Yijing Hexagram Symbols 易經六十四卦象
  104. 【4E00-9FFF】 CJK Unified Ideographs 中日韓統一表意文字
  105. 【A000-A48F】 Yi Syllables 彞文音節
  106. 【A490-A4CF】 Yi Radicals 彞文字母
  107. 【A700-A71F】 Modifier Tone Letters 聲調符號
  108. 【A720-A7FF】 Latin Extended-D 拉丁字母擴充-D
  109. 【A800-A82F】 Syloti Nagri
  110. 【A840-A87F】 Phags-pa 八思巴字母
  111. 【AC00-D7AF】 Hangul Syllables 諺文音節
  112. 【D800-DB7F】 High Surrogates 高半代用區
  113. 【DB80-DBFF】 High Private Use Surrogates 高半專用代用區
  114. 【DC00-DFFF】 Low Surrogates 低半代用區
  115. 【E000-F8FF】 Private Use Area 專用區
  116. 【F900-FAFF】 CJK Compatibility Ideographs 中日韓相容表意文字
  117. 【FB00-FB4F】 Alphabetic Presentation Forms 字母變體顯現形式
  118. 【FB50-FDFF】 Arabic Presentation Forms-A 阿拉伯文變體顯現形式-A
  119. 【FE00-FE0F】 Variation Selectors 字型變換選取器
  120. 【FE10-FE1F】 Vertical Forms 豎式標點
  121. 【FE20-FE2F】 Combining HalF】 Marks 組合半形標示
  122. 【FE30-FE4F】 CJK Compatibility Forms 中日韓相容形式
  123. 【FE50-FE6F】 Small Form Variants 小寫變體
  124. 【FE70-FEFF】 Arabic Presentation Forms-B 阿拉伯文變體顯現形式-B
  125. 【FF00-FFEF】 Halfwidth and Fullwidth Forms 半形及全形字元
  126. 【FFF0-FFFF】 Specials 特殊區域
  127. 【10000-1007F】 Linear B Syllabary 線形文字B音節文字
  128. 【10080-100FF】 Linear B Ideograms 線形文字B表意文字
  129. 【10100-1013F】 Aegean Numbers 愛琴數字
  130. 【10140-1018F】 Ancient Greek Numbers 古希臘數字
  131. 【10300-1032F】 Old Italic 古義大利文
  132. 【10330-1034F】 Gothic 哥特文
  133. 【10380-1039F】 Ugaritic 烏加里特楔形文字
  134. 【103A0-103DF】 Old Persian 古波斯文
  135. 【10400-1044F】 Deseret 猶他大學音標
  136. 【10450-1047F】 Shavian 肅伯納字母
  137. 【10480-104AF】 Osmanya
  138. 【10800-1083F】 Cypriot Syllabary 塞普勒斯音節文字
  139. 【10900-1091F】 Phoenician 腓尼基字母
  140. 【10A00-10A5F】 Kharoshthi 佉盧字母
  141. 【12000-123FF】 Cuneiform 楔形文字
  142. 【12400-1247F】 Cuneiform Numbers and Punctuation 楔形文字數字及標點
  143. 【1D000-1D0FF】 Byzantine Musical Symbols 東正教音樂符號
  144. 【1D100-1D1FF】 Musical Symbols 音樂符號
  145. 【1D200-1D24F】 Ancient Greek Musical Notation 古希臘音樂譜記號
  146. 【1D300-1D35F】 Tai Xuan Jing Symbols 太玄經符號
  147. 【1D360-1D37F】 Counting Rod Numerals 算籌記數式
  148. 【1D400-1D7FF】 Mathematical Alphanumeric Symbols 數學用字母數字元號
  149. 【20000-2A6DF】 CJK Unified Ideographs Extension B 中日韓統一表意文字擴充B
  150. 【2F800-2FA1F】 CJK Compatibility Ideographs Supplement 中日韓相容表意文字補充
  151. 【E0000-E007F】 Tags 語言編碼捲標
  152. 【E0100-E01EF】 Variation Selectors Supplement 字型變換選取器補充
  153. 【FFF80-FFFFF】 Supplementary Private Use Area-A 補充專用區-A
  154. 【10FF80-10FFFF】 Supplementary Private Use Area-B 補充專用區-B

相關文章:

[Python] RegEx
http://stackoverflow.max-everyday.com/2019/04/python-regex/

[Python] 如何取出字串中的選項
http://stackoverflow.max-everyday.com/2018/12/python-regular-expression-findall/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *