1
0

fixer_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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"
  14. "path/filepath"
  15. "strings"
  16. "testing"
  17. )
  18. func TestStopWordCounter(t *testing.T) {
  19. testDataPath := "../../../TestData/FixTimeline"
  20. testRootDir, err := pkg.CopyTestData(testDataPath)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
  25. bFind, info, err := subParserHub.DetermineFileTypeFromFile(path.Join(testRootDir, "R&M S05E10 - English.srt"))
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if bFind == false {
  30. t.Fatal("not match sub types")
  31. }
  32. allString := strings.Join(info.OtherLines, " ")
  33. s := SubTimelineFixer{}
  34. stopWords := s.StopWordCounter(strings.ToLower(allString), 5)
  35. print(len(stopWords))
  36. println(info.Name)
  37. }
  38. func TestGetOffsetTime(t *testing.T) {
  39. testDataPath := "../../../TestData/FixTimeline"
  40. testRootDir, err := pkg.CopyTestData(testDataPath)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. testRootDirYes := filepath.Join(testRootDir, "yes")
  45. testRootDirNo := filepath.Join(testRootDir, "no")
  46. subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
  47. type args struct {
  48. enSubFile string
  49. ch_enSubFile string
  50. staticLineFileSavePath string
  51. }
  52. tests := []struct {
  53. name string
  54. args args
  55. want float64
  56. wantErr bool
  57. }{
  58. /*
  59. 这里有几个比较理想的字幕时间轴校正的示例
  60. */
  61. {name: "R&M S05E01", args: args{enSubFile: path.Join(testRootDirYes, "R&M S05E01 - English.srt"),
  62. ch_enSubFile: path.Join(testRootDirYes, "R&M S05E01 - 简英.srt"),
  63. staticLineFileSavePath: "bar.html"}, want: -6.42981818181818, wantErr: false},
  64. {name: "R&M S05E10", args: args{enSubFile: path.Join(testRootDirYes, "R&M S05E10 - English.ass"),
  65. ch_enSubFile: path.Join(testRootDirYes, "R&M S05E10 - 简英.ass"),
  66. staticLineFileSavePath: "bar.html"}, want: -6.335985401459854, wantErr: false},
  67. {name: "基地 S01E03", args: args{enSubFile: path.Join(testRootDirYes, "基地 S01E03 - English.ass"),
  68. ch_enSubFile: path.Join(testRootDirYes, "基地 S01E03 - 简英.ass"),
  69. staticLineFileSavePath: "bar.html"}, want: -32.09061538461539, wantErr: false},
  70. /*
  71. WTF,这部剧集
  72. Dan Brown's The Lost Symbol
  73. 内置的英文字幕时间轴是歪的,所以修正完了就错了
  74. */
  75. {name: "Dan Brown's The Lost Symbol - S01E01", args: args{
  76. enSubFile: path.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E01.chinese(inside).ass"),
  77. ch_enSubFile: path.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E01.chinese(简英,shooter).ass"),
  78. staticLineFileSavePath: "bar.html"},
  79. want: 1.3217821782178225, wantErr: false},
  80. {name: "Dan Brown's The Lost Symbol - S01E02", args: args{
  81. enSubFile: path.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E02.chinese(inside).ass"),
  82. ch_enSubFile: path.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E02.chinese(简英,subhd).ass"),
  83. staticLineFileSavePath: "bar.html"},
  84. want: -0.5253383458646617, wantErr: false},
  85. {name: "Dan Brown's The Lost Symbol - S01E03", args: args{
  86. enSubFile: path.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E03.chinese(inside).ass"),
  87. ch_enSubFile: path.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E03.chinese(繁英,xunlei).ass"),
  88. staticLineFileSavePath: "bar.html"},
  89. want: -0.505656, wantErr: false},
  90. {name: "Dan Brown's The Lost Symbol - S01E04", args: args{
  91. enSubFile: path.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E04.chinese(inside).ass"),
  92. ch_enSubFile: path.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E04.chinese(简英,zimuku).ass"),
  93. staticLineFileSavePath: "bar.html"},
  94. want: -0.633415, wantErr: false},
  95. /*
  96. 只有一个是字幕下载了一个错误的,其他的无需修正
  97. */
  98. {name: "Don't Breathe 2 (2021) - shooter-srt", args: args{
  99. enSubFile: path.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).srt"),
  100. ch_enSubFile: path.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,shooter).srt"),
  101. staticLineFileSavePath: "bar.html"},
  102. want: 0, wantErr: false},
  103. {name: "Don't Breathe 2 (2021) - subhd-srt error matched sub", args: args{
  104. enSubFile: path.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).srt"),
  105. ch_enSubFile: path.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,subhd).srt"),
  106. staticLineFileSavePath: "bar.html"},
  107. want: 0, wantErr: false},
  108. {name: "Don't Breathe 2 (2021) - xunlei-ass", args: args{
  109. enSubFile: path.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).ass"),
  110. ch_enSubFile: path.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,xunlei).ass"),
  111. staticLineFileSavePath: "bar.html"},
  112. want: 0, wantErr: false},
  113. {name: "Don't Breathe 2 (2021) - zimuku-ass", args: args{
  114. enSubFile: path.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).ass"),
  115. ch_enSubFile: path.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,zimuku).ass"),
  116. staticLineFileSavePath: "bar.html"},
  117. want: 0, wantErr: false},
  118. /*
  119. 基地
  120. */
  121. {name: "Foundation (2021) - S01E01", args: args{
  122. enSubFile: path.Join(testRootDirNo, "Foundation (2021) - S01E01.chinese(inside).ass"),
  123. ch_enSubFile: path.Join(testRootDirNo, "Foundation (2021) - S01E01.chinese(简英,zimuku).ass"),
  124. staticLineFileSavePath: "bar.html"},
  125. want: 0, wantErr: false},
  126. {name: "Foundation (2021) - S01E02", args: args{
  127. enSubFile: path.Join(testRootDirYes, "Foundation (2021) - S01E02.chinese(inside).ass"),
  128. ch_enSubFile: path.Join(testRootDirYes, "Foundation (2021) - S01E02.chinese(简英,subhd).ass"),
  129. staticLineFileSavePath: "bar.html"},
  130. want: -30.624840, wantErr: false},
  131. {name: "Foundation (2021) - S01E03", args: args{
  132. enSubFile: path.Join(testRootDirYes, "Foundation (2021) - S01E03.chinese(inside).ass"),
  133. ch_enSubFile: path.Join(testRootDirYes, "Foundation (2021) - S01E03.chinese(简英,subhd).ass"),
  134. staticLineFileSavePath: "bar.html"},
  135. want: -32.085037037037054, wantErr: false},
  136. {name: "Foundation (2021) - S01E04", args: args{
  137. enSubFile: path.Join(testRootDirYes, "Foundation (2021) - S01E04.chinese(inside).ass"),
  138. ch_enSubFile: path.Join(testRootDirYes, "Foundation (2021) - S01E04.chinese(简英,subhd).ass"),
  139. staticLineFileSavePath: "bar.html"},
  140. want: -36.885074, wantErr: false},
  141. {name: "Foundation (2021) - S01E04", args: args{
  142. enSubFile: path.Join(testRootDirNo, "Foundation (2021) - S01E04.chinese(inside).srt"),
  143. ch_enSubFile: path.Join(testRootDirNo, "Foundation (2021) - S01E04.chinese(繁英,shooter).srt"),
  144. staticLineFileSavePath: "bar.html"},
  145. want: 0, wantErr: false},
  146. }
  147. s := NewSubTimelineFixer(sub_timeline_fiexer.SubTimelineFixerConfig{
  148. MaxCompareDialogue: 3,
  149. MaxStartTimeDiffSD: 0.1,
  150. MinMatchedPercent: 0.1,
  151. MinOffset: 0.1,
  152. })
  153. for _, tt := range tests {
  154. t.Run(tt.name, func(t *testing.T) {
  155. bFind, infoBase, err := subParserHub.DetermineFileTypeFromFile(tt.args.enSubFile)
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. if bFind == false {
  160. t.Fatal("sub not match")
  161. }
  162. /*
  163. 这里发现一个梗,内置的英文字幕导出的时候,有可能需要合并多个 Dialogue,见
  164. internal/pkg/sub_helper/sub_helper.go 中 MergeMultiDialogue4EngSubtitle 的实现
  165. */
  166. sub_helper.MergeMultiDialogue4EngSubtitle(infoBase)
  167. bFind, infoSrc, err := subParserHub.DetermineFileTypeFromFile(tt.args.ch_enSubFile)
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. if bFind == false {
  172. t.Fatal("sub not match")
  173. }
  174. /*
  175. 这里发现一个梗,内置的英文字幕导出的时候,有可能需要合并多个 Dialogue,见
  176. internal/pkg/sub_helper/sub_helper.go 中 MergeMultiDialogue4EngSubtitle 的实现
  177. */
  178. sub_helper.MergeMultiDialogue4EngSubtitle(infoSrc)
  179. bok, got, sd, err := s.GetOffsetTime(infoBase, infoSrc, tt.args.ch_enSubFile+"-bar.html", tt.args.ch_enSubFile+".log")
  180. if (err != nil) != tt.wantErr {
  181. t.Errorf("GetOffsetTime() error = %v, wantErr %v", err, tt.wantErr)
  182. return
  183. }
  184. // 在一个正负范围内都可以接受
  185. if got > tt.want-0.1 && got < tt.want+0.1 {
  186. } else {
  187. t.Errorf("GetOffsetTime() got = %v, want %v", got, tt.want)
  188. }
  189. //if got != tt.want {
  190. // t.Errorf("GetOffsetTime() got = %v, want %v", got, tt.want)
  191. //}
  192. if bok == true && got != 0 {
  193. _, err = s.FixSubTimeline(infoSrc, got, tt.args.ch_enSubFile+"-fix"+infoBase.Ext)
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. }
  198. println(fmt.Sprintf("GetOffsetTime: %fs SD:%f", got, sd))
  199. })
  200. }
  201. }
  202. func TestTFIDF(t *testing.T) {
  203. testCorpus := []string{
  204. "The quick brown fox jumped over the lazy dog",
  205. "hey diddle diddle, the cat and the fiddle",
  206. "the cow jumped over the moon",
  207. "the little dog laughed to see such fun",
  208. "and the dish ran away with the spoon",
  209. }
  210. query := "the brown fox ran around the dog"
  211. vectoriser := nlp.NewCountVectoriser(StopWords...)
  212. transformer := nlp.NewTfidfTransformer()
  213. // set k (the number of dimensions following truncation) to 4
  214. reducer := nlp.NewTruncatedSVD(4)
  215. lsiPipeline := nlp.NewPipeline(vectoriser, transformer, reducer)
  216. // Transform the corpus into an LSI fitting the model to the documents in the process
  217. lsi, err := lsiPipeline.FitTransform(testCorpus...)
  218. if err != nil {
  219. fmt.Printf("Failed to process documents because %v", err)
  220. return
  221. }
  222. // run the query through the same pipeline that was fitted to the corpus and
  223. // to project it into the same dimensional space
  224. queryVector, err := lsiPipeline.Transform(query)
  225. if err != nil {
  226. fmt.Printf("Failed to process documents because %v", err)
  227. return
  228. }
  229. // iterate over document feature vectors (columns) in the LSI matrix and compare
  230. // with the query vector for similarity. Similarity is determined by the difference
  231. // between the angles of the vectors known as the cosine similarity
  232. highestSimilarity := -1.0
  233. var matched int
  234. _, docs := lsi.Dims()
  235. for i := 0; i < docs; i++ {
  236. similarity := pairwise.CosineSimilarity(queryVector.(mat.ColViewer).ColView(0), lsi.(mat.ColViewer).ColView(i))
  237. if similarity > highestSimilarity {
  238. matched = i
  239. highestSimilarity = similarity
  240. }
  241. }
  242. fmt.Printf("Matched '%s'", testCorpus[matched])
  243. // Output: Matched 'The quick brown fox jumped over the lazy dog'
  244. }