github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/cmd/test/others/unit/pinyin_test.go (about) 1 package main 2 3 import ( 4 "strconv" 5 "strings" 6 "testing" 7 "unicode" 8 9 "github.com/360EntSecGroup-Skylar/excelize/v2" 10 "github.com/Chain-Zhang/pinyin" 11 i118Utils "github.com/easysoft/zendata/pkg/utils/i118" 12 logUtils "github.com/easysoft/zendata/pkg/utils/log" 13 ) 14 15 func TestPinYin(t *testing.T) { 16 //path := "../../data/name/cn.family.v1.xlsx" 17 //path := "../../data/name/cn.given.v1.xlsx" 18 path := "../../data/words/v1.xlsx" 19 excel, err := excelize.OpenFile(path) 20 if err != nil { 21 logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_read_file", path)) 22 return 23 } 24 25 for _, sheet := range excel.GetSheetList() { 26 rows, _ := excel.GetRows(sheet) 27 28 for index, row := range rows { 29 if index == 0 { 30 continue 31 } 32 33 if row[0] == "" { // stop when finding a blank in first column 34 break 35 } 36 37 name := strings.TrimSpace(row[2]) 38 pinyin, err := pinyin.New(name).Split(" ").Mode(pinyin.WithoutTone).Convert() 39 if err == nil { 40 t.Log(pinyin) 41 } else { 42 t.Error("fail to convert " + name) 43 } 44 45 pinyin = strings.Replace(pinyin, " ", "", -1) 46 excel.SetCellValue(sheet, "D"+strconv.Itoa(index+1), pinyin) 47 48 //doub := "false" 49 //lent := ChineseCount(name) 50 //if lent > 1 { 51 // doub = "true" 52 //} 53 //excel.SetCellValue(sheet, "E" + strconv.Itoa(index + 1), doub) 54 55 } 56 } 57 58 if err := excel.SaveAs(path); err != nil { 59 logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_write_file", path)) 60 } 61 } 62 63 func ChineseCount(str1 string) (count int) { 64 for _, char := range str1 { 65 if unicode.Is(unicode.Han, char) { 66 count++ 67 } 68 } 69 70 return 71 }