Answers.Body: <p>The easiest way to add some sort of structural similarity measure is to use n-grams; in your case bigrams might be sufficient.</p>
  
  <p>Go through each sentence and collect pairs of words, such as:</p>
  
  <ul>
  <li>"python is", "is a", "a good", "good language".</li>
  </ul>
  
  <p>Your other sentence has</p>
  
  <ul>
  <li>"language a", "a good", "good python", "python is". </li>
  </ul>
  
  <p>Out of eight bigrams you have two which are the same ("python is" and "a good"), so you could say that the structural similarity is 2/8.</p>
  
  <p>Of course you can also be more flexible if you already know that two words are semantically related. If you want to say that <em>Python is a good language</em> is structurally similar/identical to <em>Java is a great language</em>, then you could add that to the comparison so that you effectively process "[PROG_LANG] is a [POSITIVE-ADJ] language", or something similar.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-22T07:29:47.160Z
Answers.Text: Should the similarity score be 2/8? If we consider the case that both the sentences are completely similar, score would be 4/8 = 0.5 instead of 1. A normalized score should be a better metric for this. Any ideas on how that can be achieved?
Answers.UserDisplayName: Vedang Waradpande
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-15T08:09:51.907Z
Answers.LastActivityDate: 2018-05-15T08:09:51.907Z
Answers.UserDisplayName: Oliver Mason
Answers.Body: <blockquote>
    <p><strong>The best approach at this time (2019):</strong></p>
  </blockquote>
  
  <p>The most efficient approach now is to use <a href="https://tfhub.dev/google/universal-sentence-encoder/1" rel="noreferrer"><strong>Universal Sentence Encoder by Google</strong></a> (<a href="https://arxiv.org/abs/1803.11175" rel="noreferrer">paper_2018</a>) which computes semantic similarity between sentences using the dot product of their embeddings <em>(i.e learned vectors of 215 values)</em>. Similarity is a float number between 0 <em>(i.e no similarity)</em> and 1 <em>(i.e strong similarity).</em> </p>
  
  <p>The implementation is now integrated to Tensorflow Hub and can easily be used. Here is a ready-to-use code to compute the similarity between 2 sentences. Here I will get the similarity between <strong><em>"Python is a good language"</em></strong> and <strong><em>"Language a good python is"</em></strong> as in your example.</p>
  
  <blockquote>
    <p><strong>Code example:</strong> </p>
  </blockquote>
  
  <pre><code>#Requirements: Tensorflow&gt;=1.7 tensorflow-hub numpy
  
  import tensorflow as tf
  import tensorflow_hub as hub
  import numpy as np
  
  module_url = "https://tfhub.dev/google/universal-sentence-encoder-large/3" 
  embed = hub.Module(module_url)
  sentences = ["Python is a good language","Language a good python is"]
  
  similarity_input_placeholder = tf.placeholder(tf.string, shape=(None))
  similarity_sentences_encodings = embed(similarity_input_placeholder)
  
  with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    session.run(tf.tables_initializer())
    sentences_embeddings = session.run(similarity_sentences_encodings, feed_dict={similarity_input_placeholder: sentences})
    similarity = np.inner(sentences_embeddings[0], sentences_embeddings[1])
    print("Similarity is %s" % similarity)
  </code></pre>
  
  <blockquote>
    <p><strong>Output:</strong></p>
  </blockquote>
  
  <pre><code>Similarity is 0.90007496 #Strong similarity
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-25T04:20:57.427Z
Answers.Text: Another option in 2019 is BERT sentence embeddings - you can see example code here -https://github.com/hanxiao/bert-as-service
Answers.UserDisplayName: Adnan S
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-09T21:19:15.330Z
Answers.LastActivityDate: 2019-05-09T21:19:15.330Z
Answers.UserDisplayName: HLeb
Answers.Body: <p>Firstly, before we commence I recommend that you refer to similar questions on the network such as <a href="https://datascience.stackexchange.com/questions/25053/best-practical-algorithm-for-sentence-similarity">https://datascience.stackexchange.com/questions/25053/best-practical-algorithm-for-sentence-similarity</a> and <a href="https://stackoverflow.com/questions/62328/is-there-an-algorithm-that-tells-the-semantic-similarity-of-two-phrases">https://stackoverflow.com/questions/62328/is-there-an-algorithm-that-tells-the-semantic-similarity-of-two-phrases</a></p>
  
  <p>To determine the similarity of sentences we need to consider what kind of data we have. For example if you had a labelled dataset i.e. similar sentences and disimilar sentences then a straight forward approach could have been to use a supervised algorithm to classify the  sentences. </p>
  
  <p>An approach that could determine sentence structural similarity would be to average the word vectors generated by word embedding algorithms i.e word2vec. These algorithms create a vector for each word and the cosine similarity among them represents semantic similarity among words. (Daniel L 2017)</p>
  
  <p>Using word vectors we can use the following metrics to determine the similarity of words. </p>
  
  <ul>
  <li>Cosine distance between word embeddings of the words</li>
  <li>Euclidean distance between word embeddings of the words</li>
  </ul>
  
  <p>Cosine similarity is a measure of the similarity between two non-zero vectors of an inner product space that measures the cosine of the angle between them. The cosine angle is the measure of overlap between the sentences in terms of their content. </p>
  
  <p>The Euclidean distance  between two word vectors provides an effective method for measuring the linguistic or semantic similarity of the corresponding words. (Frank D 2015)</p>
  
  <p>Alternatively you could calculate the eigenvector of the sentences to determine sentence similarity. </p>
  
  <p>Eigenvectors are a special set of vectors associated with a linear system of equations (i.e. matrix equation). Here a sentence similarity matrix is generated for each cluster and the eigenvector for the matrix is calculated. You can read more on Eigenvector based approach to sentence ranking on this paper <a href="https://pdfs.semanticscholar.org/ca73/bbc99be157074d8aad17ca8535e2cd956815.pdf" rel="noreferrer">https://pdfs.semanticscholar.org/ca73/bbc99be157074d8aad17ca8535e2cd956815.pdf</a></p>
  
  <p>For source code Siraj Rawal has a Python notebook to create a set of word vectors. The word vectors can then be used to find the similarity between words. The source code is available here <a href="https://github.com/llSourcell/word_vectors_game_of_thrones-LIVE" rel="noreferrer">https://github.com/llSourcell/word_vectors_game_of_thrones-LIVE</a></p>
  
  <p>Another option is a tutorial from Oreily that utilizes the gensin Python library to determine the similarity between documents. This tutorial uses NLTK to tokenize then creates a tf-idf (term frequency-inverse document frequency) model from the corpus.  The tf-idf is then used to determine the similarity of the documents. The tutorial is available here <a href="https://www.oreilly.com/learning/how-do-i-compare-document-similarity-using-python" rel="noreferrer">https://www.oreilly.com/learning/how-do-i-compare-document-similarity-using-python</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-16T17:32:35.590Z
