language.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package language
  2. import (
  3. "github.com/abadojack/whatlanggo"
  4. "github.com/allanpk716/ChineseSubFinder/internal/logic/charset"
  5. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  6. "github.com/allanpk716/ChineseSubFinder/internal/types"
  7. "github.com/axgle/mahonia"
  8. "github.com/go-creed/sat"
  9. nzlov "github.com/nzlov/chardet"
  10. "github.com/saintfish/chardet"
  11. "strings"
  12. )
  13. // LangConverter 语言转换器
  14. func LangConverter(subLang string) types.Language {
  15. /*
  16. xunlei:未知语言、简体&英语、繁体&英语、简体、繁体、英语
  17. */
  18. if strings.Contains(subLang, types.MatchLangDouble) {
  19. // 双语 - 简英
  20. return types.ChineseSimpleEnglish
  21. } else if strings.Contains(subLang, types.MatchLangChs) {
  22. // 优先简体
  23. if strings.Contains(subLang, types.MatchLangEn) {
  24. // 简英
  25. return types.ChineseSimpleEnglish
  26. } else if strings.Contains(subLang, types.MatchLangJp) {
  27. // 简日
  28. return types.ChineseSimpleJapanese
  29. } else if strings.Contains(subLang, types.MatchLangKr) {
  30. // 简韩
  31. return types.ChineseSimpleKorean
  32. }
  33. // 默认简体中文
  34. return types.ChineseSimple
  35. } else if strings.Contains(subLang, types.MatchLangCht) {
  36. // 然后是繁体
  37. if strings.Contains(subLang, types.MatchLangEn) {
  38. // 繁英
  39. return types.ChineseTraditionalEnglish
  40. } else if strings.Contains(subLang, types.MatchLangJp) {
  41. // 繁日
  42. return types.ChineseTraditionalJapanese
  43. } else if strings.Contains(subLang, types.MatchLangKr) {
  44. // 繁韩
  45. return types.ChineseTraditionalKorean
  46. }
  47. // 默认繁体中文
  48. return types.ChineseTraditional
  49. } else if strings.Contains(subLang, types.MatchLangEn) {
  50. // 英文
  51. return types.English
  52. } else if strings.Contains(subLang, types.MatchLangJp) {
  53. // 日文
  54. return types.Japanese
  55. } else if strings.Contains(subLang, types.MatchLangKr) {
  56. // 韩文
  57. return types.Korean
  58. } else {
  59. // 都没有,则标记未知
  60. return types.Unknow
  61. }
  62. }
  63. // HasChineseLang 是否包含中文
  64. func HasChineseLang(lan types.Language) bool {
  65. switch lan {
  66. case types.ChineseSimple,
  67. types.ChineseTraditional,
  68. types.ChineseSimpleEnglish,
  69. types.ChineseTraditionalEnglish,
  70. types.ChineseSimpleJapanese,
  71. types.ChineseTraditionalJapanese,
  72. types.ChineseSimpleKorean,
  73. types.ChineseTraditionalKorean:
  74. return true
  75. default:
  76. return false
  77. }
  78. }
  79. // IsBilingualSubtitle 是否是双语字幕
  80. func IsBilingualSubtitle(lan types.Language) bool {
  81. switch lan {
  82. case types.ChineseSimpleEnglish,
  83. types.ChineseTraditionalEnglish,
  84. types.ChineseSimpleJapanese,
  85. types.ChineseTraditionalJapanese,
  86. types.ChineseSimpleKorean,
  87. types.ChineseTraditionalKorean:
  88. return true
  89. default:
  90. return false
  91. }
  92. }
  93. // Lang2EmbyName 从语言转换到 Emby 能够识别的字幕命名
  94. func Lang2EmbyName(lan types.Language) string {
  95. switch lan {
  96. case types.Unknow: // 未知语言
  97. return types.Emby_unknow
  98. case types.ChineseSimple: // 简体中文
  99. return types.Emby_chs
  100. case types.ChineseTraditional: // 繁体中文
  101. return types.Emby_cht
  102. case types.ChineseSimpleEnglish: // 简英双语字幕
  103. return types.Emby_chs_en
  104. case types.ChineseTraditionalEnglish: // 繁英双语字幕
  105. return types.Emby_cht_en
  106. case types.English: // 英文
  107. return types.Emby_en
  108. case types.Japanese: // 日语
  109. return types.Emby_jp
  110. case types.ChineseSimpleJapanese: // 简日双语字幕
  111. return types.Emby_chs_jp
  112. case types.ChineseTraditionalJapanese: // 繁日双语字幕
  113. return types.Emby_cht_jp
  114. case types.Korean: // 韩语
  115. return types.Emby_kr
  116. case types.ChineseSimpleKorean: // 简韩双语字幕
  117. return types.Emby_chs_kr
  118. case types.ChineseTraditionalKorean: // 繁韩双语字幕
  119. return types.Emby_cht_kr
  120. default:
  121. return types.Emby_unknow
  122. }
  123. }
  124. // GetLangOptions 语言识别的 Options Whitelist
  125. func GetLangOptions() whatlanggo.Options {
  126. return whatlanggo.Options{
  127. Whitelist: map[whatlanggo.Lang]bool{
  128. whatlanggo.Cmn: true, // 中文 11
  129. whatlanggo.Eng: true, // 英文 15
  130. whatlanggo.Jpn: true, // 日文 32
  131. whatlanggo.Kor: true, // 韩文 37
  132. },
  133. }
  134. }
  135. // IsWhiteListLang 是否是白名单语言
  136. func IsWhiteListLang(lang whatlanggo.Lang) bool {
  137. switch lang {
  138. // 中文 英文 日文 韩文
  139. case whatlanggo.Cmn, whatlanggo.Eng,whatlanggo.Jpn,whatlanggo.Kor:
  140. return true
  141. default:
  142. return false
  143. }
  144. }
  145. // DetectSubLangAndStatistics 检测语言然后统计
  146. func DetectSubLangAndStatistics(lines []string, langDict map[int]int, chLines *[]string) {
  147. for _, line := range lines {
  148. info := whatlanggo.DetectWithOptions(line, GetLangOptions())
  149. tmpLang := -1
  150. if IsWhiteListLang(info.Lang) == true {
  151. tmpLang = (int)(info.Lang)
  152. }
  153. // 这一种语言的 key 是否存在,不存在则新建,存在再数值 +1
  154. value, ok := langDict[tmpLang]
  155. if ok == true {
  156. // 累加
  157. value++
  158. langDict[tmpLang] = value
  159. } else {
  160. langDict[tmpLang] = 1
  161. }
  162. // 统计中文有多少行
  163. if info.Lang == whatlanggo.Cmn {
  164. *chLines = append(*chLines, line)
  165. }
  166. }
  167. }
  168. // SubLangStatistics2SubLangType 由分析的信息转换为具体是什么字幕的语言类型
  169. func SubLangStatistics2SubLangType(countLineFeed, AllLines float32, langDict map[int]int, chLines []string) types.Language {
  170. const basePer = 0.8
  171. // 是否是双语?
  172. isDouble := false
  173. perLines := countLineFeed / AllLines
  174. // 第二行字幕出现的概率大于 80% 应该稳了吧,不然还能三语?
  175. if perLines > basePer {
  176. isDouble = true
  177. }
  178. // 中文(包含了 chs 以及 cht,这一级是无法区分的,需要额外的简体和繁体区分方法)
  179. countChinese, hasChinese := langDict[int(whatlanggo.Cmn)]
  180. // 英文
  181. countEnglish, hasEnglish := langDict[int(whatlanggo.Eng)]
  182. // 日文
  183. countJapanese, hasJapanese := langDict[int(whatlanggo.Jpn)]
  184. // 韩文
  185. countKorean, hasKorean := langDict[int(whatlanggo.Kor)]
  186. // 0 - No , 1 - Chs, 2 - Cht
  187. isNoOrChsOrCht := 0
  188. isChsCount := 0
  189. if hasChinese {
  190. for _, line := range chLines {
  191. if chDict.IsChs(line, 0.9) == true {
  192. isChsCount++
  193. }
  194. }
  195. // 简体句子的占比超过 80%
  196. if float32(isChsCount) / float32(len(chLines)) > 0.8 {
  197. isNoOrChsOrCht = 1
  198. } else {
  199. isNoOrChsOrCht = 2
  200. }
  201. }
  202. // 这里有一种情况,就是双语的字幕不是在一个时间轴上的,而是分成两个时间轴的
  203. // 那么之前的 isDouble 判断就失效了,需要补判一次
  204. if isDouble == false {
  205. if hasChinese && hasEnglish {
  206. isDouble = isDoubleLang(countChinese, countEnglish)
  207. }
  208. if hasChinese && hasJapanese {
  209. isDouble = isDoubleLang(countChinese, countJapanese)
  210. }
  211. if hasChinese && hasKorean {
  212. isDouble = isDoubleLang(countChinese, countKorean)
  213. }
  214. }
  215. // 优先判断双语
  216. if isDouble == true {
  217. // 首先得在外面统计就知道是双语
  218. if hasChinese && hasEnglish {
  219. // 简体 英文
  220. return chIsChsOrCht(types.ChineseSimpleEnglish, isNoOrChsOrCht)
  221. } else if hasChinese && hasJapanese {
  222. // 简体 日文
  223. return chIsChsOrCht(types.ChineseSimpleJapanese, isNoOrChsOrCht)
  224. } else if hasChinese && hasKorean {
  225. // 简体 韩文
  226. return chIsChsOrCht(types.ChineseSimpleKorean, isNoOrChsOrCht)
  227. } else if hasChinese {
  228. return chIsChsOrCht(types.ChineseSimple, isNoOrChsOrCht)
  229. } else if hasEnglish {
  230. return types.English
  231. } else if hasJapanese {
  232. return types.Japanese
  233. } else if hasKorean {
  234. return types.Korean
  235. } else {
  236. return types.Unknow
  237. }
  238. } else {
  239. // 如果比例达不到,那么就是单语言,所以最多的那个就是当前的语言
  240. // 这里的字典是有可能出现
  241. if hasChinese {
  242. // 那么起码要占比 80% 对吧
  243. perLines = float32(countChinese) / AllLines
  244. if perLines > basePer {
  245. return chIsChsOrCht(types.ChineseSimple, isNoOrChsOrCht)
  246. }
  247. }
  248. if hasEnglish {
  249. // 那么起码要占比 80% 对吧
  250. perLines = float32(countEnglish) / AllLines
  251. if perLines > basePer {
  252. return types.English
  253. }
  254. }
  255. if hasJapanese {
  256. // 那么起码要占比 80% 对吧
  257. perLines = float32(countJapanese) / AllLines
  258. if perLines > basePer {
  259. return types.Japanese
  260. }
  261. }
  262. if hasKorean {
  263. // 那么起码要占比 80% 对吧
  264. perLines = float32(countKorean) / AllLines
  265. if perLines > basePer {
  266. return types.Korean
  267. }
  268. }
  269. return types.Unknow
  270. }
  271. }
  272. // 跟中文相关的再使用,其他的无需传入
  273. func chIsChsOrCht(language types.Language, isNoOrChsOrCht int) types.Language {
  274. // 输出原来的
  275. if isNoOrChsOrCht == 0 || isNoOrChsOrCht == 1 {
  276. return language
  277. }
  278. switch language {
  279. case types.ChineseSimpleEnglish:
  280. // 简体 英文
  281. return types.ChineseTraditionalEnglish
  282. case types.ChineseSimpleJapanese:
  283. // 简体 日文
  284. return types.ChineseTraditionalJapanese
  285. case types.ChineseSimpleKorean:
  286. // 简体 韩文
  287. return types.ChineseTraditionalKorean
  288. case types.ChineseSimple:
  289. // 简体
  290. return types.ChineseTraditional
  291. default:
  292. return language
  293. }
  294. }
  295. // IsChineseSimpleOrTraditional 暂时弃用,在 SubLangStatistics2SubLangType 检测语言,通过 unicode 做到。 从字幕的文件名称中尝试确认是简体还是繁体,不需要判断双语问题,有额外的解析器完成。只可能出现 ChineseSimple ChineseTraditional Unknow 三种情况
  296. func IsChineseSimpleOrTraditional(inputFileName string, orgLang types.Language) types.Language {
  297. if strings.Contains(inputFileName, types.SubNameKeywordChineseSimple) || strings.Contains(inputFileName, types.MatchLangChs) {
  298. // 简体中文关键词的匹配
  299. return orgLang
  300. } else if strings.Contains(inputFileName, types.SubNameKeywordTraditional) || strings.Contains(inputFileName, types.MatchLangCht) {
  301. // 繁体中文关键词的匹配
  302. if orgLang == types.ChineseSimple {
  303. // 简体 -> 繁体
  304. return types.ChineseTraditional
  305. } else if orgLang == types.ChineseSimpleEnglish {
  306. // 简体英文 -> 繁体英文
  307. return types.ChineseTraditionalEnglish
  308. } else if orgLang == types.ChineseSimpleJapanese {
  309. // 简体日文 -> 繁体日文
  310. return types.ChineseTraditionalJapanese
  311. } else if orgLang == types.ChineseSimpleKorean {
  312. // 简体韩文 -> 繁体韩文
  313. return types.ChineseTraditionalKorean
  314. }
  315. // 进来了都不是,那么就返回原来的语言
  316. return orgLang
  317. } else {
  318. // 都没有匹配上,返回原来识别出来的类型即可
  319. return orgLang
  320. }
  321. }
  322. // ConvertToString 将字符串从原始编码转换到目标编码,需要配合字符串检测编码库使用 chardet.NewTextDetector()
  323. func ConvertToString(src string, srcCode string, tagCode string) string {
  324. defer func() {
  325. if err := recover(); err != nil {
  326. log_helper.GetLogger().Errorln("ConvertToString panic:", err)
  327. }
  328. }()
  329. srcCoder := mahonia.NewDecoder(srcCode)
  330. srcResult := srcCoder.ConvertString(src)
  331. tagCoder := mahonia.NewDecoder(tagCode)
  332. _, cdata, _ := tagCoder.Translate([]byte(srcResult), true)
  333. result := string(cdata)
  334. return result
  335. }
  336. // 感谢: https://blog.csdn.net/gaoluhua/article/details/109128154,解决了编码问题
  337. // ChangeFileCoding2UTF8 自动检测文件的编码,然后转换到 UTF-8
  338. func ChangeFileCoding2UTF8(inBytes []byte) ([]byte, error) {
  339. best, err := detector.DetectBest(inBytes)
  340. utf8String := ""
  341. if err != nil {
  342. return nil, err
  343. }
  344. if best.Confidence < 90 {
  345. detectBest := nzlov.Mostlike(inBytes)
  346. utf8String, err = charset.ToUTF8(charset.Charset(detectBest), string(inBytes))
  347. } else {
  348. utf8String, err = charset.ToUTF8(charset.Charset(best.Charset), string(inBytes))
  349. }
  350. if err != nil {
  351. return nil, err
  352. }
  353. if utf8String == "" {
  354. return inBytes, nil
  355. }
  356. return []byte(utf8String), nil
  357. }
  358. func isDoubleLang(count0, count1 int) bool {
  359. if count0 >= count1 {
  360. f := float32(count0) / float32(count1)
  361. if f >= 1 && f <= 1.4 {
  362. return true
  363. } else {
  364. return false
  365. }
  366. } else {
  367. f := float32(count1) / float32(count0)
  368. if f >= 1 && f <= 1.4 {
  369. return true
  370. } else {
  371. return false
  372. }
  373. }
  374. }
  375. var (
  376. chDict = sat.DefaultDict()
  377. detector = chardet.NewTextDetector()
  378. )