Research:Link recommendation model for add-a-link structured task

14:13, 22 October 2020 (UTC)
Djellel Difallah
Duration:  2020-September – ??

This page documents a research project in progress.
Information may be incomplete and change as the project progresses.
Please contact the project lead before formally citing or reusing results from this page.

As part of the work on structured tasks, the Growth team is aiming to implement the Add a link structured task. Here we describe the algorithm developed by the research team to automatically generate link-recommendations (see Add-a-link for the technical inrastructure). As a black box the algorithm works as follows:

  • Input: source-page/article = the article in which we want to add links
  • Output: list of recommended links with the following information
    • target-page/link = article which we want to link to
    • anchor/mention = text in source-page where link to target-page should be placed
    • link-likelihood = probability the model assigns to the link. Setting a threshold for the link-probability (thus only recommending links with a probability above, say, 0.8) allows us to make more accurate predictions, however, with the expense of missing potential useful links.

In the following we want to explain some of the details of the black box that generates the recommendations.

Problem statementEdit

Our goal is to make recommendations to add links to a given article. The recommendation should state: i) which article to link to, ii) where in the text should the link be added (using existing text without adding additional words).


First, we identify unlinked parts of the text which could potentially contain a link. For example, finding the plain text “Berlin”, we might recommend the link Berlin, the capital of Germany. However, we have to consider that this text could also refer to Berlin (Wisconsin), or the music band Berlin, etc. As a second step, we thus have to identify possible candidates and decide which (if any of them) to recommend as the article we would recommend to link.

Looking at links in existing articles, we use machine learning to identify the most likely candidates for articles to link to. The details are described below.

Anchor dictionaryEdit

The anchor dictionary records all links and their anchors across all articles in a given Wikipedia. This will serve as a look-up table to identify potential candidates for linking. From the wikitext-dump we identify all (anchor,link)-pairs from the signature using mwparserfromhell. As a result we obtain a dictionary of the form "{mention: {link: count} }".

We include the following pre-processing steps:

  • We normalize titles of the links (i.e. uppercasing the first character, see format of article-titles)
  • We resolve redirects of article-titles (i.e. if the link is a redirect-page we record the title of the redirected-to article)
  • We only keep links to main-namespace articles in the wiki under consideration
  • We only keep anchor/links in articles that are not redirects and that are in the main namespace
  • We normalize anchors by lowercasing the string
  • We only keep anchors with a link-probability > 6.5% (Milne&Witten 2008). The link-probability is the fraction of times the mention appears as a link (the mention can also appear as plain text without any links). This is intended to avoid mentions such as “and” in the anchor-dictionary, similar to filtering stopwords in NLP.

Gold datasetEdit

We generate a gold-dataset of fully-formed sentences in Wikipedia articles which already contain links. For this, we consider the first sentence in an article which contains at least one link. This choice is motivated mainly by the fact that later sentences might not contain links which had already occurred earlier in the article (false negatives). An example for a gold-sentence from the article Belgium in simple wikipedia would be “Belgium, officially the Kingdom of Belgium, is a federal state in Western Europe.”

Training dataEdit

In order to train a ML-model we build a training dataset containing features X and labels Y. The gold-dataset provides examples of which words are used as anchors for which links in which articles (and similarly which words are not used as anchors). Thus, we can construct a dataset of positive (Y=1) and negative (Y=0) examples. Consider the example above for the article “simple:Belgium”: “Belgium, officially the Kingdom of Belgium, is a federal state in Western Europe”. The positive examples (Y=1) can be obtained from the existing links, for example the anchor-link pair (“state”, “simple:Sovereign state”). The negative examples (Y=0) can be obtained by two different means. The first possibility are alternative links to existing anchor-link pairs; looking up the anchor-dictionary would yield the anchor-link pair (“state”, “simple:State”). The second possibility are potential anchors which are do not contain links such as “western europe”; looking up the anchor dictionary would yield the anchor-link pair (“western europe”, “simple:Western Europe”). For the gold-sentence of an article, we generate as many positive and negative anchor-link pairs and calculate the following features (X):

  • ngram: the number of words in the anchor (based on simple tokenization)
  • frequency: count of the anchor-link pair in the anchor-dictionary

ambiguity: how many different candidate links exist for an anchor in the anchor-dictionary

  • kurtosis: the kurtosis of the shape of the distribution of candidate-links for a given anchor in the anchor-dictionary
  • Levenshtein-distance: the Levensthein-distance between the anchor and the link. This measures how similar the two strings are. Roughly speaking, it corresponds to the number of single-character edits one has to make to transform one string into another, e.g. the Levensthein-distance between “kitten” and “sitting” is 3.
  • w2v-distance: similarity between the article (source-page) and the link (target-page) based on the content of the pages. This is obtained from wikipedia2vec. Similar to the concept of word-embeddings, we map each article as a vector in an (abstract) 50-dimensional space in which articles with similar content will be located close to each other. Thus given two articles (say the source article and a possible link) we can look up their vectors and get an estimate for their similarity by calculating the distance between them (more specifically, the cosine-similarity). The rationale is that a link might be more likely if the corresponding article is more similar to the source article.
  • nav-distance: similarity between the article (source-page) and the link (target-page) based on the navigation of readers. This is obtained from an approach similar to the Navigation vectors. Conceptually similar to the w2v-distance except that we construct the 50-dimensional vectors based on the sequence of articles from readers on Wikipedia. That is, two articles will be more similar if they are read together in the same reading sessions. This feature was removed (see below).

