Text 0.5.0 rake extract - CyrilB1531/lodestar GitHub Wiki

Lodestar.Text 0.5.0. This page is frozen at that release. Read the current documentation for what main says now. A link to a decision or a migration page follows main, and leaves the archive.

Rake.Extract

Extracts the ranked candidates of one document.

public IReadOnlyList<KeywordMatch> Extract(string text)

Parameterstext is the document. A string is required, not a span: the candidates are runs of it, kept as new strings in the result, so nothing is saved by taking a span in.

ReturnsIReadOnlyList<KeywordMatch>, one entry per surviving candidate, sorted by descending score; a tie breaks by phrase, ordinal descending over UTF-16 code units — rake-nltk's own rule (rake_nltk/rake.py:241, (score, phrase) sorted with reverse=True over Python's code-point order), not text order. The two agree except when the deciding character is supplementary and the one it is compared against sits in U+E000U+FFFF, the only range a surrogate pair's leading unit sorts below. Empty when the document has none — every token was a stop word, or nothing survived RakeOptions.MinLength/MaxLength.

ExceptionsArgumentNullException when text is null.

Example — two two-word candidates tie for the top score; unlike TextRank.Extract, nothing here is glued back together, so a candidate is exactly the run RAKE found. The tie puts "natural numbers" first, ahead of "linear constraints" — reverse-alphabetical, not the order either phrase occurs in the source.

using Lodestar.Text.Keywords;

string[] stop = ["of", "the", "over", "a", "and", "are", "for", "all", "to", "in", "is", "this", "that"];
var rake = new Rake(new RakeOptions { StopWords = stop });

var hits = rake.Extract("Compatibility of systems of linear constraints over the set of natural numbers.");
int count = hits.Count;        // => 5
string top = hits[0].Phrase;      // => natural numbers
double topScore = hits[0].Score;  // => 4

Remarks — the default metric, RakeMetric.DegreeToFrequencyRatio, sums deg(w) / freq(w) over the run's words; RakeMetric has the other two. A one-word candidate's degree equals its frequency times one, so a longer run generally outranks a single repeated word — that is the whole mechanism, not a special case.

The document is scanned once: the co-occurrence tables are built from every surviving run before any candidate is scored, so a run RakeOptions.MinLength/MaxLength drops never contributes degree or frequency to what remains.

Applies to — net10.0, netstandard2.0.

See alsoRake, RakeOptions, RakeMetric, KeywordMatch, TextRank.Extract, the Python equivalence table.