aboutsummaryrefslogtreecommitdiff
path: root/search/search.go
blob: 9c9bb38ca8261972428fa51fc5996bc7587a9a85 (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
/************************************************
 *                 README                       *
 * In order for search/index to be accessible   *
 * you must link this folder (search) into your *
 * GOPATH                                       *
 ************************************************/


package main

import "search/index"
import "os"
import "fmt"
import "sort"

type res struct {
	doc string;
	score float64;
};

func main() {
	var init_index, sIndex index.I_index;
	var tmp, results, root *index.I_entry;
	var tmp_score float64;
	var scores map[string]map[string]float64; // scores[doc][word] == score
	var i,j int;
	var searchBool, perWord, docAdded map[string]bool; //map[doc]bool
	var resultSort []res;
	var err error;

	scores = make(map[string]map[string]float64);
	searchBool = make(map[string]bool);
	perWord = make(map[string]bool);
	docAdded = make(map[string]bool);


	sIndex = make(index.I_index);



	init_index, err = index.NewInvertedIndexFromFile("index.dat"); //TODO add flag for filename
	if err != nil {
		panic(err)
	}

	for i = 1; i < len(os.Args); i++ {
		sIndex[os.Args[i]] = init_index[os.Args[i]]
	}

	for _, v := range sIndex {
		for tmp = v; tmp != nil; tmp = tmp.Next {
			perWord[tmp.This.Doc] = true;
			searchBool[tmp.This.Doc] = true;
			scores[tmp.This.Doc] = make(map[string]float64);
		}
	}

	for _, v := range sIndex {
		for tmp = v; tmp != nil; tmp = tmp.Next {
			perWord[tmp.This.Doc] = true;
		}

		for d := range searchBool {
			if _, o := perWord[d]; !o {
				searchBool[d] = false;
			}
		}
		perWord = make(map[string]bool);
	}

	for k, v := range sIndex {
		for tmp = v; tmp != nil; tmp = tmp.Next {
			if searchBool[tmp.This.Doc] {
				if tmp.This.In_title {
					tmp_score = 1.0;
				} else {
					tmp_score = 0.0;
				}

				scores[tmp.This.Doc][k] = (0.9 * tmp.This.Freq) + (0.1 * tmp_score);
			}
		}

	}

	i = 0;
	results = &index.I_entry{nil, nil}
	root = &index.I_entry{nil, nil};
	results.Next = root;

	j = 0;

	for _ ,v := range sIndex {
		for tmp = v; tmp != nil; tmp = tmp.Next {
			if (searchBool[tmp.This.Doc]) {
				root.This = tmp.This;
				docAdded[root.This.Doc] = false;
				root.Next = &index.I_entry{nil, nil};
				root = root.Next;
			j++
			}
		}
	}

	resultSort = make([]res, j);

	i = 0;
	for root = results.Next; root.Next != nil; root = root.Next {
		if (!docAdded[root.This.Doc]) {
			j = 0;
			tmp_score = 0;
			for _ ,v := range scores[root.This.Doc] {
				tmp_score += v;
				j++;
			}
			tmp_score /= float64(j);
			resultSort[i] = res{root.This.Doc, tmp_score};
			docAdded[root.This.Doc] = true;
			i++;
		}
	}
	resultSort = resultSort[:i];

		sort.Slice(resultSort, func(i, j int) bool {
			return resultSort[i].score > resultSort[j].score;
		});
	
	fmt.Printf("Results: %d\n", len(resultSort));
	for i = 0; i < len(resultSort); i++ {
		fmt.Printf("\t%d. Doc: %s, Score: %.3f\n", i, resultSort[i].doc, resultSort[i].score);
	}
}