example_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package pinyin_test
  2. import (
  3. "fmt"
  4. "github.com/mozillazg/go-pinyin"
  5. )
  6. func ExampleConvert() {
  7. hans := "中国人"
  8. fmt.Println("default:", pinyin.Convert(hans, nil))
  9. // Output: default: [[zhong] [guo] [ren]]
  10. }
  11. func ExamplePinyin_default() {
  12. hans := "中国人"
  13. a := pinyin.NewArgs()
  14. fmt.Println("default:", pinyin.Pinyin(hans, a))
  15. // Output: default: [[zhong] [guo] [ren]]
  16. }
  17. func ExamplePinyin_normal() {
  18. hans := "中国人"
  19. a := pinyin.NewArgs()
  20. a.Style = pinyin.Normal
  21. fmt.Println("Normal:", pinyin.Pinyin(hans, a))
  22. // Output: Normal: [[zhong] [guo] [ren]]
  23. }
  24. func ExamplePinyin_tone() {
  25. hans := "中国人"
  26. a := pinyin.NewArgs()
  27. a.Style = pinyin.Tone
  28. fmt.Println("Tone:", pinyin.Pinyin(hans, a))
  29. // Output: Tone: [[zhōng] [guó] [rén]]
  30. }
  31. func ExamplePinyin_tone2() {
  32. hans := "中国人"
  33. a := pinyin.NewArgs()
  34. a.Style = pinyin.Tone2
  35. fmt.Println("Tone2:", pinyin.Pinyin(hans, a))
  36. // Output: Tone2: [[zho1ng] [guo2] [re2n]]
  37. }
  38. func ExamplePinyin_initials() {
  39. hans := "中国人"
  40. a := pinyin.NewArgs()
  41. a.Style = pinyin.Initials
  42. fmt.Println("Initials:", pinyin.Pinyin(hans, a))
  43. // Output: Initials: [[zh] [g] [r]]
  44. }
  45. func ExamplePinyin_firstLetter() {
  46. hans := "中国人"
  47. a := pinyin.NewArgs()
  48. a.Style = pinyin.FirstLetter
  49. fmt.Println(pinyin.Pinyin(hans, a))
  50. // Output: [[z] [g] [r]]
  51. }
  52. func ExamplePinyin_finals() {
  53. hans := "中国人"
  54. a := pinyin.NewArgs()
  55. a.Style = pinyin.Finals
  56. fmt.Println(pinyin.Pinyin(hans, a))
  57. // Output: [[ong] [uo] [en]]
  58. }
  59. func ExamplePinyin_finalsTone() {
  60. hans := "中国人"
  61. a := pinyin.NewArgs()
  62. a.Style = pinyin.FinalsTone
  63. fmt.Println(pinyin.Pinyin(hans, a))
  64. // Output: [[ōng] [uó] [én]]
  65. }
  66. func ExamplePinyin_finalsTone2() {
  67. hans := "中国人"
  68. a := pinyin.NewArgs()
  69. a.Style = pinyin.FinalsTone2
  70. fmt.Println(pinyin.Pinyin(hans, a))
  71. // Output: [[o1ng] [uo2] [e2n]]
  72. }
  73. func ExamplePinyin_heteronym() {
  74. hans := "中国人"
  75. a := pinyin.NewArgs()
  76. a.Heteronym = true
  77. a.Style = pinyin.Tone2
  78. fmt.Println(pinyin.Pinyin(hans, a))
  79. // Output: [[zho1ng zho4ng] [guo2] [re2n]]
  80. }
  81. func ExamplePinyin_fallbackCustom1() {
  82. hans := "中国人abc"
  83. a := pinyin.NewArgs()
  84. a.Fallback = func(r rune, a pinyin.Args) []string {
  85. return []string{string(r)}
  86. }
  87. fmt.Println(pinyin.Pinyin(hans, a))
  88. // Output: [[zhong] [guo] [ren] [a] [b] [c]]
  89. }
  90. func ExamplePinyin_fallbackCustom2() {
  91. hans := "中国人アイウ"
  92. a := pinyin.NewArgs()
  93. a.Fallback = func(r rune, a pinyin.Args) []string {
  94. data := map[rune][]string{
  95. 'ア': {"a"},
  96. 'イ': {"i"},
  97. 'ウ': {"u"},
  98. }
  99. s, ok := data[r]
  100. if ok {
  101. return s
  102. } else {
  103. return []string{}
  104. }
  105. }
  106. fmt.Println(pinyin.Pinyin(hans, a))
  107. // Output: [[zhong] [guo] [ren] [a] [i] [u]]
  108. }
  109. func ExampleLazyPinyin() {
  110. hans := "中国人"
  111. a := pinyin.NewArgs()
  112. fmt.Println(pinyin.LazyPinyin(hans, a))
  113. // Output: [zhong guo ren]
  114. }
  115. func ExampleSlug() {
  116. hans := "中国人"
  117. a := pinyin.NewArgs()
  118. fmt.Println(pinyin.Slug(hans, a))
  119. // Output: zhong-guo-ren
  120. }