Code Monkey home page Code Monkey logo

godnf's Introduction

Build Status codecov Go Doc Go Report

Godnf:

Implementing boolean expression indexing algorithm (which is very useful in computational advertising area) by golang

For details, see this paper: Indexing Boolean Expressions

Installation:

go get -u github.com/brg-liuwei/godnf

Testing:

Type make test to run example

make test

Type make bench to run benchmark

make bench

DNF (Disjunctive Normal Form) syntax:

  • DNF

    ( CONJUNCTION ) [ or ( CONJUNCTION ) ... ]

  • CONJUNCTION

    KEY [not] in SET [ and KEY [not] in SET ... ]

  • SET

    { VAL [, VAL, VAL] }

For example, all strings below are DNFs:

(region in {SH, BJ})
(region in {SH, BJ} and age not in {3})
(region in {SH, BJ} and age not in {3} and gender in {male})
(region in {SH, BJ} and age not in {3, 4}) or (gender in {male})
(region in {SH, BJ} and age not in {3, 4}) or (gender in {male} and age in {2})

Example:

package main

import (
	"fmt"
	dnf "github.com/brg-liuwei/godnf"
)

type attr struct {
	h        int
	w        int
	duration int
}

func (this attr) ToString() string {
	return fmt.Sprintf("{height: %d, width: %d, duration: %d}", this.h, this.w, this.duration)
}

func (this attr) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"height":   this.h,
		"width":    this.w,
		"duration": this.duration,
	}
}

func main() {
	dnf.SetDebug(true)
	dnf.SetHandler(dnf.NewHandler())
	h := dnf.GetHandler()
	var err error
	err = h.AddDoc("ad0", "0", "(region in {ShangHai, Beijing} and age not in {3, 4})", attr{300, 250, 20})
	if err != nil {
		panic(err)
	}
	err = h.AddDoc("ad1", "1", "(region in {ShenZhen, ShangHai}) or (age not in {4, 6})", attr{300, 250, 15})
	if err != nil {
		panic(err)
	}
	err = h.AddDoc("ad2", "2", "(region in {ShangHai, NanJing} and age not in {3, 5, 6})", attr{300, 250, 10})
	if err != nil {
		panic(err)
	}
	err = h.AddDoc("ad3", "3", "(region in {ChengDu, Beijing, WuHan}) or (age not in {4, 3})", attr{300, 250, 30})
	if err != nil {
		panic(err)
	}
	err = h.AddDoc("ad4", "4", "(age not in {3, 4})", attr{300, 250, 35})
	if err != nil {
		panic(err)
	}

	conds := []dnf.Cond{
		{"region", "NanJing"},
		{"age", "5"},
	}
	var docs []int
	docs, err = h.Search(conds, func(a dnf.DocAttr) bool { return a.(attr).duration <= 30 })
	if err != nil {
		panic(err)
	}
	fmt.Println("docs:", docs)
	for _, doc := range docs {
		fmt.Println(h.DocId2Map(doc))
	}
	h.DisplayDocs()

	fmt.Println()
	h.DisplayConjRevs()
	fmt.Println()
	h.DisplayConjRevs2()
}

godnf's People

Contributors

brg-liuwei avatar gxcbuf avatar dongjiahong avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.