cosine_similarity_between_texts¶
- pyhelpers.text.cosine_similarity_between_texts(txt1, txt2, cosine_distance=False)[source]¶
Calculate the cosine similarity between two sentences.
- Parameters:
txt1 (str) – The first text.
txt2 (str) – The second text.
cosine_distance (bool) – Whether to return cosine distance (i.e. 1 - cosine similarity); defaults to
False
.
- Returns:
The cosine similarity (or distance) between the input texts.
- Return type:
float
Examples:
>>> from pyhelpers.text import cosine_similarity_between_texts >>> txt1, txt2 = 'This is an apple.', 'That is a pear.' >>> cos_sim = cosine_similarity_between_texts(txt1, txt2) >>> cos_sim 0.25 >>> cos_dist = cosine_similarity_between_texts(txt1, txt2, cosine_distance=True) >>> cos_dist # 1 - cos_sim 0.75