diff options
Diffstat (limited to 'search')
-rw-r--r-- | search/.gitignore | 1 | ||||
-rw-r--r-- | search/index.go | 72 |
2 files changed, 67 insertions, 6 deletions
diff --git a/search/.gitignore b/search/.gitignore index 81a686a..9f9a462 100644 --- a/search/.gitignore +++ b/search/.gitignore @@ -2,3 +2,4 @@ pages index search +*.swp diff --git a/search/index.go b/search/index.go index 9b1bc0c..a0efb6c 100644 --- a/search/index.go +++ b/search/index.go @@ -17,6 +17,11 @@ type index struct { freq int; } +type wordList struct { + this *index + next *wordList +} + type document struct { title []string; text []string; @@ -107,13 +112,16 @@ func init() { func main() { // var words map[string]index - var p_dir string //, fname string; + var p_dir, w, fname string; var err error; - var i int; + var i, j int; + var words map[string]*wordList; + var cur *wordList; + var tmp *index; var files []os.FileInfo; var dir, fd *os.File; - var dir_info os.FileInfo; + var dir_info, fd_info os.FileInfo; var dir_mode os.FileMode; var doc *document; @@ -122,6 +130,8 @@ func main() { flag.Parse(); + words = make(map[string]*wordList); + dir, err = os.Open(p_dir); if err != nil { log.Printf("Error accessing \"%s\":\t%s\n", p_dir, err); @@ -142,8 +152,15 @@ func main() { os.Exit(1); } - for i = 0; i < len(files) && i < 1; i++ { + for i = 0; i < len(files); i++ { fd, err = os.Open(fmt.Sprintf("%s/%s", dir_info.Name(), files[i].Name())); + fd_info, err = fd.Stat(); + if err != nil { + log.Printf("Error getting info\n"); + os.Exit(1); + } + fname = fd_info.Name(); + if err != nil { log.Printf("Error reading %s/%s\n", dir_info.Name(), files[i].Name()); } else { @@ -151,10 +168,53 @@ func main() { if err != nil { log.Printf("Error parsing %s/%s\n", dir_info.Name(), files[i].Name()); } else { - fmt.Println(doc.text); - fmt.Println(doc.title); + /* Text */ + for j = 0; j < len(doc.text); j++ { + w = strings.ToLower(doc.text[j]); + + if words[w] == nil{ + tmp = &index{doc: fname, title: false, freq: 1}; + words[w] = &wordList{this: tmp, next: nil}; + } + + for cur = words[w];cur.next != nil && cur.this.doc != fname; cur = cur.next{} + + if cur.this.doc == fname { + cur.this.freq++ + } else if cur.next == nil { + tmp = &index{doc: fname, title: false, freq: 1}; + cur.next = &wordList{this: tmp, next: nil}; + } else { + panic(fmt.Sprintf("%v", cur)); + } + } + /* Title */ + for j = 0; j < len(doc.title); j++ { + w = strings.ToLower(doc.title[j]); + + if words[w] == nil{ + tmp = &index{doc: fname, title: true, freq: 1}; + words[w] = &wordList{this: tmp, next: nil}; + } + + for cur = words[w];cur.next != nil && cur.this.doc != fname; cur = cur.next{} + + if cur.this.doc == fname { + cur.this.title = true; + cur.this.freq++; + } else if cur.next == nil { + tmp = &index{doc: fname, title: true, freq: 1}; + cur.next = &wordList{this: tmp, next: nil}; + } else { + panic(fmt.Sprintf("%v", cur)); + } + } } } + /* TEST PRINT */ + for k,v := range words { + fmt.Printf("k: %s, doc: %s, title: %v, freq:%d\n",k,v.this.doc,v.this.title,v.this.freq); + } } } |