Browse Source

完成初版的预研检测,以及修复 switch 的bug

Signed-off-by: allan716 <[email protected]>
allan716 4 years ago
parent
commit
6b9999321a
5 changed files with 117 additions and 46 deletions
  1. 98 12
      common/lang.go
  2. 1 5
      common/subType.go
  3. 12 27
      sub_parser/ass/ass.go
  4. 2 1
      sub_parser/ass/ass_test.go
  5. 4 1
      sub_parser/subFileInfo.go

+ 98 - 12
common/lang.go

@@ -1,6 +1,9 @@
 package common
 
-import "strings"
+import (
+	"github.com/abadojack/whatlanggo"
+	"strings"
+)
 
 // LangConverter 语言转换器
 func LangConverter(subLang string) Language {
@@ -50,19 +53,102 @@ func LangConverter(subLang string) Language {
 	}
 }
 
+// HasChineseLang 是否包含中文
 func HasChineseLang(lan Language) bool {
 	switch lan {
-	case ChineseSimple:
-	case ChineseTraditional:
-	case ChineseSimpleEnglish:
-	case ChineseTraditionalEnglish:
-	case ChineseSimpleJapanese:
-	case ChineseTraditionalJapanese:
-	case ChineseSimpleKorean:
-	case ChineseTraditionalKorean:
+	case ChineseSimple,
+	ChineseTraditional,
+
+	ChineseSimpleEnglish,
+	ChineseTraditionalEnglish,
+
+	ChineseSimpleJapanese,
+	ChineseTraditionalJapanese,
+
+	ChineseSimpleKorean,
+	ChineseTraditionalKorean:
+		return true
+	default:
+		return false
+	}
+}
+
+// GetLangOptions 语言识别的 Options Whitelist
+func GetLangOptions() whatlanggo.Options {
+	return whatlanggo.Options{
+		Whitelist: map[whatlanggo.Lang]bool{
+			whatlanggo.Cmn: true,	// 中文	11
+			whatlanggo.Eng: true,	// 英文	15
+			whatlanggo.Jpn: true,	// 日文	32
+			whatlanggo.Kor: true,	// 韩文	37
+		},
+	}
+}
+// IsWhiteListLang 是否是白名单语言
+func IsWhiteListLang(lang whatlanggo.Lang) bool {
+	switch lang {
+	// 中文 英文 日文 韩文
+	case whatlanggo.Cmn, whatlanggo.Eng,whatlanggo.Jpn,whatlanggo.Kor:
 		return true
+	default:
+		return false
+	}
+}
+
+// DetectSubLangAndStatistics 检测语言然后统计
+func DetectSubLangAndStatistics(lines []string, langDict map[int]int) {
+	for _, line := range lines {
+		info := whatlanggo.DetectWithOptions(line, GetLangOptions())
+		tmpLang := -1
+		if IsWhiteListLang(info.Lang) == true {
+			tmpLang = (int)(info.Lang)
+		}
+		// 这一种语言的 key 是否存在,不存在则新建,存在再数值 +1
+		value, ok := langDict[tmpLang]
+		if ok == true {
+			// 累加
+			value++
+			langDict[tmpLang] = value
+		} else {
+			langDict[tmpLang] = 1
+		}
+	}
+}
+
+
+// SubLangStatistics2SubLangType 由分析的信息转换为具体是什么字幕的语言类型
+func SubLangStatistics2SubLangType(isDouble bool, langDict map[int]int) Language {
+	// TODO 现在是没有很好的办法去识别是简体还是繁体中文的,所以···
+	// 中文
+	_, hasChinese := langDict[int(whatlanggo.Cmn)]
+	// 英文
+	_, hasEnglish := langDict[int(whatlanggo.Eng)]
+	// 日文
+	_, hasJapanese := langDict[int(whatlanggo.Jpn)]
+	// 韩文
+	_, hasKorean := langDict[int(whatlanggo.Kor)]
+
+	// 优先判断双语
+	if hasChinese && hasEnglish {
+		// 简体	英文
+		return ChineseSimpleEnglish
+	} else if hasChinese && hasJapanese {
+		// 简体 日文
+		return ChineseSimpleJapanese
+	} else if hasChinese && hasKorean {
+		// 简体 韩文
+		return ChineseSimpleKorean
+	} else if hasChinese {
+		return ChineseSimple
+	} else if hasEnglish {
+		return English
+	} else if hasJapanese {
+		return Japanese
+	} else if hasKorean {
+		return Korean
+	} else {
+		return Unknow
 	}
-	return false
 }
 
 // Language 语言类型,注意,这里默认还是查找的是中文字幕,只不过下载的时候可能附带了其他的
@@ -121,7 +207,7 @@ func (l Language) String() string {
 		return MatchLangChsKr
 	case ChineseTraditionalKorean:
 		return MatchLangChtKr
+	default:
+		return MathLangChnUnknow
 	}
-
-	return MathLangChnUnknow
 }

+ 1 - 5
common/subType.go

