FindSimilarTerms
in package
Use case for finding similar terms.
Tags
Table of Contents
Properties
- $calculator : SimilarityCalculator
- $lemmatizer : LemmatizerInterface|null
- Lemmatizer used to place an unsaved term in a word family.
Methods
- __construct() : mixed
- Constructor.
- execute() : array<int, int>
- Find similar terms for a given language and term.
- formatTerm() : string
- Format a similar term for display.
- getFormattedTerms() : string
- Get formatted HTML for similar terms.
- getTableRow() : string
- Get HTML for similar terms table row.
- rankByCoverage() : array<int, int>
- Pick the terms that between them explain the most of the compared term.
- getLemmatizer() : LemmatizerInterface
- The lemmatizer, built on first use.
- resolveLemma() : string
- Work out which word family the searched term belongs to.
- sharesWordFamily() : bool
- Whether a candidate belongs to the searched term's word family.
Properties
$calculator
private
SimilarityCalculator
$calculator
$lemmatizer
Lemmatizer used to place an unsaved term in a word family.
private
LemmatizerInterface|null
$lemmatizer
Methods
__construct()
Constructor.
public
__construct([SimilarityCalculator|null $calculator = null ][, LemmatizerInterface|null $lemmatizer = null ]) : mixed
Parameters
- $calculator : SimilarityCalculator|null = null
-
Similarity calculator
- $lemmatizer : LemmatizerInterface|null = null
-
Lemmatizer for the searched term
execute()
Find similar terms for a given language and term.
public
execute(int $languageId, string $comparedTerm, int $maxCount, float $minRanking[, float $phoneticWeight = 0.3 ]) : array<int, int>
Parameters
- $languageId : int
-
Language ID
- $comparedTerm : string
-
Term to compare with
- $maxCount : int
-
Maximum number of terms to return
- $minRanking : float
-
Minimum similarity ranking (0-1)
- $phoneticWeight : float = 0.3
-
Weight for phonetic similarity (0-1)
Return values
array<int, int> —Word IDs, most useful first
formatTerm()
Format a similar term for display.
public
formatTerm(int $termId, string $compare) : string
Parameters
- $termId : int
-
Term ID
- $compare : string
-
Similar term to compare with
Return values
string —HTML-formatted string
getFormattedTerms()
Get formatted HTML for similar terms.
public
getFormattedTerms(int $languageId, string $comparedTerm) : string
Parameters
- $languageId : int
-
Language ID
- $comparedTerm : string
-
Term to compare with
Return values
string —HTML output
getTableRow()
Get HTML for similar terms table row.
public
getTableRow() : string
Return values
string —HTML output or empty string
rankByCoverage()
Pick the terms that between them explain the most of the compared term.
public
rankByCoverage(array<int, array{id: int, textLc: string, status: int, lemmaLc?: string}> $candidates, string $comparedTermLc, int $maxCount, float $minRanking[, float $phoneticWeight = 0.3 ][, string $lemmaLc = '' ]) : array<int, int>
Ranking each candidate against the whole term independently — what this used to do — makes a compound's siblings crowd out its parts: every word sharing "geschwindigkeit" scores on that shared half, so the term that would explain the other half never makes the list. So the picks are made one at a time, and after each one the term shrinks to the part still unexplained. A candidate that only repeats an earlier pick then scores near zero, and a short term covering fresh ground wins on merit.
The first pick is unchanged: with nothing covered yet, the score is the plain pairwise similarity. Admission to the pool uses that same pairwise score against the minimum ranking, so this changes which candidates are chosen and in what order, never which ones were eligible.
Terms of the same word family are the exception, and come first. Letter pairs cannot reach an irregular form — "bought" and "buy" share none at all, and no amount of tuning would have found them — so a shared lemma admits a candidate whatever it scores, and ranks it above the terms that merely look alike.
Parameters
- $candidates : array<int, array{id: int, textLc: string, status: int, lemmaLc?: string}>
-
Candidates
- $comparedTermLc : string
-
Lowercased term
- $maxCount : int
-
Maximum to return
- $minRanking : float
-
Minimum (0-1)
- $phoneticWeight : float = 0.3
-
Phonetic (0-1)
- $lemmaLc : string = ''
-
Term's lemma
Return values
array<int, int> —Word IDs, most useful first
getLemmatizer()
The lemmatizer, built on first use.
private
getLemmatizer() : LemmatizerInterface
Deliberately the dictionary one: this runs on every lookup, and the NLP lemmatizer would put a network round-trip in that path. Terms already in the vocabulary carry the lemma their configured lemmatizer produced when they were saved, so an install on spaCy still gets word families here — only a term that has never been saved falls back to this.
Return values
LemmatizerInterfaceresolveLemma()
Work out which word family the searched term belongs to.
private
resolveLemma(int $languageId, string $comparedTermLc) : string
Prefers the lemma already stored on the term, so whatever lemmatizer the language is configured with is the one that decides. Only a term that is not in the vocabulary yet — the common case when adding a word while reading — is looked up in the dictionary. A word the dictionary does not know is its own lemma, which is what makes searching a base form pull up its inflections.
Parameters
- $languageId : int
-
Language ID
- $comparedTermLc : string
-
Lowercased term
Return values
string —Lowercased lemma, or an empty string when unavailable
sharesWordFamily()
Whether a candidate belongs to the searched term's word family.
private
sharesWordFamily(array{id: int, textLc: string, status: int, lemmaLc?: string} $candidate, string $lemmaLc) : bool
Matches on the candidate's own lemma, and on the candidate being the lemma — a base form usually carries no lemma of its own.
Parameters
- $candidate : array{id: int, textLc: string, status: int, lemmaLc?: string}
-
Candidate
- $lemmaLc : string
-
Term's lemma