Hướng dẫn how do you check if two sentences are similar in python? - làm cách nào để kiểm tra xem hai câu có giống nhau trong python không?
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. Show
Sự tương đồng về câu với 0.7003971105290047 0.9671912343259517 0.6121211244876517 4Các mô hình bắt buộc phải được tải trước. Để sử dụng 5 sử dụng 6 để tải xuống.Để sử dụng 7 sử dụng 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. https://spacy.io/usage/vectors-similarity/ Code:
Output:
Sự tương đồng về câu với 0.7003971105290047 0.9671912343259517 0.6121211244876517 9https://github.com/UKPLab/sentence-transformers https://www.sbert.net/docs/usage/semantic_textual_similarity.html Cài đặt với 0.Điều này tạo ra sự nhúng câu.Code:
Output:
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:
Output:
Điều tương tự với 1 và 2,Code:
Output:
Code:
Output:
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("") 3https://tfhub.dev/google/universal-sentence-encoder/4 https://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: 0Output: 1Code: 2Output: 3Câu khác nhúng thư việnhttps://github.com/facebookresearch/InferSent https://github.com/Tiiiger/bert_score Hình minh họa này cho thấy phương pháp, Tài nguyênLàm thế nào để tính toán sự tương đồng giữa hai tài liệu văn bản? https://en.wikipedia.org/wiki/Cosine_similarity#Angular_distance_and_similarity https://towardsdatascience.com/word-distance-between-word-embeddings-cc3e9cf1d632 https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.cosine.html https://www.tensorflow.org/api_docs/python/tf/keras/losses/CosineSimilarity https://nlp.town/blog/sentence-similarity/ |