TextRank¤
TextRank
pydantic-model
¤
Bases: TopWords
Extracts keyterms using the TextRank algorithm.
Config:
default:validation_config
Fields:
-
doc(str | Doc) -
normalize(Optional[Literal['orth', 'lower', 'lemma']]) -
include_pos(Optional[str | Collection[str]]) -
stopwords(Optional[str | Collection[str]]) -
ngrams(Optional[int | Iterable[int]]) -
window_size(Optional[int]) -
edge_weighting(Optional[str]) -
position_bias(Optional[bool]) -
candidate_weighting(Optional[Literal['unique', 'frequency']]) -
topn(Optional[int | float]) -
keyterms(list[tuple[str, float]] | None)
Source code in lexos/topwords/keyterms/textrank.py
candidate_weighting: Optional[Literal['unique', 'frequency']] = 'unique'
pydantic-field
¤
How to weight candidates based on their frequency or uniqueness.
doc: str | Doc
pydantic-field
¤
The raw text or spaCy doc to analyze.
edge_weighting: Optional[str] = 'binary'
pydantic-field
¤
How to weight edges in the graph: "binary" or "count".
include_pos: Optional[str | Collection[str]] = ('NOUN', 'PROPN', 'ADJ')
pydantic-field
¤
POS tags to include for candidate selection.
ngrams: Optional[int | Iterable[int]] = 1
pydantic-field
¤
The ngram range for candidate selection, e.g., 1 for unigrams, (1, 2) for unigrams and bigrams.
normalize: Optional[Literal['orth', 'lower', 'lemma']] = None
pydantic-field
¤
How to normalize tokens for candidate selection.
position_bias: Optional[bool] = False
pydantic-field
¤
Whether to bias towards candidates appearing earlier in the text.
stopwords: Optional[str | Collection[str]] = None
pydantic-field
¤
Custom stopwords to exclude from candidates.
topn: Optional[int | float] = 10
pydantic-field
¤
The number of top keyterms to return (int or float ratio of candidates).
window_size: Optional[int] = 2
pydantic-field
¤
The size of the sliding window for co-occurrence.
__init__(**kwargs) -> None
¤
Initialize the TextRank object and extract keyterms.
Source code in lexos/topwords/keyterms/textrank.py
to_df()
¤
to_dict()
¤
textrank(doc: Doc | str, *, normalize: Literal['orth', 'lower', 'lemma'] | None = None, include_pos: Optional[str | Collection[str]] = ('NOUN', 'PROPN', 'ADJ'), stopwords: Optional[str | Collection[str]] = None, ngrams: int | Iterable[int] | None = 1, window_size: int = 2, edge_weighting: str = 'binary', position_bias: bool = False, candidate_weighting: Literal['unique', 'frequency'] = 'unique', topn: int | float = 10) -> list[tuple[str, float]]
¤
Extract key terms from a document using the TextRank algorithm, or a variation thereof.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Doc | str
|
spaCy |
required |
normalize
|
Literal['orth', 'lower', 'lemma'] | None
|
If "lemma", lemmatize
terms; if "lower", lowercase terms; if None, use the orthographic forms
that appear in |
None
|
include_pos
|
str | Collection[str] | None
|
One or more POS tags with which
to filter for good candidate keyterms. If |
('NOUN', 'PROPN', 'ADJ')
|
stopwords
|
str | Collection[str] | None
|
One or more stopwords to filter out.
When provided for spaCy |
None
|
ngrams
|
int | Iterable[int] | None
|
Candidate n-gram lengths to
extract (for example, |
1
|
window_size
|
int
|
Size of sliding window in which term co-occurrences are determined. |
2
|
edge_weighting
|
str
|
If "count", the nodes for all co-occurring terms are connected by edges with weight equal to the number of times they co-occurred within a sliding window; if "binary", all such edges have weight = 1. |
'binary'
|
position_bias
|
bool
|
If True, bias the PageRank algorithm for weighting
nodes in the word graph, such that words appearing earlier and more
frequently in |
False
|
candidate_weighting
|
Literal['unique', 'frequency']
|
Weighting mode for candidate phrase scoring. If "unique", score each unique candidate phrase once (Textacy behaviour). If "frequency", multiply each candidate phrase's score by its observed frequency in the document. |
'unique'
|
topn
|
int | float
|
Number of top-ranked terms to return as key terms.
If an integer, represents the absolute number; if a float, value
must be in the interval (0.0, 1.0], which is converted to an int by
|
10
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, float]]
|
list[tuple[str, float]]: Sorted list of top |
Notes
Example parameter settings for different TextRank variations:
- TextRank:
window_size=2, edge_weighting="binary", position_bias=False - SingleRank:
window_size=10, edge_weighting="count", position_bias=False - PositionRank:
window_size=10, edge_weighting="count", position_bias=True
Source code in lexos/topwords/keyterms/textrank.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |