|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/gob" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "reflect" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/go-ego/riot" |
| 15 | + "github.com/go-ego/riot/types" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // SecondsInADay seconds in a day |
| 20 | + SecondsInADay = 86400 |
| 21 | + // MaxTokenProximity max token proximity |
| 22 | + MaxTokenProximity = 150 |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + weiboData = flag.String( |
| 27 | + "weibo_data", |
| 28 | + "../../testdata/weibo_data.txt", |
| 29 | + "索引的微博帖子,每行当作一个文档") |
| 30 | + query = flag.String( |
| 31 | + "query", |
| 32 | + "chinajoy 游戏", |
| 33 | + "待搜索的短语") |
| 34 | + dictionaries = flag.String( |
| 35 | + "dictionaries", |
| 36 | + "../data/dict/dictionary.txt", |
| 37 | + "分词字典文件") |
| 38 | + stopTokenFile = flag.String( |
| 39 | + "stop_token_file", |
| 40 | + "../data/dict/stop_tokens.txt", |
| 41 | + "停用词文件") |
| 42 | + |
| 43 | + searcher = riot.Engine{} |
| 44 | + options = types.RankOpts{ |
| 45 | + ScoringCriteria: WeiboScoringCriteria{}, |
| 46 | + OutputOffset: 0, |
| 47 | + MaxOutputs: 100, |
| 48 | + } |
| 49 | + searchQueries = []string{} |
| 50 | +) |
| 51 | + |
| 52 | +// WeiboScoringFields 微博评分字段 |
| 53 | +type WeiboScoringFields struct { |
| 54 | + // 帖子的时间戳 |
| 55 | + Timestamp uint32 |
| 56 | + |
| 57 | + // 帖子的转发数 |
| 58 | + RepostsCount uint32 |
| 59 | + |
| 60 | + // 帖子的长度 |
| 61 | + TextLength int |
| 62 | +} |
| 63 | + |
| 64 | +// WeiboScoringCriteria 自定义的微博评分规则 |
| 65 | +type WeiboScoringCriteria struct { |
| 66 | +} |
| 67 | + |
| 68 | +// Score score and sort |
| 69 | +func (criteria WeiboScoringCriteria) Score( |
| 70 | + doc types.IndexedDoc, fields interface{}) []float32 { |
| 71 | + if doc.TokenProximity > MaxTokenProximity { // 评分第一步 |
| 72 | + return []float32{} |
| 73 | + } |
| 74 | + if reflect.TypeOf(fields) != reflect.TypeOf(WeiboScoringFields{}) { |
| 75 | + return []float32{} |
| 76 | + } |
| 77 | + output := make([]float32, 4) |
| 78 | + wsf := fields.(WeiboScoringFields) |
| 79 | + output[0] = float32(wsf.Timestamp / SecondsInADay) // 评分第二步 |
| 80 | + output[1] = float32(int(doc.BM25)) // 评分第三步 |
| 81 | + output[2] = float32(wsf.RepostsCount) // 评分第四步 |
| 82 | + output[3] = float32(wsf.TextLength) // 评分第五步 |
| 83 | + return output |
| 84 | +} |
| 85 | + |
| 86 | +func main() { |
| 87 | + // 解析命令行参数 |
| 88 | + flag.Parse() |
| 89 | + log.Printf("待搜索的短语为\"%s\"", *query) |
| 90 | + |
| 91 | + // 初始化 |
| 92 | + gob.Register(WeiboScoringFields{}) |
| 93 | + searcher.Init(types.EngineOpts{ |
| 94 | + Using: 1, |
| 95 | + SegmenterDict: *dictionaries, |
| 96 | + StopTokenFile: *stopTokenFile, |
| 97 | + IndexerOpts: &types.IndexerOpts{ |
| 98 | + IndexType: types.LocsIndex, |
| 99 | + }, |
| 100 | + DefaultRankOpts: &options, |
| 101 | + }) |
| 102 | + defer searcher.Close() |
| 103 | + |
| 104 | + // 读入微博数据 |
| 105 | + file, err := os.Open(*weiboData) |
| 106 | + if err != nil { |
| 107 | + log.Fatal(err) |
| 108 | + } |
| 109 | + defer file.Close() |
| 110 | + log.Printf("读入文本 %s", *weiboData) |
| 111 | + scanner := bufio.NewScanner(file) |
| 112 | + lines := []string{} |
| 113 | + fieldsSlice := []WeiboScoringFields{} |
| 114 | + for scanner.Scan() { |
| 115 | + data := strings.Split(scanner.Text(), "||||") |
| 116 | + if len(data) != 10 { |
| 117 | + continue |
| 118 | + } |
| 119 | + timestamp, _ := strconv.ParseUint(data[1], 10, 32) |
| 120 | + repostsCount, _ := strconv.ParseUint(data[4], 10, 32) |
| 121 | + text := data[9] |
| 122 | + if text != "" { |
| 123 | + lines = append(lines, text) |
| 124 | + fields := WeiboScoringFields{ |
| 125 | + Timestamp: uint32(timestamp), |
| 126 | + RepostsCount: uint32(repostsCount), |
| 127 | + TextLength: len(text), |
| 128 | + } |
| 129 | + fieldsSlice = append(fieldsSlice, fields) |
| 130 | + } |
| 131 | + } |
| 132 | + log.Printf("读入%d条微博\n", len(lines)) |
| 133 | + |
| 134 | + // 建立索引 |
| 135 | + log.Print("建立索引") |
| 136 | + for i, text := range lines { |
| 137 | + searcher.IndexDoc( |
| 138 | + uint64(i), |
| 139 | + types.DocIndexData{Content: text, Fields: fieldsSlice[i]}) |
| 140 | + } |
| 141 | + searcher.FlushIndex() |
| 142 | + log.Print("索引建立完毕") |
| 143 | + |
| 144 | + // 搜索 |
| 145 | + log.Printf("开始查询") |
| 146 | + output := searcher.Search(types.SearchReq{Text: *query}) |
| 147 | + |
| 148 | + // 显示 |
| 149 | + fmt.Println("output...") |
| 150 | + fmt.Println(output) |
| 151 | + log.Printf("查询完毕") |
| 152 | +} |
0 commit comments