util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package main
  2. import (
  3. "fmt"
  4. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  5. "regexp"
  6. "unicode"
  7. "unicode/utf8"
  8. )
  9. // GetJyURLByID 获取剑鱼地址
  10. func GetJyURLByID(id string) string {
  11. var Url = "https://www.jianyu360.com/article/content/%s.html"
  12. url := fmt.Sprintf(Url, util.CommonEncodeArticle("content", id))
  13. return url
  14. }
  15. // IsHanStart 判断字符串是否以汉字开头或者以字母开头
  16. func IsHanStart(s string) bool {
  17. if s == "" {
  18. return false
  19. }
  20. return unicode.Is(unicode.Scripts["Han"], []rune(s)[0]) || unicode.IsLetter([]rune(s)[0])
  21. }
  22. // IsHan 判断字符是否为汉字
  23. func IsHan(c rune) bool {
  24. return unicode.Is(unicode.Scripts["Han"], c)
  25. }
  26. // IsBracketStartWithHan 判断公司名称是否以圆括号开头且括号内汉字开头
  27. func IsBracketStartWithHan(s string) bool {
  28. if len(s) == 0 || s[0] != '(' {
  29. return false
  30. }
  31. // 索引 i 和 j 分别是左右圆括号的位置,如果找不到右圆括号则返回 false
  32. i, j := 0, 0
  33. for j = i + 1; j < len(s); j++ {
  34. if s[j] == ')' {
  35. break
  36. }
  37. }
  38. if j >= len(s) {
  39. return false
  40. }
  41. // 检查圆括号内是否以汉字或字母开头
  42. bracketContent := s[i+1 : j]
  43. if len(bracketContent) == 0 || (!unicode.IsLetter(rune(bracketContent[0])) && !IsHan([]rune(bracketContent)[0])) {
  44. return false
  45. }
  46. return true
  47. }
  48. // IsCompanyName 判断字符串是否以汉字开头、以括号开头并且括号里面是汉字、以"公司"结尾,其中一个条件符合即返回true,否则返回false
  49. func IsCompanyName(s string) bool {
  50. r := []rune(s)
  51. //if len(r) >= 6 && (string(r[len(r)-6:]) == "有限公司" || string(r[len(r)-6:]) == "股份有限公司") {
  52. // return (IsHanStart(s) || IsBracketStartWithHan(s))
  53. //} else if len(r) >= 2 && string(r[len(r)-2:]) == "公司" {
  54. // return (IsHanStart(s) || IsBracketStartWithHan(s))
  55. //}
  56. if len(r) >= 2 {
  57. return (IsHanStart(s) || IsBracketStartWithHan(s))
  58. }
  59. return false
  60. }
  61. // GetChineseCharacters 提取字符串中的汉字
  62. func GetChineseCharacters(s string) string {
  63. re := regexp.MustCompile(`[\p{Han}]+`)
  64. return re.FindString(s)
  65. }
  66. func getCompanyName(name string) string {
  67. if IsCompanyName(name) {
  68. return name
  69. }
  70. return GetChineseCharacters(name)
  71. }
  72. func IsUnicodeStart(s string) bool {
  73. if len(s) == 0 {
  74. return false
  75. }
  76. _, size := utf8.DecodeRuneInString(s)
  77. return size > 0
  78. }
  79. // RemoveDuplicateSuffix 去除字符串末尾的重复字词
  80. func RemoveDuplicateSuffix(str string, suffix string) string {
  81. // 构建正则表达式:^(.*?)(重复的结尾词)+$
  82. re := regexp.MustCompile(fmt.Sprintf(`^(.*?)(%s)+$`, suffix))
  83. matches := re.FindStringSubmatch(str)
  84. if len(matches) == 3 {
  85. return matches[1] + matches[2]
  86. }
  87. return str
  88. }
  89. //func findName(name string) []map[string]interface{} {
  90. // filter := bson.M{"name": name, "status": 1}
  91. // info, _ := Mgo.Find(wccBuyer, filter, nil, nil, false, -1, -1)
  92. //
  93. // return *info
  94. //}
  95. //
  96. //func findNameID(id string) []map[string]interface{} {
  97. // filter := bson.M{"name_id": id, "status": 1}
  98. // info, _ := Mgo.Find(wccBuyer, filter, nil, nil, false, -1, -1)
  99. //
  100. // return *info
  101. //}
  102. // isStringRepeating 判断字符串内字符完全重复,例如:山东大学山东大学
  103. func isStringRepeating(str string) bool {
  104. for i := 0; i < len(str); i++ {
  105. for j := i + 1; j < len(str); j++ {
  106. if str[i] != str[j] {
  107. return false
  108. }
  109. }
  110. }
  111. return true
  112. }