Hướng dẫn how can you tell if two sentences are similar in python? - Làm thế nào bạn có thể biết nếu hai câu giống nhau trong python?

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.

Sự tương đồng về câu với 0.7003971105290047 0.9671912343259517 0.6121211244876517 4

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 5 sử dụng 0.7003971105290047 0.9671912343259517 0.6121211244876517 6 để tải xuống. Để sử dụng 0.7003971105290047 0.9671912343259517 0.6121211244876517 7 sử dụng 0.7003971105290047 0.9671912343259517 0.6121211244876517 8.

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.

//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 0.7003971105290047 0.9671912343259517 0.6121211244876517 9

//github.com/UKPLab/sentence-transformers

//www.sbert.net/docs/usage/semantic_textual_similarity.html

Cài đặt với 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. Điều này tạo ra sự nhúng câu.

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]

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.

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]])

Điều tương tự với 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("") 1 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("") 2,

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ủa câu với 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("") 3

//tfhub.dev/google/universal-sentence-encoder/4

//colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/semantic_similarity_with_tf_hub_universal_encoder.ipynb

Mô hình rất lớn cho cái này khoảng 1GB và có vẻ chậm hơn những người khác. Điều này cũng tạo ra nhúng cho các câu.

Code:

0.7003971105290047 0.9671912343259517 0.6121211244876517 0

Output:

0.7003971105290047 0.9671912343259517 0.6121211244876517 1

Code:

0.7003971105290047 0.9671912343259517 0.6121211244876517 2

Output:

0.7003971105290047 0.9671912343259517 0.6121211244876517 3

Câu khác nhúng thư viện

//github.com/facebookresearch/InferSent

//github.com/Tiiiger/bert_score

Hình minh họa này cho thấy phương pháp,

Tài nguyên

Làm thế nào để tính toán sự tương đồng giữa hai tài liệu văn bản?

//en.wikipedia.org/wiki/Cosine_similarity#Angular_distance_and_similarity

//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 so sánh hai câu trong Python?

So sánh các chuỗi bằng cách sử dụng == và! = Cách đơn giản nhất để kiểm tra xem hai chuỗi có bằng nhau trong Python có sử dụng toán tử == không. Và nếu bạn đang tìm kiếm điều ngược lại, thì! = Là những gì bạn cần.use the == operator. And if you are looking for the opposite, then != is what you need.

Làm thế nào để bạn biết nếu hai câu giống nhau?

Sự tương đồng về thứ tự từ là một cách để đánh giá sự tương tự của câu xem xét thứ tự của các từ.Hai câu thường giống nhau nếu cùng một từ tồn tại trong cả hai câu theo cùng một thứ tự.Tuy nhiên, các câu nên được coi là không hoàn toàn giống nhau nếu các từ của một câu có thứ tự khác nhau như câu khác.if same words exist in both sentences in the same order. However, sentences should be considered as not completely similar if words of a sentence have dif- ferent order as the other sentence.

Làm thế nào để bạn tìm thấy sự tương đồng giữa hai văn bản trong Python?

Cài đặt GENSIM, lấy bộ dữ liệu của Text Text8 để đào tạo mô hình Doc2VEC.Gắn thẻ dữ liệu văn bản, sau đó sử dụng nó để xây dựng từ vựng mô hình và đào tạo mô hình.Sử dụng mô hình để có được các bản nhúng câu của các tiêu đề và tính toán độ tương tự cosin giữa chúng.

Làm cách nào để tìm thấy sự tương đồng về văn bản?

Introduction..
Lấy một dòng câu, biến nó thành một vector ..
Lấy nhiều hình phạt khác và thay đổi chúng thành các vectơ ..
Câu giao ngay với khoảng cách ngắn nhất (Euclide) hoặc góc nhỏ nhất (độ tương tự cosin) trong số đó ..
Chúng tôi ngay lập tức nhận được một tiêu chuẩn về sự tương đồng ngữ nghĩa kết nối các câu ..

Chủ đề