multi_byte.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package chardet
  2. import (
  3. "errors"
  4. "math"
  5. )
  6. type recognizerMultiByte struct {
  7. charset string
  8. language string
  9. decoder charDecoder
  10. commonChars []uint16
  11. }
  12. type charDecoder interface {
  13. DecodeOneChar([]byte) (c uint16, remain []byte, err error)
  14. }
  15. func (r *recognizerMultiByte) Match(input *recognizerInput) (output recognizerOutput) {
  16. return recognizerOutput{
  17. Charset: r.charset,
  18. Language: r.language,
  19. Confidence: r.matchConfidence(input),
  20. }
  21. }
  22. func (r *recognizerMultiByte) matchConfidence(input *recognizerInput) int {
  23. raw := input.raw
  24. var c uint16
  25. var err error
  26. var totalCharCount, badCharCount, singleByteCharCount, doubleByteCharCount, commonCharCount int
  27. for c, raw, err = r.decoder.DecodeOneChar(raw); len(raw) > 0; c, raw, err = r.decoder.DecodeOneChar(raw) {
  28. totalCharCount++
  29. if err != nil {
  30. badCharCount++
  31. } else if c <= 0xFF {
  32. singleByteCharCount++
  33. } else {
  34. doubleByteCharCount++
  35. if r.commonChars != nil && binarySearch(r.commonChars, c) {
  36. commonCharCount++
  37. }
  38. }
  39. if badCharCount >= 2 && badCharCount*5 >= doubleByteCharCount {
  40. return 0
  41. }
  42. }
  43. if doubleByteCharCount <= 10 && badCharCount == 0 {
  44. if doubleByteCharCount == 0 && totalCharCount < 10 {
  45. return 0
  46. } else {
  47. return 10
  48. }
  49. }
  50. if doubleByteCharCount < 20*badCharCount {
  51. return 0
  52. }
  53. if r.commonChars == nil {
  54. confidence := 30 + doubleByteCharCount - 20*badCharCount
  55. if confidence > 100 {
  56. confidence = 100
  57. }
  58. return confidence
  59. }
  60. maxVal := math.Log(float64(doubleByteCharCount) / 4)
  61. scaleFactor := 90 / maxVal
  62. confidence := int(math.Log(float64(commonCharCount)+1)*scaleFactor + 10)
  63. if confidence > 100 {
  64. confidence = 100
  65. }
  66. if confidence < 0 {
  67. confidence = 0
  68. }
  69. return confidence
  70. }
  71. func binarySearch(l []uint16, c uint16) bool {
  72. start := 0
  73. end := len(l) - 1
  74. for start <= end {
  75. mid := (start + end) / 2
  76. if c == l[mid] {
  77. return true
  78. } else if c < l[mid] {
  79. end = mid - 1
  80. } else {
  81. start = mid + 1
  82. }
  83. }
  84. return false
  85. }
  86. var eobError = errors.New("End of input buffer")
  87. var badCharError = errors.New("Decode a bad char")
  88. type charDecoder_sjis struct {
  89. }
  90. func (charDecoder_sjis) DecodeOneChar(input []byte) (c uint16, remain []byte, err error) {
  91. if len(input) == 0 {
  92. return 0, nil, eobError
  93. }
  94. first := input[0]
  95. c = uint16(first)
  96. remain = input[1:]
  97. if first <= 0x7F || (first > 0xA0 && first <= 0xDF) {
  98. return
  99. }
  100. if len(remain) == 0 {
  101. return c, remain, badCharError
  102. }
  103. second := remain[0]
  104. remain = remain[1:]
  105. c = c<<8 | uint16(second)
  106. if (second >= 0x40 && second <= 0x7F) || (second >= 0x80 && second <= 0xFE) {
  107. } else {
  108. err = badCharError
  109. }
  110. return
  111. }
  112. var commonChars_sjis = []uint16{
  113. 0x8140, 0x8141, 0x8142, 0x8145, 0x815b, 0x8169, 0x816a, 0x8175, 0x8176, 0x82a0,
  114. 0x82a2, 0x82a4, 0x82a9, 0x82aa, 0x82ab, 0x82ad, 0x82af, 0x82b1, 0x82b3, 0x82b5,
  115. 0x82b7, 0x82bd, 0x82be, 0x82c1, 0x82c4, 0x82c5, 0x82c6, 0x82c8, 0x82c9, 0x82cc,
  116. 0x82cd, 0x82dc, 0x82e0, 0x82e7, 0x82e8, 0x82e9, 0x82ea, 0x82f0, 0x82f1, 0x8341,
  117. 0x8343, 0x834e, 0x834f, 0x8358, 0x835e, 0x8362, 0x8367, 0x8375, 0x8376, 0x8389,
  118. 0x838a, 0x838b, 0x838d, 0x8393, 0x8e96, 0x93fa, 0x95aa,
  119. }
  120. func newRecognizer_sjis() *recognizerMultiByte {
  121. return &recognizerMultiByte{
  122. "Shift_JIS",
  123. "ja",
  124. charDecoder_sjis{},
  125. commonChars_sjis,
  126. }
  127. }
  128. type charDecoder_euc struct {
  129. }
  130. func (charDecoder_euc) DecodeOneChar(input []byte) (c uint16, remain []byte, err error) {
  131. if len(input) == 0 {
  132. return 0, nil, eobError
  133. }
  134. first := input[0]
  135. remain = input[1:]
  136. c = uint16(first)
  137. if first <= 0x8D {
  138. return uint16(first), remain, nil
  139. }
  140. if len(remain) == 0 {
  141. return 0, nil, eobError
  142. }
  143. second := remain[0]
  144. remain = remain[1:]
  145. c = c<<8 | uint16(second)
  146. if first >= 0xA1 && first <= 0xFE {
  147. if second < 0xA1 {
  148. err = badCharError
  149. }
  150. return
  151. }
  152. if first == 0x8E {
  153. if second < 0xA1 {
  154. err = badCharError
  155. }
  156. return
  157. }
  158. if first == 0x8F {
  159. if len(remain) == 0 {
  160. return 0, nil, eobError
  161. }
  162. third := remain[0]
  163. remain = remain[1:]
  164. c = c<<0 | uint16(third)
  165. if third < 0xa1 {
  166. err = badCharError
  167. }
  168. }
  169. return
  170. }
  171. var commonChars_euc_jp = []uint16{
  172. 0xa1a1, 0xa1a2, 0xa1a3, 0xa1a6, 0xa1bc, 0xa1ca, 0xa1cb, 0xa1d6, 0xa1d7, 0xa4a2,
  173. 0xa4a4, 0xa4a6, 0xa4a8, 0xa4aa, 0xa4ab, 0xa4ac, 0xa4ad, 0xa4af, 0xa4b1, 0xa4b3,
  174. 0xa4b5, 0xa4b7, 0xa4b9, 0xa4b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 0xbaf1, 0xbbe7, 0xbbf3, 0xbbfd, 0xbcad, 0xbcba,
  189. 0xbcd2, 0xbcf6, 0xbdba, 0xbdc0, 0xbdc3, 0xbdc5, 0xbec6, 0xbec8, 0xbedf, 0xbeee,
  190. 0xbef8, 0xbefa, 0xbfa1, 0xbfa9, 0xbfc0, 0xbfe4, 0xbfeb, 0xbfec, 0xbff8, 0xc0a7,
  191. 0xc0af, 0xc0b8, 0xc0ba, 0xc0bb, 0xc0bd, 0xc0c7, 0xc0cc, 0xc0ce, 0xc0cf, 0xc0d6,
  192. 0xc0da, 0xc0e5, 0xc0fb, 0xc0fc, 0xc1a4, 0xc1a6, 0xc1b6, 0xc1d6, 0xc1df, 0xc1f6,
  193. 0xc1f8, 0xc4a1, 0xc5cd, 0xc6ae, 0xc7cf, 0xc7d1, 0xc7d2, 0xc7d8, 0xc7e5, 0xc8ad,
  194. }
  195. func newRecognizer_euc_jp() *recognizerMultiByte {
  196. return &recognizerMultiByte{
  197. "EUC-JP",
  198. "ja",
  199. charDecoder_euc{},
  200. commonChars_euc_jp,
  201. }
  202. }
  203. func newRecognizer_euc_kr() *recognizerMultiByte {
  204. return &recognizerMultiByte{
  205. "EUC-KR",
  206. "ko",
  207. charDecoder_euc{},
  208. commonChars_euc_kr,
  209. }
  210. }
  211. type charDecoder_big5 struct {
  212. }
  213. func (charDecoder_big5) DecodeOneChar(input []byte) (c uint16, remain []byte, err error) {
  214. if len(input) == 0 {
  215. return 0, nil, eobError
  216. }
  217. first := input[0]
  218. remain = input[1:]
  219. c = uint16(first)
  220. if first <= 0x7F || first == 0xFF {
  221. return
  222. }
  223. if len(remain) == 0 {
  224. return c, nil, eobError
  225. }
  226. second := remain[0]
  227. remain = remain[1:]
  228. c = c<<8 | uint16(second)
  229. if second < 0x40 || second == 0x7F || second == 0xFF {
  230. err = badCharError
  231. }
  232. return
  233. }
  234. var commonChars_big5 = []uint16{
  235. 0xa140, 0xa141, 0xa142, 0xa143, 0xa147, 0xa149, 0xa175, 0xa176, 0xa440, 0xa446,
  236. 0xa447, 0xa448, 0xa451, 0xa454, 0xa457, 0xa464, 0xa46a, 0xa46c, 0xa477, 0xa4a3,
  237. 0xa4a4, 0xa4a7, 0xa4c1, 0xa4ce, 0xa4d1, 0xa4df, 0xa4e8, 0xa4fd, 0xa540, 0xa548,
  238. 0xa558, 0xa569, 0xa5cd, 0xa5e7, 0xa657, 0xa661, 0xa662, 0xa668, 0xa670, 0xa6a8,
  239. 0xa6b3, 0xa6b9, 0xa6d3, 0xa6db, 0xa6e6, 0xa6f2, 0xa740, 0xa751, 0xa759, 0xa7da,
  240. 0xa8a3, 0xa8a5, 0xa8ad, 0xa8d1, 0xa8d3, 0xa8e4, 0xa8fc, 0xa9c0, 0xa9d2, 0xa9f3,
  241. 0xaa6b, 0xaaba, 0xaabe, 0xaacc, 0xaafc, 0xac47, 0xac4f, 0xacb0, 0xacd2, 0xad59,
  242. 0xaec9, 0xafe0, 0xb0ea, 0xb16f, 0xb2b3, 0xb2c4, 0xb36f, 0xb44c, 0xb44e, 0xb54c,
  243. 0xb5a5, 0xb5bd, 0xb5d0, 0xb5d8, 0xb671, 0xb7ed, 0xb867, 0xb944, 0xbad8, 0xbb44,
  244. 0xbba1, 0xbdd1, 0xc2c4, 0xc3b9, 0xc440, 0xc45f,
  245. }
  246. func newRecognizer_big5() *recognizerMultiByte {
  247. return &recognizerMultiByte{
  248. "Big5",
  249. "zh",
  250. charDecoder_big5{},
  251. commonChars_big5,
  252. }
  253. }
  254. type charDecoder_gb_18030 struct {
  255. }
  256. func (charDecoder_gb_18030) DecodeOneChar(input []byte) (c uint16, remain []byte, err error) {
  257. if len(input) == 0 {
  258. return 0, nil, eobError
  259. }
  260. first := input[0]
  261. remain = input[1:]
  262. c = uint16(first)
  263. if first <= 0x80 {
  264. return
  265. }
  266. if len(remain) == 0 {
  267. return 0, nil, eobError
  268. }
  269. second := remain[0]
  270. remain = remain[1:]
  271. c = c<<8 | uint16(second)
  272. if first >= 0x81 && first <= 0xFE {
  273. if (second >= 0x40 && second <= 0x7E) || (second >= 0x80 && second <= 0xFE) {
  274. return
  275. }
  276. if second >= 0x30 && second <= 0x39 {
  277. if len(remain) == 0 {
  278. return 0, nil, eobError
  279. }
  280. third := remain[0]
  281. remain = remain[1:]
  282. if third >= 0x81 && third <= 0xFE {
  283. if len(remain) == 0 {
  284. return 0, nil, eobError
  285. }
  286. fourth := remain[0]
  287. remain = remain[1:]
  288. if fourth >= 0x30 && fourth <= 0x39 {
  289. c = c<<16 | uint16(third)<<8 | uint16(fourth)
  290. return
  291. }
  292. }
  293. }
  294. err = badCharError
  295. }
  296. return
  297. }
  298. var commonChars_gb_18030 = []uint16{
  299. 0xa1a1, 0xa1a2, 0xa1a3, 0xa1a4, 0xa1b0, 0xa1b1, 0xa1f1, 0xa1f3, 0xa3a1, 0xa3ac,
  300. 0xa3ba, 0xb1a8, 0xb1b8, 0xb1be, 0xb2bb, 0xb3c9, 0xb3f6, 0xb4f3, 0xb5bd, 0xb5c4,
  301. 0xb5e3, 0xb6af, 0xb6d4, 0xb6e0, 0xb7a2, 0xb7a8, 0xb7bd, 0xb7d6, 0xb7dd, 0xb8b4,
  302. 0xb8df, 0xb8f6, 0xb9ab, 0xb9c9, 0xb9d8, 0xb9fa, 0xb9fd, 0xbacd, 0xbba7, 0xbbd6,
  303. 0xbbe1, 0xbbfa, 0xbcbc, 0xbcdb, 0xbcfe, 0xbdcc, 0xbecd, 0xbedd, 0xbfb4, 0xbfc6,
  304. 0xbfc9, 0xc0b4, 0xc0ed, 0xc1cb, 0xc2db, 0xc3c7, 0xc4dc, 0xc4ea, 0xc5cc, 0xc6f7,
  305. 0xc7f8, 0xc8ab, 0xc8cb, 0xc8d5, 0xc8e7, 0xc9cf, 0xc9fa, 0xcab1, 0xcab5, 0xcac7,
  306. 0xcad0, 0xcad6, 0xcaf5, 0xcafd, 0xccec, 0xcdf8, 0xceaa, 0xcec4, 0xced2, 0xcee5,
  307. 0xcfb5, 0xcfc2, 0xcfd6, 0xd0c2, 0xd0c5, 0xd0d0, 0xd0d4, 0xd1a7, 0xd2aa, 0xd2b2,
  308. 0xd2b5, 0xd2bb, 0xd2d4, 0xd3c3, 0xd3d0, 0xd3fd, 0xd4c2, 0xd4da, 0xd5e2, 0xd6d0,
  309. }
  310. func newRecognizer_gb_18030() *recognizerMultiByte {
  311. return &recognizerMultiByte{
  312. "GB18030",
  313. "zh",
  314. charDecoder_gb_18030{},
  315. commonChars_gb_18030,
  316. }
  317. }