I would like to compare two texts and try to find their degree of similarity and highlight the differences. I would like to do this, not only in terms of words, but also sentences/context, if it could be possible. I thought of using SequenceAlignment, but it is not a good fit in this case as there are many differences between the two texts.

txt1 = "Meghan Markle made a surprise appearance via Skype during \
Prince Harry's visit to Malawi earlier today. Prince Harry had been \
speaking at a college in Lilongwe in Malawi promoting girls' \
education. A grinning Meghan appeared via Skype from Johannesburg, \
South Africa, where she is staying with royal baby Archie Harrison."

txt2 = "Women from the Nalikule College of Education in Malawi were \
treated to a surprise appearance by Meghan Markle via Skype over the \
weekend, but no one was more excited than Prince Harry!
 During his outing on Sunday, Harry saw the impact of U.K. \
investments to ensure that girls obtain at least 12 years of quality \
education. The project is supported by the Queen\[CloseCurlyQuote]s \
Commonwealth Trust, of which Harry is president and Meghan is \
 The Duchess of Sussex, 38, remained in South Africa with the couple\
\[CloseCurlyQuote]s almost 5-month-old son Archie, but joined the \
important meeting with a Skype call."

Do you have any idea on how I could do? With Jaccard similarity will have a low score, however the 2 texts have very similar meanings. Here Jaccard similarity is neither able to capture semantic similarity nor lexical semantic of these two texts.


  • $\begingroup$ There are several ways to do this. Are you interested in solutions that use external text data (e.g. UK news), or you want to derive context only from those two texts? $\endgroup$ Commented Sep 30, 2019 at 20:58
  • $\begingroup$ I would like to derive the context from those two texts, but if it could be extended to other cases, it would be interesting. I do not know what you mean when you say external text data. Do you mean UK news vs, for example, tweets, stories...? $\endgroup$ Commented Sep 30, 2019 at 21:56
  • $\begingroup$ "Do you mean UK news vs, for example, tweets, stories...?" -- Yes, I meant that. (Not that "could be extended to other cases", although of course such extensions would be / should be easy.) $\endgroup$ Commented Oct 1, 2019 at 11:42

1 Answer 1


If we consider the applications of Bag-or-words and/or Linear vector space models for doing Natural language processing adequate -- meaning we ignore the sequential order of the words -- then we can apply this Latent Semantic Analysis (LSA) package: "MonadicLatentSemanticAnalysis.m" (LSAMon below.) See:

[1] "A monad for Latent Semantic Analysis workflows".

The second and third two approaches in this answer are documented in [1].

The out-of-the-box solution is the first approach, which uses the built-in (experimental) function FeatureExtraction. What happens under the hood in the first approach is explained and illustrated in the second and third approaches.

1. Using the two texts only (FeatureExtraction)

text = StringJoin[txt1, txt2]
sentences = TextSentences[text]

fed = FeatureExtraction[DeleteStopwords@sentences, "TFIDF", "FeatureDistance"]

fed[txt1, txt2]
(* 0.302942 *)

enter image description here

2. Using the two texts only (LSAMon)

  1. Join the texts and partition the result into sentences.

  2. For each sentence:

    • extract words,
    • delete stop words,
    • stem the rest of the words.
  3. Make a sentence-word contingency matrix.

  4. Apply LSI weighting functions to the contingency matrix.

  5. Extract topics.

  6. Represent each of the texts into the space of sentence-word matrix.

  7. Represent each of the texts into the space of sentence-topic matrix.

  8. Using the representations compute similarities between the texts with Cosine similarity. (Or with other metrics.)

The steps above are easily done with the software monad LSAMon.

The topics extraction is done in order to put the sentence contexts into the similarity computation. (As requested by OP.)

Selecting different LSI functions and number of topics produces different results. The application of the weight functions {"IDF", "None", "Cosine"} corresponds to "TFIDF" used in FeatureExtraction. (Although, I am not sure is the "Cosine" normalization applied or not.)

Here are illustrating screenshots:

enter image description here

enter image description here

3. Using an external text corpus

In the sequence of steps above we can use an external, large text corpus to make a document-term matrix and to derive a document-topics matrix. The two texts are represented in the topic space of that matrix.

Here is a screenshot using USA State of Union speeches (because I have it handy.) Better, more adequate text corpus can be used. (E.g. contemporary UK news.)

enter image description here

enter image description here

Note that OP's texts seem to be adequately represented by the topics extracted from the State of Union speeches.

4. Using Neural Net encoding

Similar to the previous approach that uses an external corpus we can use some of Mathematica's Neural net encoders of texts and compute similarities based on those representations.

  • 1
    $\begingroup$ Great answer, well done! $\endgroup$
    – Carl Lange
    Commented Oct 1, 2019 at 12:26
  • $\begingroup$ @CarlLange Thanks! Ideally, using the net encoders should be the easiest out of the box solution for you. $\endgroup$ Commented Oct 1, 2019 at 12:31
  • $\begingroup$ Thank you so much, Anton Antonov. I learned a lot from your answer as well. I am trying to use the net encoder as well, but I need to better understand how it works $\endgroup$ Commented Oct 1, 2019 at 23:52
  • 1
    $\begingroup$ @Matt Take a look also at FeatureExtraction and FeatureDistance, I just updated my answer using that function. $\endgroup$ Commented Oct 2, 2019 at 2:12
  • $\begingroup$ Thanks Anton Antonov. $\endgroup$ Commented Oct 2, 2019 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.