# similarity **Repository Path**: weidongkl/similarity ## Basic Information - **Project Name**: similarity - **Description**: calculating the similarity between strings using various algorithms 计算字符串的相似度 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2024-07-05 - **Last Updated**: 2025-06-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: Go语言, similarity ## README # Similarity This project provides a Go package for calculating the similarity between strings using various algorithms. It supports different similarity measures through the `Calculator` interface and includes functions to compute and sort similarities. ## Features - **Modular Design**: Use different similarity measures by implementing the `Calculator` interface. - **Efficient Calculation**: Concurrent processing for calculating similarities in arrays. - **Priority Queue**: Sort and retrieve strings based on their similarity to a given string. ## Installation To install the package, use the following command: ```sh go get gitee.com/weidongkl/similarity ``` ## Usage ### Implementing a Similarity Measure To create a custom similarity measure, implement the `Calculator` interface: ```go type Calculator interface { Calculate(s1, s2 string) (float64, error) } ``` ### Example: Cosine Here's an example of how to use the Cosine as a similarity measure: ```go package main import ( "fmt" "gitee.com/weidongkl/similarity" "gitee.com/weidongkl/similarity/cosine" ) func main() { calc := cosine.New() s1 := "string1" s2 := "string2" similarity, err := calc.Calculate(s1, s2) if err != nil { fmt.Println("Error calculating similarity:", err) } else { fmt.Printf("Similarity between %s and %s: %f\n", s1, s2, similarity) } } ``` ### Sorting Strings by Similarity To sort an array of strings by their similarity to a given string, use the `CalculateArray` function: ```go package main import ( "fmt" "gitee.com/weidongkl/similarity" "gitee.com/weidongkl/similarity/jaccard" ) func main() { calc := jaccard.New() s1 := "base" sArr := []string{"test1", "test2", "test3"} sortedItems, err := similarity.CalculateArray(calc, s1, sArr) if err != nil { fmt.Println("Error calculating similarities:", err) } else { fmt.Println("Sorted items by similarity:") for _, item := range sortedItems { fmt.Printf("%s: %f\n", item.Value, item.Priority) } } } ``` ## Directory Structure ``` similarity/ ├── hamming/ │ └── hamming.go ├── jaccard/ │ └── jaccard.go ├── cosine/ │ └── cosine.go ├── levenshtein/ │ └── levenshtein.go ├── similarity.go ├── errors.go ├── heap.go ├── heap_test.go ├── similarity_test.go ├── go.mod ├── README.md └── .gitignore ``` ## Contributing 1. Fork the repository. 2. Create a new branch for your feature. 3. Commit your changes. 4. Push the branch. 5. Create a pull request. ## License This project is licensed under the MIT License. ## Contact For any questions or issues, please contact weidongkx@gmail.com.