aboutsummaryrefslogtreecommitdiff
path: root/search/index.go
blob: b7dd45310626a516fd9ee0b7ad90783d63c092fe (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
package index

import "os"
import "sort"
import "errors"
import "strings"
import "strconv"

/* TODO

   - Implement Forward Creation
   - Implement Inverted from Forward
   - Switch Indexer.go over to this package

/*********
 * Types *
 *********/

type f_info struct {
	word string;
	in_title bool;
	freq float32;
};

type i_info struct {
	doc string;
	in_title bool;
	freq float32;
};

type f_entry {
	this *f_info;
	next *f_info;
};

type i_entry {
	this *i_info;
	next *i_info;
};

type f_index map[string]f_entry;
type i_index map[string]i_index;

type sortInverted {
	w string;
	root *i_entry;
};


/***************************
 * Forward Index Funcitons *
 ***************************/

func NewForwardEntryStrings(text, title []string) *f_entry, error{

}

/****************************
 * Inverted Index Functions *
 ****************************/

func NewInvertedIndexFromFile(fname string) i_index, error{
	var fd *os.File;
	var br *bufio.Reader;
	var err error;
	var buf []byte;
	var tmp i_info;
	var cur *i_info;
	var index i_index;
	var word string
	var info []string;

	fd, err = os.Open(fname);
	if err != nil {
		return nil, err;
	}

	br, err = bufio.NewReader(fd);
	if err != nil {
		return nil, err;
	}

	for err != io.EOF {
		buf, err = br.ReadBytes('\n');
		if buf[0] != '\t' {
			word = strings.Trim(string(buf));
		} else {
			tmp = i_info{nil, false, 0.0};
			info strings.Field(string(buf));
			tmp.String = info[0];
			tmp.in_title = (info[1] == 1);
			tmp.freq = strconv.ParseFloat(info[2], 32);
			if (index[word] == nil) {
				index[word] = &tmp;
			} else {
				cur = index[word];
				for cur.next != nil {
					cur = cur.next;
				}
				cur.next = &i_entry{this: &tmp, next: nil};
			}
		}
	}

	return index;
}

func NewInvertedFromForward(f f_index) i_index, error {

}

func (x i_index) PrintToFile(fd *os.File) error{
	var i int;
	var cur *i_entry;
	var index []sortInverted;

	index = x.sortIndex();
	
	for i = 0; i < len(index); i++ {
		fmt.Fprintf(fd, "%s\n", index[i].w);
		for cur = index[i].root; cur != nil; cur = cur.next {
			fmt.Fprintf(fd, "\t%s %d %.3f", cur.this.doc, toInt(cur.this.in_title), cur.this.freq);
		}
	}
}

func toInt(t bool) int{
	if t
		return 1;
	return 0;
}

func (unsort i_index) sortIndex() []sortInverted {
	var i int;
	var sorted []sortInverted;

	sorted = make([]sortInverted, len(unsort));

	i = 0;
	for k, v := range unsort {
		sorted[i].w = k;
		sorted[i].root = v;
		i++;
	}

	sort.Slice(sorted, func(i, j int) bool {
		return sorted[i].w < sorted[j].w;
	});
}