languages.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Metadata about languages used by our model training code for our
  5. SingleByteCharSetProbers. Could be used for other things in the future.
  6. This code is based on the language metadata from the uchardet project.
  7. """
  8. from __future__ import absolute_import, print_function
  9. from string import ascii_letters
  10. # TODO: Add Ukranian (KOI8-U)
  11. class Language(object):
  12. """Metadata about a language useful for training models
  13. :ivar name: The human name for the language, in English.
  14. :type name: str
  15. :ivar iso_code: 2-letter ISO 639-1 if possible, 3-letter ISO code otherwise,
  16. or use another catalog as a last resort.
  17. :type iso_code: str
  18. :ivar use_ascii: Whether or not ASCII letters should be included in trained
  19. models.
  20. :type use_ascii: bool
  21. :ivar charsets: The charsets we want to support and create data for.
  22. :type charsets: list of str
  23. :ivar alphabet: The characters in the language's alphabet. If `use_ascii` is
  24. `True`, you only need to add those not in the ASCII set.
  25. :type alphabet: str
  26. :ivar wiki_start_pages: The Wikipedia pages to start from if we're crawling
  27. Wikipedia for training data.
  28. :type wiki_start_pages: list of str
  29. """
  30. def __init__(self, name=None, iso_code=None, use_ascii=True, charsets=None,
  31. alphabet=None, wiki_start_pages=None):
  32. super(Language, self).__init__()
  33. self.name = name
  34. self.iso_code = iso_code
  35. self.use_ascii = use_ascii
  36. self.charsets = charsets
  37. if self.use_ascii:
  38. if alphabet:
  39. alphabet += ascii_letters
  40. else:
  41. alphabet = ascii_letters
  42. elif not alphabet:
  43. raise ValueError('Must supply alphabet if use_ascii is False')
  44. self.alphabet = ''.join(sorted(set(alphabet))) if alphabet else None
  45. self.wiki_start_pages = wiki_start_pages
  46. def __repr__(self):
  47. return '{}({})'.format(self.__class__.__name__,
  48. ', '.join('{}={!r}'.format(k, v)
  49. for k, v in self.__dict__.items()
  50. if not k.startswith('_')))
  51. LANGUAGES = {'Arabic': Language(name='Arabic',
  52. iso_code='ar',
  53. use_ascii=False,
  54. # We only support encodings that use isolated
  55. # forms, because the current recommendation is
  56. # that the rendering system handles presentation
  57. # forms. This means we purposefully skip IBM864.
  58. charsets=['ISO-8859-6', 'WINDOWS-1256',
  59. 'CP720', 'CP864'],
  60. alphabet=u'ءآأؤإئابةتثجحخدذرزسشصضطظعغػؼؽؾؿـفقكلمنهوىيًٌٍَُِّ',
  61. wiki_start_pages=[u'الصفحة_الرئيسية']),
  62. 'Belarusian': Language(name='Belarusian',
  63. iso_code='be',
  64. use_ascii=False,
  65. charsets=['ISO-8859-5', 'WINDOWS-1251',
  66. 'IBM866', 'MacCyrillic'],
  67. alphabet=(u'АБВГДЕЁЖЗІЙКЛМНОПРСТУЎФХЦЧШЫЬЭЮЯ'
  68. u'абвгдеёжзійклмнопрстуўфхцчшыьэюяʼ'),
  69. wiki_start_pages=[u'Галоўная_старонка']),
  70. 'Bulgarian': Language(name='Bulgarian',
  71. iso_code='bg',
  72. use_ascii=False,
  73. charsets=['ISO-8859-5', 'WINDOWS-1251',
  74. 'IBM855'],
  75. alphabet=(u'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ'
  76. u'абвгдежзийклмнопрстуфхцчшщъьюя'),
  77. wiki_start_pages=[u'Начална_страница']),
  78. 'Czech': Language(name='Czech',
  79. iso_code='cz',
  80. use_ascii=True,
  81. charsets=['ISO-8859-2', 'WINDOWS-1250'],
  82. alphabet=u'áčďéěíňóřšťúůýžÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ',
  83. wiki_start_pages=[u'Hlavní_strana']),
  84. 'Danish': Language(name='Danish',
  85. iso_code='da',
  86. use_ascii=True,
  87. charsets=['ISO-8859-1', 'ISO-8859-15',
  88. 'WINDOWS-1252'],
  89. alphabet=u'æøåÆØÅ',
  90. wiki_start_pages=[u'Forside']),
  91. 'German': Language(name='German',
  92. iso_code='de',
  93. use_ascii=True,
  94. charsets=['ISO-8859-1', 'WINDOWS-1252'],
  95. alphabet=u'äöüßÄÖÜ',
  96. wiki_start_pages=[u'Wikipedia:Hauptseite']),
  97. 'Greek': Language(name='Greek',
  98. iso_code='el',
  99. use_ascii=False,
  100. charsets=['ISO-8859-7', 'WINDOWS-1253'],
  101. alphabet=(u'αβγδεζηθικλμνξοπρσςτυφχψωάέήίόύώ'
  102. u'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΣΤΥΦΧΨΩΆΈΉΊΌΎΏ'),
  103. wiki_start_pages=[u'Πύλη:Κύρια']),
  104. 'English': Language(name='English',
  105. iso_code='en',
  106. use_ascii=True,
  107. charsets=['ISO-8859-1', 'WINDOWS-1252'],
  108. wiki_start_pages=[u'Main_Page']),
  109. 'Esperanto': Language(name='Esperanto',
  110. iso_code='eo',
  111. # Q, W, X, and Y not used at all
  112. use_ascii=False,
  113. charsets=['ISO-8859-3'],
  114. alphabet=(u'abcĉdefgĝhĥijĵklmnoprsŝtuŭvz'
  115. u'ABCĈDEFGĜHĤIJĴKLMNOPRSŜTUŬVZ'),
  116. wiki_start_pages=[u'Vikipedio:Ĉefpaĝo']),
  117. 'Spanish': Language(name='Spanish',
  118. iso_code='es',
  119. use_ascii=True,
  120. charsets=['ISO-8859-1', 'ISO-8859-15',
  121. 'WINDOWS-1252'],
  122. alphabet=u'ñáéíóúüÑÁÉÍÓÚÜ',
  123. wiki_start_pages=[u'Wikipedia:Portada']),
  124. 'Estonian': Language(name='Estonian',
  125. iso_code='et',
  126. use_ascii=False,
  127. charsets=['ISO-8859-4', 'ISO-8859-13',
  128. 'WINDOWS-1257'],
  129. # C, F, Š, Q, W, X, Y, Z, Ž are only for
  130. # loanwords
  131. alphabet=(u'ABDEGHIJKLMNOPRSTUVÕÄÖÜ'
  132. u'abdeghijklmnoprstuvõäöü'),
  133. wiki_start_pages=[u'Esileht']),
  134. 'Finnish': Language(name='Finnish',
  135. iso_code='fi',
  136. use_ascii=True,
  137. charsets=['ISO-8859-1', 'ISO-8859-15',
  138. 'WINDOWS-1252'],
  139. alphabet=u'ÅÄÖŠŽåäöšž',
  140. wiki_start_pages=[u'Wikipedia:Etusivu']),
  141. 'French': Language(name='French',
  142. iso_code='fr',
  143. use_ascii=True,
  144. charsets=['ISO-8859-1', 'ISO-8859-15',
  145. 'WINDOWS-1252'],
  146. alphabet=u'œàâçèéîïùûêŒÀÂÇÈÉÎÏÙÛÊ',
  147. wiki_start_pages=[u'Wikipédia:Accueil_principal',
  148. u'Bœuf (animal)']),
  149. 'Hebrew': Language(name='Hebrew',
  150. iso_code='he',
  151. use_ascii=False,
  152. charsets=['ISO-8859-8', 'WINDOWS-1255'],
  153. alphabet=u'אבגדהוזחטיךכלםמןנסעףפץצקרשתװױײ',
  154. wiki_start_pages=[u'עמוד_ראשי']),
  155. 'Croatian': Language(name='Croatian',
  156. iso_code='hr',
  157. # Q, W, X, Y are only used for foreign words.
  158. use_ascii=False,
  159. charsets=['ISO-8859-2', 'WINDOWS-1250'],
  160. alphabet=(u'abcčćdđefghijklmnoprsštuvzž'
  161. u'ABCČĆDĐEFGHIJKLMNOPRSŠTUVZŽ'),
  162. wiki_start_pages=[u'Glavna_stranica']),
  163. 'Hungarian': Language(name='Hungarian',
  164. iso_code='hu',
  165. # Q, W, X, Y are only used for foreign words.
  166. use_ascii=False,
  167. charsets=['ISO-8859-2', 'WINDOWS-1250'],
  168. alphabet=(u'abcdefghijklmnoprstuvzáéíóöőúüű'
  169. u'ABCDEFGHIJKLMNOPRSTUVZÁÉÍÓÖŐÚÜŰ'),
  170. wiki_start_pages=[u'Kezdőlap']),
  171. 'Italian': Language(name='Italian',
  172. iso_code='it',
  173. use_ascii=True,
  174. charsets=['ISO-8859-1', 'ISO-8859-15',
  175. 'WINDOWS-1252'],
  176. alphabet=u'ÀÈÉÌÒÓÙàèéìòóù',
  177. wiki_start_pages=[u'Pagina_principale']),
  178. 'Lithuanian': Language(name='Lithuanian',
  179. iso_code='lt',
  180. use_ascii=False,
  181. charsets=['ISO-8859-13', 'WINDOWS-1257',
  182. 'ISO-8859-4'],
  183. # Q, W, and X not used at all
  184. alphabet=(u'AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ'
  185. u'aąbcčdeęėfghiįyjklmnoprsštuųūvzž'),
  186. wiki_start_pages=[u'Pagrindinis_puslapis']),
  187. 'Latvian': Language(name='Latvian',
  188. iso_code='lv',
  189. use_ascii=False,
  190. charsets=['ISO-8859-13', 'WINDOWS-1257',
  191. 'ISO-8859-4'],
  192. # Q, W, X, Y are only for loanwords
  193. alphabet=(u'AĀBCČDEĒFGĢHIĪJKĶLĻMNŅOPRSŠTUŪVZŽ'
  194. u'aābcčdeēfgģhiījkķlļmnņoprsštuūvzž'),
  195. wiki_start_pages=[u'Sākumlapa']),
  196. 'Macedonian': Language(name='Macedonian',
  197. iso_code='mk',
  198. use_ascii=False,
  199. charsets=['ISO-8859-5', 'WINDOWS-1251',
  200. 'MacCyrillic', 'IBM855'],
  201. alphabet=(u'АБВГДЃЕЖЗЅИЈКЛЉМНЊОПРСТЌУФХЦЧЏШ'
  202. u'абвгдѓежзѕијклљмнњопрстќуфхцчџш'),
  203. wiki_start_pages=[u'Главна_страница']),
  204. 'Dutch': Language(name='Dutch',
  205. iso_code='nl',
  206. use_ascii=True,
  207. charsets=['ISO-8859-1', 'WINDOWS-1252'],
  208. wiki_start_pages=[u'Hoofdpagina']),
  209. 'Polish': Language(name='Polish',
  210. iso_code='pl',
  211. # Q and X are only used for foreign words.
  212. use_ascii=False,
  213. charsets=['ISO-8859-2', 'WINDOWS-1250'],
  214. alphabet=(u'AĄBCĆDEĘFGHIJKLŁMNŃOÓPRSŚTUWYZŹŻ'
  215. u'aąbcćdeęfghijklłmnńoóprsśtuwyzźż'),
  216. wiki_start_pages=[u'Wikipedia:Strona_główna']),
  217. 'Portuguese': Language(name='Portuguese',
  218. iso_code='pt',
  219. use_ascii=True,
  220. charsets=['ISO-8859-1', 'ISO-8859-15',
  221. 'WINDOWS-1252'],
  222. alphabet=u'ÁÂÃÀÇÉÊÍÓÔÕÚáâãàçéêíóôõú',
  223. wiki_start_pages=[u'Wikipédia:Página_principal']),
  224. 'Romanian': Language(name='Romanian',
  225. iso_code='ro',
  226. use_ascii=True,
  227. charsets=['ISO-8859-2', 'WINDOWS-1250'],
  228. alphabet=u'ăâîșțĂÂÎȘȚ',
  229. wiki_start_pages=[u'Pagina_principală']),
  230. 'Russian': Language(name='Russian',
  231. iso_code='ru',
  232. use_ascii=False,
  233. charsets=['ISO-8859-5', 'WINDOWS-1251',
  234. 'KOI8-R', 'MacCyrillic', 'IBM866',
  235. 'IBM855'],
  236. alphabet=(u'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'
  237. u'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'),
  238. wiki_start_pages=[u'Заглавная_страница']),
  239. 'Slovak': Language(name='Slovak',
  240. iso_code='sk',
  241. use_ascii=True,
  242. charsets=['ISO-8859-2', 'WINDOWS-1250'],
  243. alphabet=u'áäčďéíĺľňóôŕšťúýžÁÄČĎÉÍĹĽŇÓÔŔŠŤÚÝŽ',
  244. wiki_start_pages=[u'Hlavná_stránka']),
  245. 'Slovene': Language(name='Slovene',
  246. iso_code='sl',
  247. # Q, W, X, Y are only used for foreign words.
  248. use_ascii=False,
  249. charsets=['ISO-8859-2', 'WINDOWS-1250'],
  250. alphabet=(u'abcčdefghijklmnoprsštuvzž'
  251. u'ABCČDEFGHIJKLMNOPRSŠTUVZŽ'),
  252. wiki_start_pages=[u'Glavna_stran']),
  253. # Serbian can be written in both Latin and Cyrillic, but there's no
  254. # simple way to get the Latin alphabet pages from Wikipedia through
  255. # the API, so for now we just support Cyrillic.
  256. 'Serbian': Language(name='Serbian',
  257. iso_code='sr',
  258. alphabet=(u'АБВГДЂЕЖЗИЈКЛЉМНЊОПРСТЋУФХЦЧЏШ'
  259. u'абвгдђежзијклљмнњопрстћуфхцчџш'),
  260. charsets=['ISO-8859-5', 'WINDOWS-1251',
  261. 'MacCyrillic', 'IBM855'],
  262. wiki_start_pages=[u'Главна_страна']),
  263. 'Thai': Language(name='Thai',
  264. iso_code='th',
  265. use_ascii=False,
  266. charsets=['ISO-8859-11', 'TIS-620', 'CP874'],
  267. alphabet=u'กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำิีึืฺุู฿เแโใไๅๆ็่้๊๋์ํ๎๏๐๑๒๓๔๕๖๗๘๙๚๛',
  268. wiki_start_pages=[u'หน้าหลัก']),
  269. 'Turkish': Language(name='Turkish',
  270. iso_code='tr',
  271. # Q, W, and X are not used by Turkish
  272. use_ascii=False,
  273. charsets=['ISO-8859-3', 'ISO-8859-9',
  274. 'WINDOWS-1254'],
  275. alphabet=(u'abcçdefgğhıijklmnoöprsştuüvyzâîû'
  276. u'ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZÂÎÛ'),
  277. wiki_start_pages=[u'Ana_Sayfa']),
  278. 'Vietnamese': Language(name='Vietnamese',
  279. iso_code='vi',
  280. use_ascii=False,
  281. # Windows-1258 is the only common 8-bit
  282. # Vietnamese encoding supported by Python.
  283. # From Wikipedia:
  284. # For systems that lack support for Unicode,
  285. # dozens of 8-bit Vietnamese code pages are
  286. # available.[1] The most common are VISCII
  287. # (TCVN 5712:1993), VPS, and Windows-1258.[3]
  288. # Where ASCII is required, such as when
  289. # ensuring readability in plain text e-mail,
  290. # Vietnamese letters are often encoded
  291. # according to Vietnamese Quoted-Readable
  292. # (VIQR) or VSCII Mnemonic (VSCII-MNEM),[4]
  293. # though usage of either variable-width
  294. # scheme has declined dramatically following
  295. # the adoption of Unicode on the World Wide
  296. # Web.
  297. charsets=['WINDOWS-1258'],
  298. alphabet=(u'aăâbcdđeêghiklmnoôơpqrstuưvxy'
  299. u'AĂÂBCDĐEÊGHIKLMNOÔƠPQRSTUƯVXY'),
  300. wiki_start_pages=[u'Chữ_Quốc_ngữ']),
  301. }