@@ -20,15 +20,11 @@ func IsSubTypeWanted(subName string) bool {
 func IsSubExtWanted(subName string) bool {
 	inExt := filepath.Ext(subName)
 	switch inExt {
-	case SubExtSSA:
-	case SubExtASS:
-	case SubExtSRT:
+	case SubExtSSA,SubExtASS,SubExtSRT:
 		return true
 	default:
 		return false
 	}
-
-	return false
 }
 
 const (

+ 12 - 27
sub_parser/ass/ass.go

@@ -1,7 +1,6 @@
 package ass
 
 import (
-	"github.com/abadojack/whatlanggo"
 	"github.com/allanpk716/ChineseSubFinder/common"
 	"github.com/allanpk716/ChineseSubFinder/sub_parser"
 	"io/ioutil"
@@ -11,37 +10,27 @@ import (
 )
 
 type Parser struct {
-	langOptions whatlanggo.Options 	// Whitelist
 }
 
 func NewParser() *Parser {
-	p := Parser{}
-	p.langOptions = whatlanggo.Options{
-		Whitelist: map[whatlanggo.Lang]bool{
-			whatlanggo.Cmn: true,	// 中文
-			whatlanggo.Eng: true,	// 英文
-			whatlanggo.Jpn: true,	// 日文
-			whatlanggo.Kor: true,	// 韩文
-		},
-	}
 	return &Parser{}
 }
 
 // DetermineFileType 确定字幕文件的类型,是双语字幕或者某一种语言等等信息
-func (p Parser) DetermineFileType(filePath string) (common.Language, *sub_parser.SubFileInfo, error) {
+func (p Parser) DetermineFileType(filePath string) (*sub_parser.SubFileInfo, error) {
 	nowExt := filepath.Ext(filePath)
 	if strings.ToLower(nowExt) != common.SubExtASS && strings.ToLower(nowExt) != common.SubExtSSA {
-		return common.Unknow, nil ,nil
+		return nil ,nil
 	}
 	fBytes, err := ioutil.ReadFile(filePath)
 	if err != nil {
-		return common.Unknow, nil ,err
+		return nil ,err
 	}
 	re := regexp.MustCompile(regString)
 	// 找到 start end text
 	matched := re.FindAllStringSubmatch(string(fBytes), -1)
 	if len(matched) < 1 {
-		return common.Unknow, nil ,nil
+		return nil ,nil
 	}
 	subFileInfo := sub_parser.SubFileInfo{}
 	subFileInfo.Ext = nowExt
@@ -91,22 +80,18 @@ func (p Parser) DetermineFileType(filePath string) (common.Language, *sub_parser
 	if perLines > 0.8 {
 		isDouble = true
 	}
-	println(isDouble)
-	// 需要判断每一个 Line 是啥语言
+	// 需要判断每一个 Line 是啥语言,[语言的code]次数
+	var langDict map[int]int
+	langDict = make(map[int]int)
 	for _, dialogue := range subFileInfo.Dialogues {
-		for i, line := range dialogue.Lines {
-			println(line)
-			info := whatlanggo.DetectWithOptions(line, p.langOptions)
-			// 补是语言是 info.Lang -1
-			println(i, "Language:", info.Lang, info.Lang.String())
-		}
+		common.DetectSubLangAndStatistics(dialogue.Lines, langDict)
 	}
-
-
-	return common.Unknow, &subFileInfo ,nil
+	// 从统计出来的字典,找出 Top 1 或者 2 的出来,然后计算出是什么语言的字幕
+	detectLang := common.SubLangStatistics2SubLangType(isDouble, langDict)
+	subFileInfo.Lang = detectLang
+	return &subFileInfo, nil
 }
 
-
 const (
 	// 字幕文件对话的每一行
 	regString = `Dialogue: [^,.]*[0-9]*,([1-9]?[0-9]*:[0-9]*:[0-9]*.[0-9]*),([1-9]?[0-9]*:[0-9]*:[0-9]*.[0-9]*),[^,.]*,[^,.]*,[0-9]*,[0-9]*,[0-9]*,[^,.]*,(.*)`

+ 2 - 1
sub_parser/ass/ass_test.go

@@ -8,8 +8,9 @@ func TestParser_DetermineFileType(t *testing.T) {
 
 	filePath := "C:\\Tmp\\saw9.ass"
 	parser := NewParser()
-	_, _, err := parser.DetermineFileType(filePath)
+	sfi, err := parser.DetermineFileType(filePath)
 	if err != nil {
 		t.Fatal(err)
 	}
+	println(sfi.Name, sfi.Lang.String(), sfi.Ext)
 }

+ 4 - 1
sub_parser/subFileInfo.go

@@ -1,8 +1,11 @@
 package sub_parser
 
+import "github.com/allanpk716/ChineseSubFinder/common"
+
 type SubFileInfo struct {
-	Name	string			// 字幕的名称
+	Name	string			// 字幕的名称,注意,这里需要额外的赋值,不会自动检测
 	Ext		string			// 字幕的后缀名
+	Lang common.Language	// 识别出来的语言
 	Dialogues []OneDialogue	// 整个字幕文件的所有对话
 }