当前位置: 首页>>代码示例>>Golang>>正文


Golang suggest.Suggest类代码示例

本文整理汇总了Golang中github.com/anchepiece/suggest.Suggest的典型用法代码示例。如果您正苦于以下问题:Golang Suggest类的具体用法?Golang Suggest怎么用?Golang Suggest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Suggest类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ServeHTTP

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
}
开发者ID:anchepiece,项目名称:suggest,代码行数:58,代码来源:api.go

示例2: TestCalculateSimilarity

// TestCalculateSimilarity tests the expected distance using the default parameters
func TestCalculateSimilarity(t *testing.T) {

	s := suggest.Suggest{}

	for _, tt := range distanceTests {
		actual := s.CalculateSimilarity(tt.words[0], tt.words[1])
		if actual != tt.expected {
			t.Errorf("CalculateSimilarity(%v) = %d, want %d", tt.words, actual, tt.expected)
		}
	}
}
开发者ID:anchepiece,项目名称:suggest,代码行数:12,代码来源:suggest_test.go

示例3: simpleExample

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)
}
开发者ID:anchepiece,项目名称:suggest-autocorrect,代码行数:34,代码来源:main.go

示例4: TestExactMatchAgainst

func TestExactMatchAgainst(t *testing.T) {

	s := suggest.Suggest{}

	for _, tt := range writeTests {
		query := tt.query
		commands := tt.commands
		actual := s.ExactMatchAgainst(query, commands)

		if actual != tt.expected {
			t.Errorf("ExactMatchAgainst(%q, %v) = %q, want %q", tt.query, tt.commands, actual, tt.expected)
		}
	}
}
开发者ID:anchepiece,项目名称:suggest,代码行数:14,代码来源:suggest_test.go

示例5: TestQueryAgainst

func TestQueryAgainst(t *testing.T) {

	s := suggest.Suggest{}

	for _, tt := range queryTests {
		query := tt.query
		result, err := s.QueryAgainst(query, tt.commands)
		actual := result.Matches

		if err != nil {
			t.Errorf("TestQueryAgainst(%v) returned error %v", tt.query, err)
			continue
		}

		if !reflect.DeepEqual(actual, tt.expected) {
			t.Errorf("TestQueryAgainst(%q, %v) = %q, want %q", tt.query, tt.commands, actual, tt.expected)
		}
	}
}
开发者ID:anchepiece,项目名称:suggest,代码行数:19,代码来源:suggest_test.go

示例6: simpleExample3

func simpleExample3() {
	// make sure our example in doc.go works
	suggester := suggest.Suggest{}
	suggester.Options.SimilarityMinimum = 7
	suggester.Options.AutocorrectDisabled = false

	query := "proflie"
	commands := []string{"perfil", "profiel", "profile", "profil", "account"}

	suggester.Commands = commands
	if result, err := suggester.Query(query); err == nil {
		if !result.Success() {
			fmt.Println("No close matches")
		} else {
			fmt.Println("Similar matches:", result.Matches) // [profile profil profiel]
			fmt.Println("Autocorrect:", result.Autocorrect) // profile
		}
	}
	os.Exit(0)
}
开发者ID:anchepiece,项目名称:suggest-autocorrect,代码行数:20,代码来源:main.go

示例7: TestAutocorrectAgainst

func TestAutocorrectAgainst(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.AutocorrectAgainst(query, tt.commands)
		if err != nil {
			t.Errorf("AutocorrectAgainst(%v) returned error %v", tt.query, err)
			continue
		}
		if actual != tt.expected {
			t.Errorf("AutocorrectAgainst(%q, %v) = %q, want %q", tt.query, tt.commands, actual, tt.expected)
		}
	}
}
开发者ID:anchepiece,项目名称:suggest,代码行数:21,代码来源:suggest_test.go

示例8: simpleExample2

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
		}
	}
}
开发者ID:anchepiece,项目名称:suggest-autocorrect,代码行数:37,代码来源:main.go


注:本文中的github.com/anchepiece/suggest.Suggest类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。