citeget.rank¶
Rank libgen search results against a requested title and author.
search() returns whatever libgen’s relevance ordering produced, which for a
known title is typically the same book repeated in several formats, interleaved
with derivative works (summaries, workbooks) and download-spam listings. This
module scores each result against what the caller actually asked for, so a
caller can take the best match rather than the first row.
Simple case:
from citeget import search
from citeget.rank import rank_results
results = search("Introduction to Algorithms Cormen")
best = rank_results(results, title="Introduction to Algorithms",
authors="Thomas H. Cormen")[0]
Everything about the scoring is overridable without editing this module: pass
weights, format_preference, language, or decoy_pattern.
Scoring uses only fields already present in a search() result dict, so
ranking costs no extra requests.
Two independent guards keep non-books from winning:
a decoy pattern over the title (
summary,workbook,gratis, …), catching derivative works and download-spam landing pages, anda stub check — a PDF under a few hundred kB is a landing page, not a scan. This floor is much higher than the general one, because a short EPUB of a real book is legitimately ~100 kB; one shared floor gets one of those two wrong.
Both are penalties rather than filters, so a decoy can still surface when it is genuinely the only thing available — it just never outranks a real match.
Based on the ranking contributed in https://github.com/thorwhalen/citeget/issues/7.
Functions
|
Score one |
|
Return |
|
Keep only the first result for each distinct work, preserving order. |
|
Identity of the work behind a result, ignoring which format it is in. |
|
Bytes from a libgen size string such as |
|
Normalized surnames usable for matching, in either name ordering. |
Classes
|
Relative contribution of each signal to the total score. |
|
Plausibility bounds for a real book file. |
|
A search result with its score and the signal breakdown behind it. |
- class citeget.rank.ScoreWeights(title=5.0, author=3.0, format=1.2, language=1.0, size=0.8, decoy_penalty=6.0, stub_penalty=3.0)[source]¶
Bases:
objectRelative contribution of each signal to the total score.
Title and author dominate: format and language only break ties between results that are already plausibly the right book.
- class citeget.rank.ScoredResult(result, score, title_match, author_match, is_decoy, is_stub)[source]¶
Bases:
objectA search result with its score and the signal breakdown behind it.
- class citeget.rank.SizeBounds(min_bytes=80000, min_pdf_bytes=300000, implausible_bytes=120000000)[source]¶
Bases:
objectPlausibility bounds for a real book file.
min_pdf_bytesis deliberately far abovemin_bytes: a landing-page “download” stub is typically a valid but nearly empty PDF, whereas a short epub of a genuine book can legitimately be ~100 kB.
- citeget.rank.candidate_surnames(authors)[source]¶
Normalized surnames usable for matching, in either name ordering.
Multi-word surnames contribute their last word, since that is what a caller-supplied author string is most likely to share.
>>> sorted(candidate_surnames("Alice Smith & Bob Jones")) ['jones', 'smith'] >>> sorted(candidate_surnames("Sander van der Linden")) ['linden']- Return type:
- citeget.rank.dedupe_key(result)[source]¶
Identity of the work behind a result, ignoring which format it is in.
Libgen lists one edition once per format, so
max_downloads=5otherwise means “five copies of one book”. Title, year and author surnames together identify an edition well enough to collapse those rows while keeping genuinely different editions apart.- Return type:
- citeget.rank.dedupe_results(results, *, key=<function dedupe_key>)[source]¶
Keep only the first result for each distinct work, preserving order.
Apply after ranking so the surviving copy of each work is the best-scoring one. Accepts either plain result dicts or
ScoredResultobjects and returns the same kind it was given.- Return type:
- citeget.rank.parse_size(size_text)[source]¶
Bytes from a libgen size string such as
'2 MB'; 0 if unparseable.>>> parse_size("2 MB") 2000000 >>> parse_size("587 kB") 587000 >>> parse_size("") 0- Return type:
- citeget.rank.rank_results(results, *, title, authors=None, **kwargs)[source]¶
Return
resultsasScoredResultobjects, best match first.kwargsare forwarded toscore_result()(format_preference,language,weights,size_bounds,decoy_pattern).- Return type:
- citeget.rank.score_result(result, *, title, authors=None, format_preference=('pdf', 'epub', 'azw3', 'mobi', 'azw', 'djvu', 'fb2', 'txt'), language='english', weights=ScoreWeights(title=5.0, author=3.0, format=1.2, language=1.0, size=0.8, decoy_penalty=6.0, stub_penalty=3.0), size_bounds=SizeBounds(min_bytes=80000, min_pdf_bytes=300000, implausible_bytes=120000000), decoy_pattern=re.compile('\\b(summary|summaries|workbook|study guide|sparknotes|cliffs?notes|key takeaways|conversation starters|review and analysis|analysis of|insights (?:on|from)|companion to|instaread|blinkist|downloaden|, re.IGNORECASE))[source]¶
Score one
search()result against a requested title and author.- Parameters:
result (
dict) – A result dict fromciteget.search().title (
str) – The title actually wanted.authors (
Optional[str]) – The author(s) wanted, in any of the orderingsciteget.namesunderstands. None skips author scoring.format_preference (
Sequence) – File formats, best first.language (
Optional[str]) – Preferred language; a result with no language set is treated as neutral rather than penalised, since libgen often omits it.weights (
ScoreWeights) – Per-signal weights.size_bounds (
SizeBounds) – What counts as a plausible file size.decoy_pattern (
Pattern) – Title pattern marking derivative or spam listings.
- Return type: