ass.go 9.9 KB

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