summaryrefslogtreecommitdiff
path: root/gwic.go
blob: cb086b2ef0a7f119120f8750d084c25bf1019df3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main

import (
	"bufio"
	"flag"
	"fmt"
	"os"
	"regexp"
	"strings"
	"text/tabwriter"
)

type queue struct {
	q    []string
	maxl int
}

func (q *queue) insert(s string) {
	if len(q.q) <= q.maxl {
		q.q = append(q.q, s)
		return
	}
	q.q = append(q.q[1:len(q.q)], s)
}

type work struct {
	c    chan string
	left int
	done chan struct{}
}

func (w *work) finish() {
	close(w.c)
	w.done <- struct{}{}
	return
}

func (w *work) print_kwic(sl []string, out chan string) {
	for word := range w.c {
		sl = append(sl, word)
	}
	out <- strings.Join(sl, "\t")
	<-w.done
}

func feedworkers(wlist []*work, w string) []*work {
	var ret []*work
	if len(wlist) == 0 {
		return ret
	}
	ret = make([]*work, len(wlist))

	copy(ret, wlist)
	//fmt.Fprintf(os.Stderr, "wlist not empty: %v ret: %v\n", wlist, ret)

	for _, wc := range wlist {
		if wc.left == 0 {
			wc.finish()
			//fmt.Fprintf(os.Stderr, "Closed wc: %s\n", wc)
			// TODO: remove the right one?
			if len(wlist) >= 1 {
				ret = wlist[1:]
			}
		} else {
			wc.c <- w
			wc.left -= 1
			//fmt.Fprintf(os.Stderr, "diminished wc: %v\n", wc)
		}
	}

	return ret
}

func printqueue(q *queue, words chan string, left int, out chan string) {
	var hungryhippos []*work

	for w := range words {
		index := strings.Index(w, kw)
		if index < 0 {
			q.insert(w)
			hungryhippos = feedworkers(hungryhippos, w)
			continue
		}

		q.insert(fmt.Sprintf("%q", w))

		//fmt.Fprintf(os.Stdout, "%q totally matched %q\n", w, kw)
		//fmt.Fprintf(os.Stdout, "queue was %v\n", q.q)

		wc := &work{c: make(chan string, 10), left: 5, done: make(chan struct{})}

		hungryhippos = feedworkers(hungryhippos, w)

		sl := make([]string, MAX)
		//n := copy(sl, q.q[6:])
		copy(sl, q.q[6:])
		//fmt.Fprintf(os.Stdout, "copied %d : %v\n", n, sl)
		hungryhippos = append(hungryhippos, wc)
		//fmt.Fprintf(os.Stderr, "appended hungryhippos: %v %d\n", hungryhippos, len(hungryhippos))
		go wc.print_kwic(sl, out)
	}
	for _, wc := range hungryhippos {
		wc.finish()
	}
	close(out)
}

var (
	tw *tabwriter.Writer
	kw string
)

const MAX int = 11

func main() {
	flag.Parse()
	kw = flag.Arg(0)
	if len(kw) == 0 {
		fmt.Printf("Need string to search for. Exiting...\n")
		os.Exit(1)
	}
	tw = tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
	splitreg := regexp.MustCompile("[ \t\n]")

	q := queue{maxl: MAX, q: make([]string, MAX)}
	sc := make(chan string, 1000)
	out := make(chan string, 1000)

	reader := bufio.NewReader(os.Stdin)
	line, err := reader.ReadString(byte('\n'))
	go func(c chan string) {
		var w string
		for err == nil {
			splline := splitreg.Split(line, -1)
			for _, w = range splline {
				c <- w
			}
			line, err = reader.ReadString(byte('\n'))
		}
		fmt.Printf("Got an error: %q.\n", err)
		c <- w
		close(c)
	}(sc)

	go printqueue(&q, sc, 5, out)

	for l := range out {
		fmt.Fprintf(tw, "%s\n", l)
	}
	tw.Flush()
}