Thus, for each triple (article, anchor, link) we will calculate a set of features X and record the label Y=0,1 (depending on whether the anchor-link pair exist in the article.

Training the modelEdit

Given the training data consisting of features X and labels Y, we train a classifier that distinguishes between the positive and the negative examples in the training dataset. Specifically, we use xgboost with default parameters.

Generating recommendationsEdit

Generating link recommendations work in the following way once we trained the model:

  • Choose an article and get the wikitext
  • Identify an unlinked part of plain text and consider as a possible anchor
  • Identify link candidates by looking up the anchor in the anchor-dictionary
  • For the triple (article, anchor, link) calculate the features X
  • Using the features X, make a prediction for the probability P(Y=1|X) from the xgboost-model. This yields the probability that we should add the anchor-link pair in the given article
  • Rank all candidate-links for a possible anchor according to their probability
  • Return the top candidate if the probability exceeds a given threshold (say 0.5)

Evaluating the model (Backtesting)Edit

We evaluate the trained model using a held-out sample of the gold-dataset containing sentences with existing links taken from Wikipedia articles.

We generate link recommendations for the plain text of the sentence (removing all links). We then compare the predicted anchor-link pairs with the actual anchor-link pairs and calculate precision (how many of the recommended links are true, i.e. exist in the gold-data) and recall (how many of the true links are also recommended by the model). It is probably better to aim for a higher precision (making sure that the links we recommend are actually genuine) than for a high recall (making sure that we capture all existing links) in order to reduce the amount of false positives.

Hard-coded rules for (not) linkingEdit

We implement some hard-coded rules when to *not* add a link (anchor-text,link-title) to a source-page

  • The anchor is the same as the title of the source-page
  • The link already exists somewhere in the source-page. this implies "only recommend links on their first occurrence" since we scan the source-page iteratively from top to bottom to generate link recommendations. specifically, we check that
    • the anchor-text is not linked somewhere else in the text of the source-page
    • the link-title is not linked to in any of the other links in the text of the source-page
  • The page that we link to belongs to one of the following (such as disambiguation pages). In practice, we check the P31 (instance-of) property of the corresponding Wikidata-item (almost every Wikipedia article has a Wikidata item). Thus this list is, in principle, easily customizable if other link-types should be filtered.

We have also implemented implicit rules related to the anchor-text

  • The anchor-text is typically not used as a link across all other articles in the respective Wikipedia. More precisely, the anchor-text has to be used as a link in at least 6.5% of all of its occurrences (whether as a link or as a raw text without a link) across all articles in Wikipedia.
    • This prevents to add links to many of the unwanted cases generalizing the overall usage in the respective Wikipedia without explicitly encoding every single case. For example, it avoids adding links to years such as “640 AD” since, in most cases, it is not linked when looking across all articles (even though there might be few exising cases).


Second set of results (2020-12)Edit

We made some changes to the training of the link-recommendation model:

  • the extracted example-sentences are now assigned to training and testing data randomly (previous method had some potential bias)
  • the generation of candidate anchors from n-grams now works properly if the ngram contains non-alphabetic characters- One common case are places which contain commas, such as en:Latrobe,_Pennsylvania; not identifying those links often leads to false positives downstream (e.g. suggesting wrongly en:Pennsylvania.
  • removing the navigation-based features (obtained from reading sessions) from the model. We find that these features do not substantially contribute to the model performance in the backtesting data. Removing these features will make it easier to potentially share the model and the underlying the data publicly.

We also trained/evaluated the model on bnwiki instead of kowiki.

Precision and recall for different wikisEdit

We evaluate the link-recommendation model using precision and recall:

  • precision = How many of the recommended links did also exist in the evaluation data?
  • recall = How many of the links existing in the evaluation data did we recommend?

The threshold is a free parameter in the model which allows us to balance the tradeoff between precision and recall, i.e. increasing precision typically comes at the cost of recall. This parameter can in principle be chosen differently for each wiki in order to find an acceptable value for precision (making sure the recommendations are accurate) while ensuring that recall is not too small (making sure we make enough recommendations).

We see that for most wikis one can obtain a precision between 70-80% with recall at around 40% (or more).

Threshold (link-probability)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
simplewiki prec 0.55 0.59 0.64 0.68 0.73 0.79 0.84 0.88 0.92 0.95
recall 0.64 0.64 0.62 0.59 0.53 0.45 0.36 0.27 0.15 0.06
dewiki prec 0.63 0.67 0.7 0.73 0.76 0.8 0.84 0.88 0.91 0.95
recall 0.63 0.62 0.6 0.58 0.54 0.48 0.41 0.33 0.24 0.12
ptwiki prec 0.6 0.7 0.75 0.78 0.82 0.84 0.87 0.9 0.93 0.93
recall 0.58 0.62 0.62 0.6 0.57 0.53 0.48 0.41 0.31 0.2
arwiki prec 0.49 0.56 0.6 0.65 0.7 0.75 0.8 0.85 0.91 0.97
recall 0.45 0.45 0.44 0.42 0.39 0.34 0.28 0.21 0.14 0.07
bnwiki prec 0.57 0.6 0.63 0.67 0.7 0.75 0.8 0.85 0.91 0.95
recall 0.45 0.45 0.44 0.41 0.37 0.3 0.22 0.15 0.09 0.04
cswiki prec 0.6 0.64 0.67 0.7 0.74 0.78 0.82 0.86 0.91 0.96
recall 0.58 0.57 0.56 0.53 0.5 0.44 0.37 0.27 0.16 0.05
viwiki prec 0.64 0.73 0.78 0.82 0.86 0.89 0.92 0.96 0.99 0.99
recall 0.69 0.75 0.75 0.73 0.69 0.65 0.58 0.51 0.44 0.41

An anecdote on the usefulness of the backtesting-evaluation. In a first version, bnwiki showed considerably worse performance than the other languages. Investigating manually examples from the backtesting data revealed that our approach was not able to identify correctly fully formed sentences (with existing links). This is caused by the fact that Bengali language uses an alternative symbol for full stop, (daṛi). Once we account for this, performance was similar to the other wikis. More importantly, this anecdote shows that the backtesting-evaluation can indicate if our approach is not working for a specific language (even though it will take some detective work once we know that something is not working).

Feature importance for predictionEdit

We assess the importance of the different features described above for making the recommendations. The xgboost model allows us to calculate a feature-importance score based on gain, i.e. "The average training loss reduction gained when using a feature for splitting." (this is similar to the Gini importance measure used in scikit-learn tree models, see here for a good overview of different ways to calculate feature importance scores)

feature importance scores for the link recommendation model for different languages

The navigation-based features ("nav") have the lowest feature score across all wikis and the normalized value is close to 0. This suggests that the feature is not relevant for the predictions to generate link recommendations. In fact, running the model with and without this feature shows hardly any differences; the changes are on the order of 0.00-0.02 and are thus most likely not significant (not shown here).

Number of link recommendations for an article?Edit

We want to know how many link-recommendations are generated for a given article in a given language. The potential problem is that if we choose the threshold parameter too conservative (high precision but low recall), that only for few articles we will be able to generate link-recommendations.

Fraction of articles for which we generate at least 1 (blue), 5 (orange), or 10 (green) link recommendations in different wikis

Thus, for each language, we select 500 random articles (main namespace, no redirects), and count the number of generated link-recommendations for different choices of the threshold parameter. We show the fraction of articles (of the 500 randomly selected) for which the algorithm generates at least 1 (blue), 5 (orange), or 10 (green) link recommendations. For example, for arwiki, for threshold parameter 0.4 we can generate 5 or more link recommendations for approximately 10% of the articles. Extrapolating to the 1M articles in arwiki, we could guess that there are around 100k articles for which we can generate 5 or more link recommendations.

First set of results (2020-10)Edit

We ran an initial evaluation on 7 wikis (simple, de, pt, ar, cs, ko, vi). There are some variations in the performance but, overall, the model yields satisfactory results across the different languages. Aiming for a recall of around 0.5 (i.e. the link recommendation capture 50% of the true links), we get a precision between 70-80%. In some languages, the precision is even higher (e.g. vi). The reasons for these variations are not clear at the moment and requires further research.

Threshold (link-probability) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
simple prec 0.53 0.55 0.58 0.62 0.67 0.7 0.75 0.82 0.85 0.83
recall 0.67 0.66 0.64 0.61 0.55 0.48 0.38 0.27 0.17 0.07
de prec 0.62 0.67 0.69 0.72 0.75 0.79 0.83 0.87 0.91 0.94
recall 0.63 0.62 0.6 0.58 0.54 0.48 0.41 0.33 0.24 0.11
pt prec 0.57 0.67 0.72 0.79 0.82 0.85 0.87 0.9 0.94 0.99
recall 0.54 0.57 0.58 0.58 0.57 0.52 0.46 0.4 0.3 0.18
ar prec 0.56 0.61 0.67 0.72 0.77 0.81 0.86 0.9 0.94 0.98
recall 0.43 0.43 0.42 0.4 0.37 0.34 0.29 0.24 0.18 0.08
cs prec 0.58 0.62 0.64 0.67 0.71 0.74 0.78 0.84 0.88 0.94
recall 0.56 0.55 0.54 0.51 0.48 0.42 0.35 0.25 0.13 0.03
ko prec 0.49 0.61 0.64 0.67 0.69 0.72 0.75 0.79 0.83 0.87
recall 0.4 0.4 0.4 0.4 0.39 0.37 0.35 0.3 0.21 0.08
vi prec 0.74 0.79 0.83 0.86 0.88 0.91 0.94 0.96 0.99 0.996
recall 0.81 0.78 0.76 0.74 0.72 0.68 0.63 0.58 0.53 0.48