Perform Semantic Similarity Comparison of Two Strings in Python
Cloudmersive’s NLP API v2 offers a variety of iterations with useful analytical features, enabling you to quickly determine important traits of inputted sentence text. One such iteration — /nlp-v2/analytics/similarity — enables your application to analyze two input text strings and determine their semantic similarity. This perceptive NLP ability is uses Deep Learning to perform the similarity comparison.
To get set up with this API, you’ll first have to install the Python SDK with the below code:
pip install cloudmersive-nlp-api-clientfrom __future__ import print_function
import time
import cloudmersive_nlp_api_client
from cloudmersive_nlp_api_client.rest import ApiException
from pprint import pprint
After that, you’ll find a code snippet to authorize your API key from your free Cloudmersive account:
# Configure API key authorization: Apikey configuration = cloudmersive_nlp_api_client.Configuration() configuration.api_key['Apikey'] = 'YOUR_API_KEY'
Last but not least, it’s time to copy in the callback function, and you’re ready to go:
# create an instance of the API class
api_instance = cloudmersive_nlp_api_client.AnalyticsApi(cloudmersive_nlp_api_client.ApiClient(configuration))
input = cloudmersive_nlp_api_client.SimilarityAnalysisRequest() # SimilarityAnalysisRequest | Input similarity analysis requesttry:
# Perform Semantic Similarity Comparison of Two Strings
api_response = api_instance.analytics_similarity(input)
pprint(api_response)
except ApiException as e:
print("Exception when calling AnalyticsApi->analytics_similarity: %s\n" % e)