fixer_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package sub_timeline_fixer
  2. import (
  3. "fmt"
  4. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/ass"
  5. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
  8. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
  9. "github.com/allanpk716/ChineseSubFinder/internal/types/sub_timeline_fiexer"
  10. "github.com/james-bowman/nlp"
  11. "github.com/james-bowman/nlp/measures/pairwise"
  12. "gonum.org/v1/gonum/mat"
  13. "path/filepath"
  14. "strings"
  15. "testing"
  16. )
  17. func TestStopWordCounter(t *testing.T) {
  18. testDataPath := "../../../TestData/FixTimeline"
  19. testRootDir, err := pkg.CopyTestData(testDataPath)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
  24. bFind, info, err := subParserHub.DetermineFileTypeFromFile(filepath.Join(testRootDir, "R&M S05E10 - English.srt"))
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. if bFind == false {
  29. t.Fatal("not match sub types")
  30. }
  31. allString := strings.Join(info.OtherLines, " ")
  32. s := SubTimelineFixer{}
  33. stopWords := s.StopWordCounter(strings.ToLower(allString), 5)
  34. print(len(stopWords))
  35. println(info.Name)
  36. }
  37. func TestGetOffsetTime(t *testing.T) {
  38. testDataPath := "../../../TestData/FixTimeline"
  39. testRootDir, err := pkg.CopyTestData(testDataPath)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. testRootDirYes := filepath.Join(testRootDir, "yes")
  44. testRootDirNo := filepath.Join(testRootDir, "no")
  45. subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
  46. type args struct {
  47. enSubFile string
  48. ch_enSubFile string
  49. staticLineFileSavePath string
  50. }
  51. tests := []struct {
  52. name string
  53. args args
  54. want float64
  55. wantErr bool
  56. }{
  57. /*
  58. 这里有几个比较理想的字幕时间轴校正的示例
  59. */
  60. {name: "R&M S05E01", args: args{enSubFile: filepath.Join(testRootDirYes, "R&M S05E01 - English.srt"),
  61. ch_enSubFile: filepath.Join(testRootDirYes, "R&M S05E01 - 简英.srt"),
  62. staticLineFileSavePath: "bar.html"}, want: -6.42981818181818, wantErr: false},
  63. {name: "R&M S05E10", args: args{enSubFile: filepath.Join(testRootDirYes, "R&M S05E10 - English.ass"),
  64. ch_enSubFile: filepath.Join(testRootDirYes, "R&M S05E10 - 简英.ass"),
  65. staticLineFileSavePath: "bar.html"}, want: -6.335985401459854, wantErr: false},
  66. {name: "基地 S01E03", args: args{enSubFile: filepath.Join(testRootDirYes, "基地 S01E03 - English.ass"),
  67. ch_enSubFile: filepath.Join(testRootDirYes, "基地 S01E03 - 简英.ass"),
  68. staticLineFileSavePath: "bar.html"}, want: -32.09061538461539, wantErr: false},
  69. /*
  70. WTF,这部剧集
  71. Dan Brown's The Lost Symbol
  72. 内置的英文字幕时间轴是歪的,所以修正完了就错了
  73. */
  74. {name: "Dan Brown's The Lost Symbol - S01E01", args: args{
  75. enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E01.chinese(inside).ass"),
  76. ch_enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E01.chinese(简英,shooter).ass"),
  77. staticLineFileSavePath: "bar.html"},
  78. want: 1.3217821782178225, wantErr: false},
  79. {name: "Dan Brown's The Lost Symbol - S01E02", args: args{
  80. enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E02.chinese(inside).ass"),
  81. ch_enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E02.chinese(简英,subhd).ass"),
  82. staticLineFileSavePath: "bar.html"},
  83. want: -0.5253383458646617, wantErr: false},
  84. {name: "Dan Brown's The Lost Symbol - S01E03", args: args{
  85. enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E03.chinese(inside).ass"),
  86. ch_enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E03.chinese(繁英,xunlei).ass"),
  87. staticLineFileSavePath: "bar.html"},
  88. want: -0.505656, wantErr: false},
  89. {name: "Dan Brown's The Lost Symbol - S01E04", args: args{
  90. enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E04.chinese(inside).ass"),
  91. ch_enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E04.chinese(简英,zimuku).ass"),
  92. staticLineFileSavePath: "bar.html"},
  93. want: -0.633415, wantErr: false},
  94. /*
  95. 只有一个是字幕下载了一个错误的,其他的无需修正
  96. */
  97. {name: "Don't Breathe 2 (2021) - shooter-srt", args: args{
  98. enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).srt"),
  99. ch_enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,shooter).srt"),
  100. staticLineFileSavePath: "bar.html"},
  101. want: 0, wantErr: false},
  102. {name: "Don't Breathe 2 (2021) - subhd-srt error matched sub", args: args{
  103. enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).srt"),
  104. ch_enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,subhd).srt"),
  105. staticLineFileSavePath: "bar.html"},
  106. want: 0, wantErr: false},
  107. {name: "Don't Breathe 2 (2021) - xunlei-ass", args: args{
  108. enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).ass"),
  109. ch_enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,xunlei).ass"),
  110. staticLineFileSavePath: "bar.html"},
  111. want: 0, wantErr: false},
  112. {name: "Don't Breathe 2 (2021) - zimuku-ass", args: args{
  113. enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).ass"),
  114. ch_enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,zimuku).ass"),
  115. staticLineFileSavePath: "bar.html"},
  116. want: 0, wantErr: false},
  117. /*
  118. 基地
  119. */
  120. {name: "Foundation (2021) - S01E01", args: args{
  121. enSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E01.chinese(inside).ass"),
  122. ch_enSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E01.chinese(简英,zimuku).ass"),
  123. staticLineFileSavePath: "bar.html"},
  124. want: 0, wantErr: false},
  125. {name: "Foundation (2021) - S01E02", args: args{
  126. enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E02.chinese(inside).ass"),
  127. ch_enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E02.chinese(简英,subhd).ass"),
  128. staticLineFileSavePath: "bar.html"},
  129. want: -30.624840, wantErr: false},
  130. {name: "Foundation (2021) - S01E03", args: args{
  131. enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E03.chinese(inside).ass"),
  132. ch_enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E03.chinese(简英,subhd).ass"),
  133. staticLineFileSavePath: "bar.html"},
  134. want: -32.085037037037054, wantErr: false},
  135. {name: "Foundation (2021) - S01E04", args: args{
  136. enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E04.chinese(inside).ass"),
  137. ch_enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E04.chinese(简英,subhd).ass"),
  138. staticLineFileSavePath: "bar.html"},
  139. want: -36.885074, wantErr: false},
  140. {name: "Foundation (2021) - S01E04", args: args{
  141. enSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E04.chinese(inside).srt"),
  142. ch_enSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E04.chinese(繁英,shooter).srt"),
  143. staticLineFileSavePath: "bar.html"},
  144. want: 0, wantErr: false},
  145. /*
  146. The Card Counter
  147. */
  148. {name: "The Card Counter", args: args{
  149. enSubFile: filepath.Join(testRootDirNo, "The Card Counter (2021).chinese(inside).ass"),
  150. ch_enSubFile: filepath.Join(testRootDirNo, "The Card Counter (2021).chinese(简英,xunlei).ass"),
  151. staticLineFileSavePath: "bar.html"},
  152. want: 0, wantErr: false},
  153. {name: "The Card Counter", args: args{
  154. enSubFile: filepath.Join(testRootDirNo, "The Card Counter (2021).chinese(inside).ass"),
  155. ch_enSubFile: filepath.Join(testRootDirNo, "The Card Counter (2021).chinese(简英,shooter).ass"),
  156. staticLineFileSavePath: "bar.html"},
  157. want: 0.224844, wantErr: false},
  158. /*
  159. Kingdom Ashin of the North
  160. */
  161. {name: "Kingdom Ashin of the North - error matched sub", args: args{
  162. enSubFile: filepath.Join(testRootDirNo, "Kingdom Ashin of the North (2021).chinese(inside).ass"),
  163. ch_enSubFile: filepath.Join(testRootDirNo, "Kingdom Ashin of the North (2021).chinese(简英,subhd).ass"),
  164. staticLineFileSavePath: "bar.html"},
  165. want: 0, wantErr: false},
  166. /*
  167. Only Murders in the Building
  168. */
  169. {name: "Only Murders in the Building - S01E06", args: args{
  170. enSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E06.chinese(inside).ass"),
  171. ch_enSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E06.chinese(简英,subhd).ass"),
  172. staticLineFileSavePath: "bar.html"},
  173. want: 0, wantErr: false},
  174. {name: "Only Murders in the Building - S01E08", args: args{
  175. enSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E08.chinese(inside).ass"),
  176. ch_enSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E08.chinese(简英,subhd).ass"),
  177. staticLineFileSavePath: "bar.html"},
  178. want: 0, wantErr: false},
  179. /*
  180. Ted Lasso
  181. */
  182. {name: "Ted Lasso - S02E09", args: args{
  183. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(inside).ass"),
  184. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(简英,subhd).ass"),
  185. staticLineFileSavePath: "bar.html"},
  186. want: 0, wantErr: false},
  187. {name: "Ted Lasso - S02E09", args: args{
  188. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(inside).ass"),
  189. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(简英,zimuku).ass"),
  190. staticLineFileSavePath: "bar.html"},
  191. want: 0, wantErr: false},
  192. {name: "Ted Lasso - S02E10", args: args{
  193. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(inside).ass"),
  194. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(简英,subhd).ass"),
  195. staticLineFileSavePath: "bar.html"},
  196. want: 0, wantErr: false},
  197. {name: "Ted Lasso - S02E10", args: args{
  198. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(inside).ass"),
  199. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(简英,zimuku).ass"),
  200. staticLineFileSavePath: "bar.html"},
  201. want: 0, wantErr: false},
  202. {name: "Ted Lasso - S02E10", args: args{
  203. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(inside).ass"),
  204. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(简英,shooter).ass"),
  205. staticLineFileSavePath: "bar.html"},
  206. want: 0, wantErr: false},
  207. {name: "Ted Lasso - S02E11", args: args{
  208. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(inside).ass"),
  209. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(简英,subhd).ass"),
  210. staticLineFileSavePath: "bar.html"},
  211. want: 0, wantErr: false},
  212. {name: "Ted Lasso - S02E11", args: args{
  213. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(inside).ass"),
  214. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(简英,zimuku).ass"),
  215. staticLineFileSavePath: "bar.html"},
  216. want: 0, wantErr: false},
  217. {name: "Ted Lasso - S02E12", args: args{
  218. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(inside).ass"),
  219. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(简英,subhd).ass"),
  220. staticLineFileSavePath: "bar.html"},
  221. want: 0, wantErr: false},
  222. {name: "Ted Lasso - S02E12", args: args{
  223. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(inside).ass"),
  224. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(简英,shooter).ass"),
  225. staticLineFileSavePath: "bar.html"},
  226. want: 0, wantErr: false},
  227. /*
  228. The Protégé
  229. */
  230. {name: "The Protégé", args: args{
  231. enSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(inside).ass"),
  232. ch_enSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(简英,zimuku).ass"),
  233. staticLineFileSavePath: "bar.html"},
  234. want: 0, wantErr: false},
  235. {name: "The Protégé", args: args{
  236. enSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(inside).srt"),
  237. ch_enSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(简英,shooter).srt"),
  238. staticLineFileSavePath: "bar.html"},
  239. want: 0, wantErr: false},
  240. /*
  241. The Witcher Nightmare of the Wolf
  242. */
  243. {name: "The Witcher Nightmare of the Wolf", args: args{
  244. enSubFile: filepath.Join(testRootDirNo, "The Witcher Nightmare of the Wolf.chinese(inside).ass"),
  245. ch_enSubFile: filepath.Join(testRootDirNo, "The Witcher Nightmare of the Wolf.chinese(简英,zimuku).ass"),
  246. staticLineFileSavePath: "bar.html"},
  247. want: 0, wantErr: false},
  248. /*
  249. What If…!
  250. */
  251. {name: "What If…! - S01E07", args: args{
  252. enSubFile: filepath.Join(testRootDirNo, "What If…! - S01E07.chinese(inside).ass"),
  253. ch_enSubFile: filepath.Join(testRootDirNo, "What If…! - S01E07.chinese(简英,subhd).ass"),
  254. staticLineFileSavePath: "bar.html"},
  255. want: 0, wantErr: false},
  256. {name: "What If…! - S01E09", args: args{
  257. enSubFile: filepath.Join(testRootDirNo, "What If…! - S01E09.chinese(inside).srt"),
  258. ch_enSubFile: filepath.Join(testRootDirNo, "What If…! - S01E09.chinese(简英,shooter).srt"),
  259. staticLineFileSavePath: "bar.html"},
  260. want: 0, wantErr: false},
  261. }
  262. s := NewSubTimelineFixer(sub_timeline_fiexer.SubTimelineFixerConfig{
  263. MaxCompareDialogue: 3,
  264. MaxStartTimeDiffSD: 0.1,
  265. MinMatchedPercent: 0.1,
  266. MinOffset: 0.1,
  267. })
  268. for _, tt := range tests {
  269. t.Run(tt.name, func(t *testing.T) {
  270. bFind, infoBase, err := subParserHub.DetermineFileTypeFromFile(tt.args.enSubFile)
  271. if err != nil {
  272. t.Fatal(err)
  273. }
  274. if bFind == false {
  275. t.Fatal("sub not match")
  276. }
  277. /*
  278. 这里发现一个梗,内置的英文字幕导出的时候,有可能需要合并多个 Dialogue,见
  279. internal/pkg/sub_helper/sub_helper.go 中 MergeMultiDialogue4EngSubtitle 的实现
  280. */
  281. sub_helper.MergeMultiDialogue4EngSubtitle(infoBase)
  282. bFind, infoSrc, err := subParserHub.DetermineFileTypeFromFile(tt.args.ch_enSubFile)
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. if bFind == false {
  287. t.Fatal("sub not match")
  288. }
  289. /*
  290. 这里发现一个梗,内置的英文字幕导出的时候,有可能需要合并多个 Dialogue,见
  291. internal/pkg/sub_helper/sub_helper.go 中 MergeMultiDialogue4EngSubtitle 的实现
  292. */
  293. sub_helper.MergeMultiDialogue4EngSubtitle(infoSrc)
  294. bok, got, sd, err := s.GetOffsetTime(infoBase, infoSrc, tt.args.ch_enSubFile+"-bar.html", tt.args.ch_enSubFile+".log")
  295. if (err != nil) != tt.wantErr {
  296. t.Errorf("GetOffsetTime() error = %v, wantErr %v", err, tt.wantErr)
  297. return
  298. }
  299. // 在一个正负范围内都可以接受
  300. if got > tt.want-0.1 && got < tt.want+0.1 {
  301. } else {
  302. t.Errorf("GetOffsetTime() got = %v, want %v", got, tt.want)
  303. }
  304. //if got != tt.want {
  305. // t.Errorf("GetOffsetTime() got = %v, want %v", got, tt.want)
  306. //}
  307. if bok == true && got != 0 {
  308. _, err = s.FixSubTimeline(infoSrc, got, tt.args.ch_enSubFile+FixMask+infoBase.Ext)
  309. if err != nil {
  310. t.Fatal(err)
  311. }
  312. }
  313. println(fmt.Sprintf("GetOffsetTime: %fs SD:%f", got, sd))
  314. })
  315. }
  316. }
  317. func TestTFIDF(t *testing.T) {
  318. testCorpus := []string{
  319. "The quick brown fox jumped over the lazy dog",
  320. "hey diddle diddle, the cat and the fiddle",
  321. "the cow jumped over the moon",
  322. "the little dog laughed to see such fun",
  323. "and the dish ran away with the spoon",
  324. }
  325. query := "the brown fox ran around the dog"
  326. vectoriser := nlp.NewCountVectoriser(StopWords...)
  327. transformer := nlp.NewTfidfTransformer()
  328. // set k (the number of dimensions following truncation) to 4
  329. reducer := nlp.NewTruncatedSVD(4)
  330. lsiPipeline := nlp.NewPipeline(vectoriser, transformer, reducer)
  331. // Transform the corpus into an LSI fitting the model to the documents in the process
  332. lsi, err := lsiPipeline.FitTransform(testCorpus...)
  333. if err != nil {
  334. fmt.Printf("Failed to process documents because %v", err)
  335. return
  336. }
  337. // run the query through the same pipeline that was fitted to the corpus and
  338. // to project it into the same dimensional space
  339. queryVector, err := lsiPipeline.Transform(query)
  340. if err != nil {
  341. fmt.Printf("Failed to process documents because %v", err)
  342. return
  343. }
  344. // iterate over document feature vectors (columns) in the LSI matrix and compare
  345. // with the query vector for similarity. Similarity is determined by the difference
  346. // between the angles of the vectors known as the cosine similarity
  347. highestSimilarity := -1.0
  348. var matched int
  349. _, docs := lsi.Dims()
  350. for i := 0; i < docs; i++ {
  351. similarity := pairwise.CosineSimilarity(queryVector.(mat.ColViewer).ColView(0), lsi.(mat.ColViewer).ColView(i))
  352. if similarity > highestSimilarity {
  353. matched = i
  354. highestSimilarity = similarity
  355. }
  356. }
  357. fmt.Printf("Matched '%s'", testCorpus[matched])
  358. // Output: Matched 'The quick brown fox jumped over the lazy dog'
  359. }