fixer_test.go 18 KB

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