Introduction to SELMR

Posted on Fri 23 August 2024 in Selmr

Introduction to Selmr

Selmr is a library for generating and using simple multiset embeddings that work like language models. The multiset embeddings do not use real-valued vectors; instead they use multisets and are derived directly from plain text data.

Multiset embeddings, called Simple Explainable Language Multiset Representations (SELMRs), consist of multisets created from all word and context combinations contained in a collection of documents given some contraints. They can be used for downstream NLP tasks like text classifications and searching, in a similar manner as real-valued vector embeddings.

SELMRs produce explainable results without any randomness and enable explicit links with lexical, linguistical and terminological annotations. No model is trained and no dimensionality reduction is applied.

Selmr is published as a Rust crate and a Python package. Below the usage is described in Python code.

Simple model based on DBpedia pages

The model used below is based on the plain text of 10.000 DBpedia pages (217 Mb), with phrase length of one to three words, and left and right context length of also one to three words. It and can be downloaded from here:

Read the file with:

import selmr
s = selmr.PySELMR()
s.read("E:\\data\\selmr\\dbpedia_1_10_p=3_lc=3_rc=3_lang=en.zip", format="zip")

Example: (multi-)word similarities

Take for example the multi-word "has been suggested". The multiset embedding based on the plain text data of 10.000 DBpedia pages returns the following top ten most similar multi-words

s.most_similar("has been suggested", topn=10)

This gives:

[('has been suggested', 25.0),
 ('has been argued', 13.0),
 ('has been speculated', 12.0),
 ('is argued', 10.0),
 ('is clear', 9.0),
 ('is speculated', 9.0),
 ('is more likely', 9.0),
 ('was clear', 9.0),
 ('is probable', 9.0),
 ('is generally accepted', 9.0)]

This shows the multi-words with the number of contexts that they have in common with "has been suggested" (see below). So "is probable" has 11 contexts in common with "has been suggested". The more contexts that a multi-word has in common the more "similar" it is. Note that this similarity measure is only based on patterns in text; it is not a syntactical or semantic similarity measure.

An example with the name "Aldous Huxley" which returns the following.

s.most_similar("Aldous Huxley", multiset_topn=25, topn=10)

This gives:

[('Aldous Huxley', 25.0),
 ('Anatole France', 7.0),
 ('Ben Bova', 7.0),
 ('Blaise Pascal', 7.0),
 ('Abner Doubleday', 7.0),
 ('Henri Bergson', 7.0),
 ('Hermann Hesse', 7.0),
 ('Henryk Sienkiewicz', 7.0),
 ('A A Milne', 7.0),
 ('A E Housman', 7.0)]

The results are based on the common contexts with "Aldous Huxley". For example the coinciding contexts of "Aldous Huxley" and "Ben Bova" can be found with:

set(dict(s.get_multiset('Aldous Huxley', topn=35)).keys()).intersection(set(dict(s.get_multiset('Ben Bova', topn=35)).keys()))

This gives

{'Works by ... at LibriVox',
 'Works by ... at Open',
 'Works by ... at Project',
 'about ... at',
 'article ... bibliography',
 'by ... at',
 'by ... at LibriVox',
 'by ... at Open',
 'by ... at Project'}

So in this case similarities are found because DBpedia contains a bibliography of their work and (some of) their works are available on LibriVox, Open (Library) and Project (Gutenberg).

Taking into account contexts

Some words have multiple meanings. The different usages of a word can, to some extent, be derived from the contexts in which they are used. Take for example the word "deal" which is used as a noun and as a verb. By adding the context to the function call we can restrict the results to similar words that occur in the input context. We can restrict the output by providing the context "a ... with":

s.most_similar("deal", "a ... with", topn=10)

This gives:

[('deal', 25.0),
 ('contract', 7.0),
 ('treaty', 6.0),
 ('dispute', 5.0),
 ('problem', 4.0),
 ('partnership', 4.0),
 ('peace treaty', 4.0),
 ('close relationship', 3.0),
 ('joint venture', 3.0),
 ('collaboration', 3.0)]

Compare these results to the results when the context is "to ... with":

s.most_similar("deal", "to ... with", topn=10)

This gives

[('deal', 25.0),
 ('come up', 5.0),
 ('interact', 5.0),
 ('compete', 4.0),
 ('communicate', 4.0),
 ('coincide', 4.0),
 ('cooperate', 4.0),
 ('experiment', 4.0),
 ('cope', 4.0),
 ('interfere', 4.0)]

