-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (87 loc) · 2.74 KB
/
main.go
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
package main
import (
"fmt"
"net/http"
"strings"
"unicode"
)
// Initialize the Trie
var trie = NewTrie()
func main() {
fmt.Println("Starting server on port 8080")
initialWords := []string{"hello", "world", "hi", "hey", "how", "are", "you", "doing", "today"}
for _, word := range initialWords {
trie.Insert(strings.ToLower(word))
}
http.HandleFunc("/", serveHTML)
http.HandleFunc("/autocomplete", autocompleteHandler)
http.ListenAndServe(":8080", nil)
}
// Serve the index.html file
func serveHTML(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "view/index.html")
}
// Suggestion represents a single autocomplete suggestion
type Suggestion struct {
Value string `json:"value"`
}
// handle autocomplete requests
// autocompleteHandler handles HTTP requests for the autocomplete feature.
// It reads the query parameter "q" from the request URL, processes the input text,
// and returns a list of suggestions in HTML format.
//
// The function performs the following steps:
// 1. Trims whitespace from the query and splits it into words.
// 2. If there are multiple words, it adds valid words (longer than 2 characters) to the trie.
// 3. Searches the trie for suggestions based on the last word in the query.
// 4. Constructs an HTML response with the suggestions and writes it to the response writer.
//
// Parameters:
// - w: The HTTP response writer.
// - r: The HTTP request.
func autocompleteHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query().Get("q")
text := strings.TrimSpace(query)
words := strings.Fields(text)
if len(words) == 0 {
return
}
lastWord := words[len(words)-1]
// add new word to trie
if len(words) > 1 {
for _, word := range words[:len(words)-1] {
word = strings.ToLower(word)
if isValidWord(word) && len(word) > 2 { // Only add words longer than 2 characters
trie.Insert(word)
}
}
}
suggestions := trie.Search(lastWord)
var response strings.Builder
for _, suggestion := range suggestions {
// send a list of suggestions in HTML format
response.WriteString(fmt.Sprintf("<li>%s</li>", suggestion))
}
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(response.String()))
}
func isValidWord(word string) bool {
for _, r := range word {
if !unicode.IsLetter(r) {
return false
}
}
return true
}
// Handle autocomplete requests
// func autocompleteHandler(w http.ResponseWriter, r *http.Request) {
// query := r.URL.Query().Get("q")
// fmt.Printf("Query: %s\n", query)
// suggestions := trie.Search(strings.ToLower(query))
// var response strings.Builder
// for _, suggestion := range suggestions {
// response.WriteString(fmt.Sprintf("<li>%s</li>", suggestion))
// }
// w.Header().Set("Content-Type", "text/html")
// w.Write([]byte(response.String()))
// }