ass.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package ass
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/common"
  4. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser"
  5. "github.com/allanpk716/ChineseSubFinder/internal/pkg/language"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  7. "github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
  8. "github.com/emirpasic/gods/maps/treemap"
  9. "io/ioutil"
  10. "path/filepath"
  11. "strings"
  12. )
  13. type Parser struct {
  14. }
  15. func NewParser() *Parser {
  16. return &Parser{}
  17. }
  18. func (p Parser) GetParserName() string {
  19. return "ass"
  20. }
  21. /*
  22. DetermineFileTypeFromFile 确定字幕文件的类型,是双语字幕或者某一种语言等等信息
  23. 当 error 是 common.DetermineFileTypeFromFileExtNotFitASSorSSA
  24. 需要额外的处理逻辑,比如不用报错,而是跳过后续的逻辑
  25. */
  26. func (p Parser) DetermineFileTypeFromFile(filePath string) (bool, *subparser.FileInfo, error) {
  27. nowExt := filepath.Ext(filePath)
  28. if strings.ToLower(nowExt) != common.SubExtASS && strings.ToLower(nowExt) != common.SubExtSSA {
  29. return false, nil, nil
  30. }
  31. fBytes, err := ioutil.ReadFile(filePath)
  32. if err != nil {
  33. return false, nil, err
  34. }
  35. inBytes, err := language.ChangeFileCoding2UTF8(fBytes)
  36. if err != nil {
  37. return false, nil, err
  38. }
  39. return p.DetermineFileTypeFromBytes(inBytes, nowExt)
  40. }
  41. // DetermineFileTypeFromBytes 确定字幕文件的类型,是双语字幕或者某一种语言等等信息
  42. func (p Parser) DetermineFileTypeFromBytes(inBytes []byte, nowExt string) (bool, *subparser.FileInfo, error) {
  43. allString := string(inBytes)
  44. // 注意,需要替换掉 \r 不然正则表达式会有问题
  45. allString = strings.ReplaceAll(allString, "\r", "")
  46. // 找到 start end text
  47. matched := sub_parser.ReMatchDialogueASS.FindAllStringSubmatch(allString, -1)
  48. if len(matched) < 1 {
  49. log_helper.GetLogger().Debugln("DetermineFileTypeFromBytes can't found Dialogues, Skip")
  50. return false, nil, nil
  51. }
  52. subFileInfo := subparser.FileInfo{}
  53. subFileInfo.Content = string(inBytes)
  54. subFileInfo.Ext = nowExt
  55. subFileInfo.Dialogues = make([]subparser.OneDialogue, 0)
  56. // 这里需要统计一共有几个 \N,以及这个数量在整体行数中的比例,这样就知道是不是双语字幕了
  57. countLineFeed := 0
  58. // 有意义的对话统计数,排除 Style 类型
  59. usefullyDialogueCount := 0
  60. // 先进行字幕 StyleName 的出现次数排序,找到最多的,就是常规字幕的,不是特效的
  61. var nameMap = make(map[string]int)
  62. for _, oneLine := range matched {
  63. nowStyleName := oneLine[3]
  64. _, ok := nameMap[nowStyleName]
  65. if ok == false {
  66. nameMap[nowStyleName] = 1
  67. } else {
  68. nameMap[nowStyleName]++
  69. }
  70. }
  71. /*
  72. 现在可能会遇到两种可能出现的双语字幕:
  73. 1.
  74. 一个 Dialogue 中,直接描述两个语言
  75. 2.
  76. 排序的目标是找出 Name 有几种,一般来说都是 Default 一种
  77. 但是目前也会有用这个 Name 来做双语标记的
  78. 比如相同的时间点:一个 Name 是 Chs Subtitle
  79. 一个 Name 是 Eng Subtitle
  80. 那么排序来说,就应该是 Top1、2 两个
  81. 但是之前是为了剔除某一些特效动画,进行排序后只找 Top 1,但是遇到上面 2 的情况
  82. 解析就只读取到一个语言的字幕了
  83. 那么现在的解决方案就是,一开始先进行 Name 的统计。
  84. 然后统计是否有一个相同的时间段,出现了两个 Dialogue,比如:
  85. 0:01:01.00-0:01:11.00 这个时间段,一共有两个 Dialogue 使用了,然后需要统计这种情况占比所有的 Dialogue 的比例
  86. 如果比例很高,那么就认为是情况 2 的双语字幕
  87. 如果没有那么多,或者就没得。就任务是情况 1 的双语字幕,这个也不能说就是双语字幕,只不过走之前的逻辑就够了。
  88. */
  89. mapByValue := sortMapByValue(nameMap)
  90. if p.detectOneOrTwoLineDialogue(matched) == true {
  91. // 情况1
  92. usefullyDialogueCount, countLineFeed = p.oneLineSubDialogueParser1(matched, mapByValue, &subFileInfo)
  93. } else {
  94. // 情况2
  95. usefullyDialogueCount, countLineFeed = p.oneLineSubDialogueParser2(matched, mapByValue, &subFileInfo)
  96. }
  97. // 再分析
  98. // 需要判断每一个 Line 是啥语言,[语言的code]次数
  99. var langDict map[int]int
  100. langDict = make(map[int]int)
  101. // 抽取出所有的中文对话
  102. var chLines = make([]string, 0)
  103. // 抽取出所有的第二语言对话
  104. var otherLines = make([]string, 0)
  105. // 抽取出来的对话数组,为了后续用来匹配和修改时间轴
  106. var usefulDialogueExs = make([]subparser.OneDialogueEx, 0)
  107. // 在这之前需要把 subFileInfo.Dialogues 的内容填好,Lines 这里如果是单种语言应该就是一个元素,如果是双语就需要拆分成两个元素
  108. // 这样向后传递就简单了,也统一了
  109. for _, dialogue := range subFileInfo.Dialogues {
  110. language.DetectSubLangAndStatistics(dialogue, langDict, &usefulDialogueExs, &chLines, &otherLines)
  111. }
  112. // 从统计出来的字典,找出 Top 1 或者 2 的出来,然后计算出是什么语言的字幕
  113. detectLang := language.SubLangStatistics2SubLangType(float32(countLineFeed), float32(usefullyDialogueCount), langDict, chLines)
  114. subFileInfo.Lang = detectLang
  115. subFileInfo.Data = inBytes
  116. subFileInfo.DialoguesEx = usefulDialogueExs
  117. subFileInfo.CHLines = chLines
  118. subFileInfo.OtherLines = otherLines
  119. return true, &subFileInfo, nil
  120. }
  121. // oneLineSubDialogueParser1 情况 1 时候的解析器
  122. func (p Parser) oneLineSubDialogueParser1(matched [][]string, mapByValue StyleNameInfos, subFileInfo *subparser.FileInfo) (int, int) {
  123. var countLineFeed = 0
  124. var usefullyDialogueCount = 0
  125. // 先读取一次字幕文件
  126. for _, oneLine := range matched {
  127. // 排除特效内容,只统计有意义的对话部分
  128. if strings.Contains(oneLine[0], mapByValue[0].Name) == false {
  129. continue
  130. }
  131. usefullyDialogueCount++
  132. startTime := oneLine[1]
  133. endTime := oneLine[2]
  134. nowStyleName := oneLine[3]
  135. nowText := oneLine[4]
  136. odl := subparser.OneDialogue{
  137. StyleName: nowStyleName,
  138. StartTime: startTime,
  139. EndTime: endTime,
  140. }
  141. odl.Lines = make([]string, 0)
  142. countLineFeed = p.parseOneDialogueText(nowText, &odl, countLineFeed)
  143. subFileInfo.Dialogues = append(subFileInfo.Dialogues, odl)
  144. }
  145. return usefullyDialogueCount, countLineFeed
  146. }
  147. // oneLineSubDialogueParser2 情况 2 时候的解析器
  148. func (p Parser) oneLineSubDialogueParser2(matched [][]string, mapByValue StyleNameInfos, subFileInfo *subparser.FileInfo) (int, int) {
  149. var countLineFeed = 0
  150. var usefullyDialogueCount = 0
  151. //var timeMap = make(map[string]subparser.OneDialogue, 0)
  152. // 更换数据结构的原因是为了能够使用顺序,go 内置的 map 不是顺序的,是随机的,会导致后续的逻辑出问题
  153. var timeMap = treemap.NewWithStringComparator()
  154. // 先读取一次字幕文件
  155. for _, oneLine := range matched {
  156. usefullyDialogueCount++
  157. // 这里可能会统计到特效的部分,但是这里忽略这个问题,因为目标不是这个
  158. // 统计 Dialogue 的开始和结束时间
  159. startTime := oneLine[1]
  160. endTime := oneLine[2]
  161. nowStyleName := oneLine[3]
  162. nowText := oneLine[4]
  163. mergeTime := startTime + "_" + endTime
  164. value, ok := timeMap.Get(mergeTime)
  165. if ok == false {
  166. // 首次新增
  167. odl := subparser.OneDialogue{
  168. StyleName: nowStyleName,
  169. StartTime: startTime,
  170. EndTime: endTime,
  171. }
  172. odl.Lines = make([]string, 0)
  173. countLineFeed = p.parseOneDialogueText(nowText, &odl, countLineFeed)
  174. timeMap.Put(mergeTime, odl)
  175. } else {
  176. // 双语
  177. odl := value.(subparser.OneDialogue)
  178. countLineFeed = p.parseOneDialogueText(nowText, &odl, countLineFeed)
  179. timeMap.Put(mergeTime, odl)
  180. }
  181. }
  182. for _, value := range timeMap.Values() {
  183. odl := value.(subparser.OneDialogue)
  184. subFileInfo.Dialogues = append(subFileInfo.Dialogues, odl)
  185. }
  186. return usefullyDialogueCount, countLineFeed
  187. }
  188. // parseOneDialogueText 对话的对白内容解析
  189. func (p Parser) parseOneDialogueText(nowText string, odl *subparser.OneDialogue, countLineFeed int) int {
  190. // nowText 优先移除 \h 这个是替换空格, \h 是让两个词在一行,不换行显示
  191. nowText = strings.ReplaceAll(nowText, `\h`, " ")
  192. // nowText 这个需要先把 {} 花括号内的内容给移除
  193. nowText1 := sub_parser.ReMatchBrace.ReplaceAllString(nowText, "")
  194. nowText1 = sub_parser.ReMatchBracket.ReplaceAllString(nowText1, "")
  195. nowText1 = strings.TrimRight(nowText1, "\r")
  196. // 然后判断是否有 \N 或者 \n
  197. // 直接把 \n 替换为 \N 来解析
  198. nowText1 = strings.ReplaceAll(nowText1, `\n`, `\N`)
  199. if strings.Contains(nowText1, `\N`) {
  200. // 有,那么就需要再次切割,一般是双语字幕
  201. for _, matched2 := range sub_parser.ReCutDoubleLanguage.FindAllStringSubmatch(nowText1, -1) {
  202. for i, s := range matched2 {
  203. if i == 0 {
  204. continue
  205. }
  206. s = strings.ReplaceAll(s, `\N`, "")
  207. odl.Lines = append(odl.Lines, s)
  208. }
  209. }
  210. countLineFeed++
  211. } else {
  212. // 无,则可以直接添加
  213. nowText1 = strings.ReplaceAll(nowText1, `\N`, "")
  214. odl.Lines = append(odl.Lines, nowText1)
  215. }
  216. return countLineFeed
  217. }
  218. // detectOneOrTwoLineDialogue 优先检测一次字幕文件,可能存在的双语字幕的情况,是 1 还是 2 ,详细解释看调用此函数前的解释
  219. func (p Parser) detectOneOrTwoLineDialogue(matched [][]string) bool {
  220. /*
  221. 这里判断的方法粗暴一点,直接判断两个 Dialogue 都是一个时间段的比例是多少,达到了就是情况2,不是就是情况1
  222. */
  223. allDialogue := len(matched)
  224. twoLine := 0
  225. var timeMap = make(map[string]int, 0)
  226. // 先读取一次字幕文件
  227. for _, oneLine := range matched {
  228. // 这里可能会统计到特效的部分,但是这里忽略这个问题,因为目标不是这个
  229. // 统计 Dialogue 的开始和结束时间
  230. startTime := oneLine[1]
  231. endTime := oneLine[2]
  232. mergeTime := startTime + "_" + endTime
  233. _, ok := timeMap[mergeTime]
  234. if ok == false {
  235. timeMap[mergeTime] = 1
  236. } else {
  237. timeMap[mergeTime]++
  238. if timeMap[mergeTime] == 2 {
  239. twoLine++
  240. }
  241. }
  242. }
  243. // 目前看到的文件大概再 47% 以上,考虑到更多的“注释”、“特效”,至少有 38% 就够了
  244. per := float64(twoLine) / float64(allDialogue)
  245. if per > 0.38 {
  246. // 使用情况2的字幕分析方式
  247. return false
  248. }
  249. // 使用情况1的字幕分析方式
  250. return true
  251. }