Hầu hết các thư viện dưới đây nên là lựa chọn tốt để so sánh sự tương tự ngữ nghĩa. Bạn có thể bỏ qua so sánh từ trực tiếp bằng cách tạo từ hoặc vectơ câu bằng các mô hình đã được đặt trước từ các thư viện này.
Nội phân chính
- Sự tương đồng về câu với Spacy
- Sự tương đồng về câu với các máy biến áp câu
- Sự tương đồng câu với bộ mã hóa câu toàn cầu tfhub
- Câu khác nhúng thư viện
- Làm thế nào để bạn tìm thấy sự tương đồng giữa hai câu trong Python?
- Làm thế nào để bạn kiểm tra độ tương tự văn bản trong Python?
- Làm thế nào để bạn tìm thấy sự giống nhau của một câu?
- Làm thế nào để python tính toán độ tương tự?
Sự tương đồng về câu với Spacy
Sự tương đồng về câu với các máy biến áp câu
Sự tương đồng câu với bộ mã hóa câu toàn cầu tfhub
Câu khác nhúng thư viện
//spacy.io/usage/vectors-similarity/
Code:
import spacy
nlp = spacy.load["en_core_web_lg"]
#nlp = spacy.load["en_core_web_md"]
doc1 = nlp[u'the person wear red T-shirt']
doc2 = nlp[u'this person is walking']
doc3 = nlp[u'the boy wear red T-shirt']
print[doc1.similarity[doc2]]
print[doc1.similarity[doc3]]
print[doc2.similarity[doc3]]
Output:
0.7003971105290047
0.9671912343259517
0.6121211244876517
Sự tương đồng về câu với các máy biến áp câu
//github.com/UKPLab/sentence-transformers
//www.sbert.net/docs/usage/semantic_textual_similarity.html
Sự tương đồng câu với bộ mã hóa câu toàn cầu tfhub
Code:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer['distilbert-base-nli-mean-tokens']
sentences = [
'the person wear red T-shirt',
'this person is walking',
'the boy wear red T-shirt'
]
sentence_embeddings = model.encode[sentences]
for sentence, embedding in zip[sentences, sentence_embeddings]:
print["Sentence:", sentence]
print["Embedding:", embedding]
print[""]
Output:
Sentence: the person wear red T-shirt
Embedding: [ 1.31643847e-01 -4.20616418e-01 ... 8.13076794e-01 -4.64620918e-01]
Sentence: this person is walking
Embedding: [-3.52878094e-01 -5.04286848e-02 ... -2.36091137e-01 -6.77282438e-02]
Sentence: the boy wear red T-shirt
Embedding: [-2.36365378e-01 -8.49713564e-01 ... 1.06414437e+00 -2.70157874e-01]
Câu khác nhúng thư viện
Code:
from sentence_transformers import SentenceTransformer, util
print[util.pytorch_cos_sim[sentence_embeddings[0], sentence_embeddings[1]]]
print[util.pytorch_cos_sim[sentence_embeddings[0], sentence_embeddings[2]]]
print[util.pytorch_cos_sim[sentence_embeddings[1], sentence_embeddings[2]]]
Output:
tensor[[[0.4644]]]
tensor[[[0.9070]]]
tensor[[[0.3276]]]
Làm thế nào để bạn tìm thấy sự tương đồng giữa hai câu trong Python?
Code:
from scipy.spatial import distance
print[1 - distance.cosine[sentence_embeddings[0], sentence_embeddings[1]]]
print[1 - distance.cosine[sentence_embeddings[0], sentence_embeddings[2]]]
print[1 - distance.cosine[sentence_embeddings[1], sentence_embeddings[2]]]
Output:
0.4643629193305969
0.9069876074790955
0.3275738060474396
Code:
import torch.nn
cos = torch.nn.CosineSimilarity[dim=0, eps=1e-6]
b = torch.from_numpy[sentence_embeddings]
print[cos[b[0], b[1]]]
print[cos[b[0], b[2]]]
print[cos[b[1], b[2]]]
Output:
tensor[0.4644]
tensor[0.9070]
tensor[0.3276]
Sự tương đồng câu với bộ mã hóa câu toàn cầu tfhub
Câu khác nhúng thư viện
//colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/semantic_similarity_with_tf_hub_universal_encoder.ipynb
Làm thế nào để bạn tìm thấy sự tương đồng giữa hai câu trong Python?
Code:
0.7003971105290047
0.9671912343259517
0.6121211244876517
0Output:
0.7003971105290047
0.9671912343259517
0.6121211244876517
1Code:
0.7003971105290047
0.9671912343259517
0.6121211244876517
2Output:
0.7003971105290047
0.9671912343259517
0.6121211244876517
3Câu khác nhúng thư viện
//github.com/facebookresearch/InferSent
//github.com/Tiiiger/bert_score
Làm thế nào để bạn tìm thấy sự tương đồng giữa hai câu trong Python?
Làm thế nào để bạn kiểm tra độ tương tự văn bản trong Python?
Làm thế nào để bạn tìm thấy sự giống nhau của một câu?
Làm thế nào để python tính toán độ tương tự?
//towardsdatascience.com/word-distance-between-word-embeddings-cc3e9cf1d632
//docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.cosine.html
//www.tensorflow.org/api_docs/python/tf/keras/losses/CosineSimilarity
//nlp.town/blog/sentence-similarity/
Làm thế nào để bạn tìm thấy sự tương đồng giữa hai câu trong Python?
Làm thế nào để bạn kiểm tra độ tương tự văn bản trong Python?.
Làm thế nào để bạn tìm thấy sự giống nhau của một câu?
Làm thế nào để python tính toán độ tương tự?
Các mô hình bắt buộc phải được tải trước.
Để sử dụng
0.7003971105290047
0.9671912343259517
0.6121211244876517
4 sử dụng 0.7003971105290047
0.9671912343259517
0.6121211244876517
5 để tải xuống. Để sử dụng 0.7003971105290047
0.9671912343259517
0.6121211244876517
6 sử dụng 0.7003971105290047
0.9671912343259517
0.6121211244876517
7.Mô hình lớn là khoảng ~ 830MB dưới dạng viết và khá chậm, vì vậy trung bình có thể là một lựa chọn tốt.
Cài đặt với
0.7003971105290047
0.9671912343259517
0.6121211244876517
8. Điều này tạo ra sự nhúng câu.Làm thế nào để bạn kiểm tra độ tương tự văn bản trong Python?
Làm thế nào để bạn tìm thấy sự giống nhau của một câu?Install Gensim, get the “ text8 ” dataset to train the Doc2Vec model. Tag the text data, then use it to build the model vocabulary and train the model. Use the model to get the sentence embeddings of the headlines and calculate the cosine similarity between them.
Làm thế nào để bạn tìm thấy sự giống nhau của một câu?
Làm thế nào để python tính toán độ tương tự?.
Các mô hình bắt buộc phải được tải trước.
Để sử dụng
0.7003971105290047
0.9671912343259517
0.6121211244876517
4 sử dụng 0.7003971105290047
0.9671912343259517
0.6121211244876517
5 để tải xuống. Để sử dụng 0.7003971105290047
0.9671912343259517
0.6121211244876517
6 sử dụng 0.7003971105290047
0.9671912343259517
0.6121211244876517
7.Mô hình lớn là khoảng ~ 830MB dưới dạng viết và khá chậm, vì vậy trung bình có thể là một lựa chọn tốt.
Cài đặt với
0.7003971105290047
0.9671912343259517
0.6121211244876517
8. Điều này tạo ra sự nhúng câu.Làm thế nào để python tính toán độ tương tự?
Các mô hình bắt buộc phải được tải trước..
Để sử dụng
0.7003971105290047
0.9671912343259517
0.6121211244876517
4 sử dụng 0.7003971105290047
0.9671912343259517
0.6121211244876517
5 để tải xuống. Để sử dụng 0.7003971105290047
0.9671912343259517
0.6121211244876517
6 sử dụng 0.7003971105290047
0.9671912343259517
0.6121211244876517
7.Mô hình lớn là khoảng ~ 830MB dưới dạng viết và khá chậm, vì vậy trung bình có thể là một lựa chọn tốt.
Cài đặt với
0.7003971105290047
0.9671912343259517
0.6121211244876517
8. Điều này tạo ra sự nhúng câu.Bây giờ vectơ nhúng có thể được sử dụng để tính toán các số liệu tương tự khác nhau.
Levenshtein..
Điều tương tự với
0.7003971105290047
0.9671912343259517
0.6121211244876517
9 và from sentence_transformers import SentenceTransformer
model = SentenceTransformer['distilbert-base-nli-mean-tokens']
sentences = [
'the person wear red T-shirt',
'this person is walking',
'the boy wear red T-shirt'
]
sentence_embeddings = model.encode[sentences]
for sentence, embedding in zip[sentences, sentence_embeddings]:
print["Sentence:", sentence]
print["Embedding:", embedding]
print[""]
0,//tfhub.dev/google/universal-sentence-encoder/4
Damerau-Levenshtein..