Golang 正則提取相關信息

package main

import (
	"encoding/csv"
	"fmt"
	"github.com/hyq/Tools"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
)

func main() {
	Writecsv(GetKocSimplyInfo("htmls2"),"test.csv")
}
func GetKocSimplyInfo(path string) []kocsimply{
	filelist := Tools.H_GetFileList(path, "html")
	kocinfo := make([]kocsimply, 0, 246)

	var one_koc kocsimply
	for _, fn := range filelist {
		one_koc.filename= strings.Split(filepath.Base(fn),".")[0]
		//fmt.Println( one_koc.filename)
		content, _ := Tools.H_GetFileContents(fn)
		//fmt.Println(err,content)
		koccontents := simple_rexgex(content)
		one_koc.kocinfo = koccontents	//將得到的間個Koc所匹配出的信息添加進來
		//fmt.Println(one_koc.GetMsimNum(0.9))
		fmt.Printf("目標koc名字\t\t%s\n方案總數\t\t%d\n1.0\t\t%d\t\n0.9\t\t%d\t\n0.8\t\t%d\t\n0.7\t\t%d\t\n0.6\t\t%d\t\n0.5\t\t%d\n",
			one_koc.filename,len(one_koc.kocinfo),one_koc.GetMsimNum(1.0),one_koc.GetMsimNum(0.9),one_koc.GetMsimNum(0.8),
			one_koc.GetMsimNum(0.7),one_koc.GetMsimNum(0.6),one_koc.GetMsimNum(0.5))
		kocinfo = append(kocinfo, one_koc)
	}
	return  kocinfo
}

func simple_rexgex(content string) []kocinfo {
	kocsimplyinfo := make([]kocinfo, 0, 512)
	var KocInfo kocinfo
	item_reg := regexp.MustCompile(`<div class="col-md-2 task-item" data-id=".*?class="glyphicon glyphicon-download"`)	//獲取每個方案的單個信息
	mzlink_reg := regexp.MustCompile(`/h4><a href="(.*?\.zip)" class="glyphicon glyphicon-download"`)
	data_taskid_reg := regexp.MustCompile(`data-taskid="(\d+)"\sdata-selected`)
	data_msim := regexp.MustCompile(`data-msim="([01]\.\d+)"`)	//相似度
	data_sver := regexp.MustCompile(`data-sver="(.*?)"`)		//匹配的程序版本號
	item_respons := item_reg.FindAllStringSubmatch(content,-1)

	for _,item := range (item_respons){
		item_respon :=item[0]
		mzlink_response := mzlink_reg.FindAllStringSubmatch(item_respon,-1)
		data_taskid_response := data_taskid_reg.FindAllStringSubmatch(item_respon,-1)
		data_msim_response := data_msim.FindAllStringSubmatch(item_respon,-1)
		if len(mzlink_response)>0 {
			KocInfo.mzlink = mzlink_response[0][1] //mz下載鏈接		}
		}
		KocInfo.id = data_taskid_response[0][1]			//mz文件對就的雲渲染id
		v1, _ := strconv.ParseFloat(data_msim_response[0][1], 64)
		KocInfo.data_msim = v1		//相似度
		data_sver_response := data_sver.FindAllStringSubmatch(item_respon,-1)
		KocInfo.data_sver = data_sver_response[0][1]

		//fmt.Println(KocInfo)
		kocsimplyinfo = append(kocsimplyinfo,KocInfo)	//單個待測試所匹配出的所以方案
	}


	return kocsimplyinfo
}

type kocsimply struct {
	filename string

	kocinfo []kocinfo
}
//返回某個相似度區間的數
func (t *kocsimply) GetMsimNum(f float64) int{
	num := 0
	for _,koc := range (t.kocinfo){
		if koc.data_msim >f && koc.data_msim < f+0.1 {
			num +=1
		}
	}
	return num
}
type kocinfo struct {
	id string
	data_msim float64
	data_sver string
	mzlink string
}

func Writecsv(s []kocsimply,name string)  {
	f,err := os.Create(name)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	f.WriteString("\xEF\xBB\xBF")
	w := csv.NewWriter(f)
	slice :=[][]string{{"name","sum","1.0","0.9","0.8","0.7","0.6","0.5"}}
	for _,koc := range s{
		slice1 := []string{koc.filename,strconv.Itoa(len(koc.kocinfo)),strconv.Itoa(koc.GetMsimNum(1.0)),
			strconv.Itoa(koc.GetMsimNum(0.9)),strconv.Itoa(koc.GetMsimNum(0.8)),strconv.Itoa(koc.GetMsimNum(0.7)),
			strconv.Itoa(koc.GetMsimNum(0.6)),strconv.Itoa(koc.GetMsimNum(0.5)),
			}
		slice = append(slice,slice1)
	}

	w.WriteAll(slice)
	w.Flush()
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章