How to Perform Semantic Similarity Comparison of Two Strings in Go
Semantic similarity comparisons are a key ingredient for effective NLP, measuring the extent to which two things might mean the same thing. You can conveniently include our NLP API’s Semantic Similarity Comparison iteration into your Golang project using ready-to-run code below. When you call this API, you’ll receive a Semantic Similarity Score indicating the degree to which two input text strings semantically resembled one another, along with a Sentence Count (and a true/false response to indicate that the API call was successful).
Before you copy the below code, first head to our website (www.cloudmersive.com) and create an account to get an API key. There are several account options available, and you can easily make a free-tier account with zero financial commitments that comes with a limit of 800 API calls per month. Once that’s taken care of, copy the below snippets and place your input your text strings where indicated:
package mainimport (
"fmt"
"strings"
"net/http"
"io/ioutil"
)func main() {url := "https://api.cloudmersive.com/nlp-v2/analytics/similarity"
method := "POST"payload := strings.NewReader("TextToAnalyze1=%3Cstring%3E&TextToAnalyze2=%3Cstring%3E")client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}