main.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. )
  8. var (
  9. classifier *MultiPackageClassifier
  10. quote_classifier *QuoteClassifier
  11. )
  12. func init() {
  13. // 初始化分类器
  14. classifier = NewClassifier()
  15. quote_classifier = NewQuoteClassifier()
  16. }
  17. func main() {
  18. //packAgeDemo()
  19. //quoteDemo()
  20. // 注册路由
  21. http.HandleFunc("/classify", classifyHandler)
  22. http.HandleFunc("/quote_classify", quoteClassifyHandler)
  23. // 启动HTTP服务
  24. log.Println("Starting server on :8182")
  25. log.Fatal(http.ListenAndServe(":8182", nil))
  26. }
  27. // 报价模式测试
  28. func quoteDemo() {
  29. listContent := []string{
  30. "下浮 20.22%",
  31. "上浮动:百分之三十",
  32. }
  33. for k, content := range listContent {
  34. // 执行分类判断
  35. doc := BidDocument{
  36. Content: content,
  37. }
  38. // 执行分类判断
  39. modenum, _ := quote_classifier.QuoteMode(doc)
  40. log.Println(modenum, k)
  41. }
  42. }
  43. // 分包识别测试
  44. func packAgeDemo() {
  45. content := `预中标单位:宁波公路市政设计有限公司和浙江土力勘测设计院有限公司联合体`
  46. // 执行分类判断
  47. doc := BidDocument{
  48. Content: content,
  49. }
  50. //分包识别
  51. packageType, _ := classifier.IsMultiPackage(doc)
  52. //联合投标识别
  53. isConsortium := isConsortiumKeysReg(doc.Content)
  54. log.Println(packageType, isConsortium)
  55. }
  56. // 报价分类请求处理函数
  57. func quoteClassifyHandler(w http.ResponseWriter, r *http.Request) {
  58. // 只允许POST请求
  59. if r.Method != http.MethodPost {
  60. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  61. return
  62. }
  63. // 解析请求体
  64. var requestData map[string]interface{}
  65. err := json.NewDecoder(r.Body).Decode(&requestData)
  66. if err != nil {
  67. http.Error(w, "Invalid request payload", http.StatusBadRequest)
  68. return
  69. }
  70. defer r.Body.Close()
  71. // 执行分类判断
  72. modenum, _ := quote_classifier.QuoteMode(getDoc(requestData))
  73. // 构建响应
  74. response := map[string]interface{}{
  75. "result": modenum,
  76. "success": true,
  77. }
  78. // 设置响应头
  79. w.Header().Set("Content-Type", "application/json")
  80. // 返回JSON响应
  81. if err := json.NewEncoder(w).Encode(response); err != nil {
  82. log.Printf("Error encoding response: %v", err)
  83. http.Error(w, "Internal server error", http.StatusInternalServerError)
  84. return
  85. }
  86. }
  87. // 分包分类请求处理函数
  88. func classifyHandler(w http.ResponseWriter, r *http.Request) {
  89. // 只允许POST请求
  90. if r.Method != http.MethodPost {
  91. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  92. return
  93. }
  94. // 解析请求体
  95. var requestData map[string]interface{}
  96. err := json.NewDecoder(r.Body).Decode(&requestData)
  97. if err != nil {
  98. http.Error(w, "Invalid request payload", http.StatusBadRequest)
  99. return
  100. }
  101. defer r.Body.Close()
  102. // 执行分类判断
  103. doc := getDoc(requestData)
  104. packageType, _ := classifier.IsMultiPackage(doc) //分包识别
  105. isConsortium := isConsortiumKeysReg(doc.Content) //联合投标识别
  106. // 构建响应
  107. response := map[string]interface{}{
  108. "packageType": packageType,
  109. "description": getPackageTypeDescription(packageType),
  110. "isJointVenture": isConsortium,
  111. "success": true,
  112. }
  113. // 设置响应头
  114. w.Header().Set("Content-Type", "application/json")
  115. // 返回JSON响应
  116. if err := json.NewEncoder(w).Encode(response); err != nil {
  117. log.Printf("Error encoding response: %v", err)
  118. http.Error(w, "Internal server error", http.StatusInternalServerError)
  119. return
  120. }
  121. }
  122. // 分类逻辑
  123. func classifyBid(data map[string]interface{}) (int, bool) {
  124. content := fmt.Sprint(data["title"]) + "\n" + fmt.Sprint(data["detail"])
  125. // 文本清理
  126. content = cleanWebText(content, clearKeys, clearKeysBack)
  127. content_rmtable := removeTables(content)
  128. // 执行分类判断
  129. doc := BidDocument{
  130. Content: content,
  131. Content_NoTable: content_rmtable,
  132. Budget: content,
  133. AwardNotice: content,
  134. BidderOptions: content,
  135. }
  136. ispack, _ := classifier.IsMultiPackage(doc)
  137. // log.Printf("Classified as: %d", ispack)
  138. iscon := isConsortiumKeysReg(content)
  139. return ispack, iscon
  140. }
  141. // 分类逻辑
  142. func getDoc(data map[string]interface{}) BidDocument {
  143. content := fmt.Sprint(data["title"]) + "\n" + fmt.Sprint(data["detail"])
  144. // 文本清理
  145. content = cleanWebText(content, clearKeys, clearKeysBack)
  146. content_rmtable := removeTables(content)
  147. // 执行分类判断
  148. doc := BidDocument{
  149. Content: content,
  150. Content_NoTable: content_rmtable,
  151. Budget: content,
  152. AwardNotice: content,
  153. BidderOptions: content,
  154. }
  155. return doc
  156. }
  157. // 获取分类类型描述
  158. func getPackageTypeDescription(packageType int) string {
  159. switch packageType {
  160. case 1:
  161. return "多包"
  162. case -1:
  163. return "单包"
  164. case 0:
  165. return "不确定"
  166. default:
  167. return "不确定"
  168. }
  169. }