123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- package main
- import (
- "encoding/json"
- "fmt"
- "log"
- "net/http"
- )
- var (
- classifier *MultiPackageClassifier
- quote_classifier *QuoteClassifier
- )
- func init() {
- // 初始化分类器
- classifier = NewClassifier()
- quote_classifier = NewQuoteClassifier()
- }
- func main() {
- //packAgeDemo()
- //quoteDemo()
- // 注册路由
- http.HandleFunc("/classify", classifyHandler)
- http.HandleFunc("/quote_classify", quoteClassifyHandler)
- // 启动HTTP服务
- log.Println("Starting server on :8182")
- log.Fatal(http.ListenAndServe(":8182", nil))
- }
- // 报价模式测试
- func quoteDemo() {
- listContent := []string{
- "下浮 20.22%",
- "上浮动:百分之三十",
- }
- for k, content := range listContent {
- // 执行分类判断
- doc := BidDocument{
- Content: content,
- }
- // 执行分类判断
- modenum, _ := quote_classifier.QuoteMode(doc)
- log.Println(modenum, k)
- }
- }
- // 分包识别测试
- func packAgeDemo() {
- content := `预中标单位:宁波公路市政设计有限公司和浙江土力勘测设计院有限公司联合体`
- // 执行分类判断
- doc := BidDocument{
- Content: content,
- }
- //分包识别
- packageType, _ := classifier.IsMultiPackage(doc)
- //联合投标识别
- isConsortium := isConsortiumKeysReg(doc.Content)
- log.Println(packageType, isConsortium)
- }
- // 报价分类请求处理函数
- func quoteClassifyHandler(w http.ResponseWriter, r *http.Request) {
- // 只允许POST请求
- if r.Method != http.MethodPost {
- http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
- return
- }
- // 解析请求体
- var requestData map[string]interface{}
- err := json.NewDecoder(r.Body).Decode(&requestData)
- if err != nil {
- http.Error(w, "Invalid request payload", http.StatusBadRequest)
- return
- }
- defer r.Body.Close()
- // 执行分类判断
- modenum, _ := quote_classifier.QuoteMode(getDoc(requestData))
- // 构建响应
- response := map[string]interface{}{
- "result": modenum,
- "success": true,
- }
- // 设置响应头
- w.Header().Set("Content-Type", "application/json")
- // 返回JSON响应
- if err := json.NewEncoder(w).Encode(response); err != nil {
- log.Printf("Error encoding response: %v", err)
- http.Error(w, "Internal server error", http.StatusInternalServerError)
- return
- }
- }
- // 分包分类请求处理函数
- func classifyHandler(w http.ResponseWriter, r *http.Request) {
- // 只允许POST请求
- if r.Method != http.MethodPost {
- http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
- return
- }
- // 解析请求体
- var requestData map[string]interface{}
- err := json.NewDecoder(r.Body).Decode(&requestData)
- if err != nil {
- http.Error(w, "Invalid request payload", http.StatusBadRequest)
- return
- }
- defer r.Body.Close()
- // 执行分类判断
- doc := getDoc(requestData)
- packageType, _ := classifier.IsMultiPackage(doc) //分包识别
- isConsortium := isConsortiumKeysReg(doc.Content) //联合投标识别
- // 构建响应
- response := map[string]interface{}{
- "packageType": packageType,
- "description": getPackageTypeDescription(packageType),
- "isJointVenture": isConsortium,
- "success": true,
- }
- // 设置响应头
- w.Header().Set("Content-Type", "application/json")
- // 返回JSON响应
- if err := json.NewEncoder(w).Encode(response); err != nil {
- log.Printf("Error encoding response: %v", err)
- http.Error(w, "Internal server error", http.StatusInternalServerError)
- return
- }
- }
- // 分类逻辑
- func classifyBid(data map[string]interface{}) (int, bool) {
- content := fmt.Sprint(data["title"]) + "\n" + fmt.Sprint(data["detail"])
- // 文本清理
- content = cleanWebText(content, clearKeys, clearKeysBack)
- content_rmtable := removeTables(content)
- // 执行分类判断
- doc := BidDocument{
- Content: content,
- Content_NoTable: content_rmtable,
- Budget: content,
- AwardNotice: content,
- BidderOptions: content,
- }
- ispack, _ := classifier.IsMultiPackage(doc)
- // log.Printf("Classified as: %d", ispack)
- iscon := isConsortiumKeysReg(content)
- return ispack, iscon
- }
- // 分类逻辑
- func getDoc(data map[string]interface{}) BidDocument {
- content := fmt.Sprint(data["title"]) + "\n" + fmt.Sprint(data["detail"])
- // 文本清理
- content = cleanWebText(content, clearKeys, clearKeysBack)
- content_rmtable := removeTables(content)
- // 执行分类判断
- doc := BidDocument{
- Content: content,
- Content_NoTable: content_rmtable,
- Budget: content,
- AwardNotice: content,
- BidderOptions: content,
- }
- return doc
- }
- // 获取分类类型描述
- func getPackageTypeDescription(packageType int) string {
- switch packageType {
- case 1:
- return "多包"
- case -1:
- return "单包"
- case 0:
- return "不确定"
- default:
- return "不确定"
- }
- }
|