Lemmatized words

In the examples above the contexts are based on the exact word. In many situations it is desirable to use lemmatized words. Selmr implements a simple tokenizer to separate a word into a lemma and a suffix. A new entry is created for the lemma and the suffices are added to the contexts of that lemma. This means that the multiset of the lemma "suggest-" combines the multisets of "suggest", "suggests", "suggested" and "suggesting". The resulting multiset can be used to find similar words.

s.most_similar("suggest-", topn=10)

This gives:

[('suggest-', 25.0),
 ('speculate-', 16.0),
 ('argue-', 16.0),
 ('note-', 11.0),
 ('claim-', 10.0),
 ('indicate-', 10.0),
 ('state-', 9.0),
 ('propose-', 9.0),
 ('assert-', 9.0),
 ('theorize-', 9.0)]

How does it work

Multiset representations

A multiset is a modification of a set that allows for multiple instances for each of its elements. It contains elements with their number of instances, called multiplicities. Instead of real-valued vectors, multisets contain a variable number of elements. The corresponding multiset embedding is not an fixed length array of numbers but a dictionary with elements as keys and multiplicities as values.

This concept can be used to represent words in a corpus: by creating a multiset for each word that contains all contexts in which the word occurs with their number of instances / occurrences (the multiplicity), and by creating a multiset of each context that contains all words that fit into that context with the number of instances / occurrences.

A context of a word is a combination (a tuple) of the preceding list of words (the left side) and the following list of words (the right side) of that word, with a certain maximum length and a certain minimum number of unique occurrences.

The most similar multi-words are found by looking at the number of common contexts of an input multi-word and another mult-word in the corpus. The multi-words that have the highest number of contexts in common are the most "similar" of the input multi-word.

Similarity measures

The measure can be a simple count, the Jaccard index (based on the items in the multiset, but not their multiplicities), and the Weighted Jaccard index (based on the items and their multiplicities):

  • count: a simple count of the common items in the multisets of \(e_1\) and \(e_2\):
$$ \operatorname{count}(e_1, e_2) = | \operatorname{S}(e_1) \cap \operatorname{S}(e_2)| $$

where \(\operatorname{S}(e)\) is the set of \(e\),

  • Jaccard index. This measure is similar to a simple count, but calculates a similarity score in the range of [0, 1]. It is calculated by the intersection of contexts divided by the union of contexts of two phrases
$$ \operatorname{jaccard}(e_1, e_2) = \frac{\operatorname{S}(e_1) \cap \operatorname{S}(e_2)}{\operatorname{S}(e_1) \cup \operatorname{S}(e_2)} $$
  • Weighted Jaccard index. This measure takes into account the number of occurrences:
$$ \operatorname{weighted\_jaccard}(p1, p2) = \frac{\displaystyle\sum min(m_{\operatorname{C}(p1)}(c), m_{\operatorname{C}(p2)}(c))}{\displaystyle\sum max(m_{\operatorname{C}(p1)}(c), m_{\operatorname{C}(p2)}(c))} $$

where \(m_{C}(c)\) is the multiplicity (the number of occurrences) of \(c\) in multiset \(C\).

The most similar words are calculated accordingly. For example to find the most similar words, the Jaccard distance of the contexts of the input multi-words and the contexts of each multi-word in the corpus is calculated. The most similar multi-words of input multi-word \(p\) are calculated with

$$ \displaystyle\max_{k \in \mathrm{\mathbf{P}}} 1 - \operatorname{jaccard}(k, p) $$

where - \(\mathrm{\mathbf{P}}\) is the set of phrases that fits one of the contexts of multi-word \(p\)

If an input context is also given then we restrict the phrases by replacing \(\mathrm{\mathbf{P}}\) with \(\operatorname{Phrases}(c)\), i.e. the phrases that fit into context \(c\):

$$ \displaystyle\max_{k \in \operatorname{Phrases}(c)} 1 - \operatorname{jaccard}(k, p) $$

where - \(p\) is the input phrase and \(c\) is the input context, - \(\operatorname{Phrases}(c)\) is the set of phrases that fit into context \(c\).

Note that the most_similar function has an argument 'multiset_topn' to specify the number of most common contexts that has to be used when calculating the most similar multi-words. Taking into account only a limited number of contexts yields better results. Similarly, if a context is specified then the argument specifies the number of most common multi-words that are used.