Answers.Text: Thanks for providing valuable details for the problem. I had seen the example of gensim but I have a question will it be able to solve the problem that I mentioned in question. Although the solution I created is working fine in finding the similarity between sentences but it is getting stuck when order of words is jumbled.
Answers.UserDisplayName: Shubham Tiwari
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-14T08:31:26.910Z
Answers.LastActivityDate: 2018-01-14T19:32:47.363Z
Answers.LastEditDate: 2018-01-14T19:32:47.363Z
Answers.UserDisplayName: Seth Simba
Body: <p>I am working on a problem where I need to determine whether two sentences are similar or not.  I implemented a solution using BM25 algorithm and wordnet synsets for determining syntactic &amp; semantic similarity.  The solution is working adequately, and even if the word order in the sentences is jumbled, it is measuring that two sentences are similar. For example</p>
  
  <ol>
  <li>Python is a good language.</li>
  <li>Language a good python is.</li>
  </ol>
  
  <p>My problem is to determine that these two sentences are similar.  </p>
  
  <ul>
  <li>What could be the possible solution for structural similarity? </li>
  <li>How will I maintain the structure of sentences?</li>
  </ul>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-11T02:26:14.577Z
Comments.Text: You may be able to use sentence vectors and compare them.
Comments.UserDisplayName: Aiden Grossman
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-16T12:39:43.470Z
Comments.Text: I highly suggest you to use Gensim (https://radimrehurek.com/gensim/) for this task. Especially the models LSI and/or word2vec and fasttext
Comments.UserDisplayName: Robin
Tags: <natural-language-processing><python><similarity>
Title: How do I compute the structural similarity between sentences?
UserDisplayName: Shubham Tiwari
ThreadText: # How do I compute the structural similarity between sentences?
  
  (Asked by: username_0 on 2018-01-10)
  
  I am working on a problem where I need to determine whether two sentences are similar or not. I implemented a solution using BM25 algorithm and wordnet synsets for determining syntactic & semantic similarity. The solution is working adequately, and even if the word order in the sentences is jumbled, it is measuring that two sentences are similar. For example
  
  1. Python is a good language.
  2. Language a good python is.
  
  My problem is to determine that these two sentences are similar.
  
  * What could be the possible solution for structural similarity?
  * How will I maintain the structure of sentences?
  
  **Tags:** natural-language-processing, python, similarity
  > **Comments:**
  > **username_1**: You may be able to use sentence vectors and compare them.  
  > **username_2**: I highly suggest you to use Gensim (https://radimrehurek.com/gensim/) for this task. Especially the models LSI and/or word2vec and fasttext  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2018-05-15. Score: 2 | Accepted answer
  
  The easiest way to add some sort of structural similarity measure is to use n-grams; in your case bigrams might be sufficient.
  
  Go through each sentence and collect pairs of words, such as:
  
  * "python is", "is a", "a good", "good language".
  
  Your other sentence has
  
  * "language a", "a good", "good python", "python is".
  
  Out of eight bigrams you have two which are the same ("python is" and "a good"), so you could say that the structural similarity is 2/8.
  
  Of course you can also be more flexible if you already know that two words are semantically related. If you want to say that *Python is a good language* is structurally similar/identical to *Java is a great language*, then you could add that to the comparison so that you effectively process "[PROG\_LANG] is a [POSITIVE-ADJ] language", or something similar.
  
  > **Comments:**
  > **username_4**: Should the similarity score be 2/8? If we consider the case that both the sentences are completely similar, score would be 4/8 = 0.5 instead of 1. A normalized score should be a better metric for this. Any ideas on how that can be achieved?  
  
  ---
  
  ### Answer by username_5 on 2019-05-09. Score: 5
  
  The best approach at this time (2019):
  
  The most efficient approach now is to use Universal Sentence Encoder by Google (paper_2018) which computes semantic similarity between sentences using the dot product of their embeddings (i.e learned vectors of 215 values). Similarity is a float number between 0 (i.e no similarity) and 1 (i.e strong similarity). 
  The implementation is now integrated to Tensorflow Hub and can easily be used. Here is a ready-to-use code to compute the similarity between 2 sentences. Here I will get the similarity between "Python is a good language" and "Language a good python is" as in your example.
  
  Code example: 
  
  #Requirements: Tensorflow>=1.7 tensorflow-hub numpy
  
  import tensorflow as tf
  import tensorflow_hub as hub
  import numpy as np
  
  module_url = "https://tfhub.dev/google/universal-sentence-encoder-large/3" 
  embed = hub.Module(module_url)
  sentences = ["Python is a good language","Language a good python is"]
  
  similarity_input_placeholder = tf.placeholder(tf.string, shape=(None))
  similarity_sentences_encodings = embed(similarity_input_placeholder)
  
  with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    session.run(tf.tables_initializer())
    sentences_embeddings = session.run(similarity_sentences_encodings, feed_dict={similarity_input_placeholder: sentences})
    similarity = np.inner(sentences_embeddings[0], sentences_embeddings[1])
    print("Similarity is %s" % similarity)
  
  
  Output:
  
  Similarity is 0.90007496 #Strong similarity
  
  > **Comments:**
  > **username_6**: Another option in 2019 is BERT sentence embeddings - you can see example code here -https://github.com/hanxiao/bert-as-service  
  
  ---
  
  ### Answer by username_7 on 2018-01-14. Score: 5
  
  Firstly, before we commence I recommend that you refer to similar questions on the network such as <https://datascience.stackexchange.com/questions/25053/best-practical-algorithm-for-sentence-similarity> and <https://stackoverflow.com/questions/62328/is-there-an-algorithm-that-tells-the-semantic-similarity-of-two-phrases>
  
  To determine the similarity of sentences we need to consider what kind of data we have. For example if you had a labelled dataset i.e. similar sentences and disimilar sentences then a straight forward approach could have been to use a supervised algorithm to classify the sentences.
  
  An approach that could determine sentence structural similarity would be to average the word vectors generated by word embedding algorithms i.e word2vec. These algorithms create a vector for each word and the cosine similarity among them represents semantic similarity among words. (Daniel L 2017)
  
  Using word vectors we can use the following metrics to determine the similarity of words.
  
  * Cosine distance between word embeddings of the words
  * Euclidean distance between word embeddings of the words
  
  Cosine similarity is a measure of the similarity between two non-zero vectors of an inner product space that measures the cosine of the angle between them. The cosine angle is the measure of overlap between the sentences in terms of their content.
  
  The Euclidean distance between two word vectors provides an effective method for measuring the linguistic or semantic similarity of the corresponding words. (Frank D 2015)
  
  Alternatively you could calculate the eigenvector of the sentences to determine sentence similarity.
  
  Eigenvectors are a special set of vectors associated with a linear system of equations (i.e. matrix equation). Here a sentence similarity matrix is generated for each cluster and the eigenvector for the matrix is calculated. You can read more on Eigenvector based approach to sentence ranking on this paper <https://pdfs.semanticscholar.org/ca73/bbc99be157074d8aad17ca8535e2cd956815.pdf>
  
  For source code Siraj Rawal has a Python notebook to create a set of word vectors. The word vectors can then be used to find the similarity between words. The source code is available here <https://github.com/llSourcell/word_vectors_game_of_thrones-LIVE>
  
  Another option is a tutorial from Oreily that utilizes the gensin Python library to determine the similarity between documents. This tutorial uses NLTK to tokenize then creates a tf-idf (term frequency-inverse document frequency) model from the corpus. The tf-idf is then used to determine the similarity of the documents. The tutorial is available here <https://www.oreilly.com/learning/how-do-i-compare-document-similarity-using-python>
  
  > **Comments:**
  > **username_0**: Thanks for providing valuable details for the problem. I had seen the example of gensim but I have a question will it be able to solve the problem that I mentioned in question. Although the solution I created is working fine in finding the similarity between sentences but it is getting stuck when order of words is jumbled.  
  
  ---
  
  