enjoy_wukong.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package main
  2. import (
  3. "log"
  4. "github.com/huichen/wukong/engine"
  5. "github.com/huichen/wukong/types"
  6. )
  7. var (
  8. searcher = engine.Engine{}
  9. )
  10. type Data struct {
  11. Id int
  12. Content string
  13. Labels []string
  14. }
  15. func (d *Data) Print() {
  16. log.Println(d.Id, d.Content, d.Labels)
  17. }
  18. func main() {
  19. datas := make([]Data, 0)
  20. data0 := Data{Id: 0, Content: "此次百度收购将成中国互联网最大并购", Labels: []string{"百度", "中国"}}
  21. datas = append(datas, data0)
  22. data1 := Data{Id: 1, Content: "百度宣布拟全资收购91无线业务", Labels: []string{"百度"}}
  23. datas = append(datas, data1)
  24. data2 := Data{Id: 2, Content: "百度是中国最大的搜索引擎", Labels: []string{"百度"}}
  25. datas = append(datas, data2)
  26. data3 := Data{Id: 3, Content: "百度在研制无人汽车", Labels: []string{"百度"}}
  27. datas = append(datas, data3)
  28. data4 := Data{Id: 4, Content: "BAT是中国互联网三巨头", Labels: []string{"百度"}}
  29. datas = append(datas, data4)
  30. // 初始化
  31. searcher.Init(types.EngineInitOptions{
  32. SegmenterDictionaries: "../data/dictionary.txt",
  33. IndexerInitOptions: &types.IndexerInitOptions{
  34. IndexType: types.LocationsIndex,
  35. //IndexType: types.FrequenciesIndex,
  36. //IndexType: types.DocIdsIndex,
  37. },
  38. })
  39. defer searcher.Close()
  40. // 将文档加入索引
  41. for _, data := range datas {
  42. searcher.IndexDocument(uint64(data.Id), types.DocumentIndexData{Content: data.Content, Labels: data.Labels})
  43. }
  44. // 等待索引刷新完毕
  45. searcher.FlushIndex()
  46. // 搜索输出格式见types.SearchResponse结构体
  47. res := searcher.Search(types.SearchRequest{Text: "百度"})
  48. log.Println("关键字", res.Tokens, "共有", res.NumDocs, "条搜索结果")
  49. for i := range res.Docs {
  50. datas[res.Docs[i].DocId].Print()
  51. }
  52. }