Beispiel #1
0
func simpleExample() {

	suggester := suggest.Suggest{}

	query := "fgerp"
	commands := []string{"cat", "mkdir", "fgrep", "history"}

	match, err := suggester.AutocorrectAgainst(query, commands)
	if err == nil {
		fmt.Println("Autocorrected to:", match) // "fgrep"
	}

	suggester.Commands = commands
	if match, err := suggester.Autocorrect(query); err == nil {
		fmt.Println("Also Autocorrected to:", match) // "fgrep"
	}

	query = "println"
	commands = []string{"Fprint", "Fprintf", "Fprintln", "Sprintf", "Print", "Printf", "Println"}
	suggester.Options.SimilarityMinimum = 8

	fmt.Printf("Searching %v for %s\n", query, commands)

	if result, err := suggester.QueryAgainst(query, commands); err == nil {
		if !result.Success() {
			fmt.Println("No close matches")
		} else {
			fmt.Println("Similar matches:", result.Matches) // [Println Fprintln]
			fmt.Println("Autocorrect:", result.Autocorrect) // Println

		}
	}
	os.Exit(0)
}
Beispiel #2
0
func (mh *matchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("content-type", "application/json")
	if r.Method != http.MethodGet {
		w.WriteHeader(http.StatusNotFound)
		return
	}
	mr := &matchResponse{}
	query := r.URL.Query().Get("q")
	if query == "" {
		mr.Error = "Must supply query parameter 'q' in URL."
		response, err := json.Marshal(mr)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusBadRequest)
		w.Write(response)
		return
	}
	suggester := suggest.Suggest{}

	suggester.Commands = matches
	match, err := suggester.Autocorrect(query)
	if err != nil {
		mr.Error = err.Error()
		response, err := json.Marshal(mr)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusInternalServerError)
		w.Write(response)
		return
	}

	if match == "" {
		mr.Error = "No match was found"
		response, err := json.Marshal(mr)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusNotFound)
		w.Write(response)
		return
	}

	mr.Changed = match != query
	mr.Match = match
	response, err := json.Marshal(mr)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusOK)
	w.Write(response)
	return
}
Beispiel #3
0
func TestAutocorrect(t *testing.T) {

	s := suggest.Suggest{}

	if s.Options.AutocorrectDisabled != false {
		t.Errorf("AutocorrectDisabled should be initialized to false")
	}

	for _, tt := range autocorrectTests {
		query := tt.query
		s.Commands = tt.commands
		actual, err := s.Autocorrect(query)
		if err != nil {
			t.Errorf("Autocorrect(%v) returned error %v", tt.query, err)
			continue
		}
		if actual != tt.expected {
			t.Errorf("Autocorrect(%q, %v) = %q, want %q", tt.query, tt.commands, actual, tt.expected)
		}
	}
}
Beispiel #4
0
func simpleExample2() {
	suggester := suggest.Suggest{}

	query := "fgerp"
	commands := []string{"cat", "mkdir", "fgrep", "history"}

	suggester.Commands = commands
	if match, err := suggester.Autocorrect("mkdri"); err == nil {
		fmt.Println("Autocorrected to:", match) // "mkdir"
	}

	// Alternate autocorrect usage pattern
	match, _ := suggester.AutocorrectAgainst(query, commands)
	if match != "" {
		fmt.Println("Autocorrected to:", match) // "fgrep"
	}

	// Alternate usage pattern
	query = "printf"
	commands = []string{"Fprint", "Fprintf", "Fprintln", "Sprintf", "Printf", "Println"}
	suggester.Options.SimilarityMinimum = 8

	fmt.Printf("Searching %v in %s\n", query, commands)

	if result, err := suggester.QueryAgainst(query, commands); err == nil {
		if !result.Success() {
			fmt.Println("No close matches")

		} else {
			fmt.Println("Similar matches:", result.Matches)
			// [Println Fprintln]

			fmt.Println("Autocorrect:", result.Autocorrect)
			// Println
		}
	}
}