MicroSAS
EN

How I built a document summarizer that runs in the browser, without an LLM

An extractive summarizer picks sentences that are already in the document instead of writing new ones. Here is how mine works, step by step, with the actual numbers it uses, and where it falls short.

· José Antonio Conesa Blázquez

Try the summarizerPDF, Word, ODT, EPUB or plain text. Free, no sign-up, nothing is uploaded.

Why not just use an LLM?

When someone hands me a 40-page report, I want to know what it says before deciding whether to read it. The usual answer today is to paste it into a chatbot. That has two problems. The document goes to someone else's server, which is a bad idea for a contract or an internal report. And the summary is rewritten, so you can't tell whether a number was in the original or was made up.

Extractive summarization is older and much less clever, but it has a property I care about: every sentence in the summary is in the document, word for word. You can point at it. So the tool highlights each chosen sentence in the original text, can show the score of every sentence, and explains below each summary how it was made, using your document's numbers.

Everything below runs in plain JavaScript in the browser. The only libraries are pdf.js to read PDFs and pdf-lib to write the downloadable summary.

1. Getting clean text out of a PDF

This is the unglamorous half of the work. pdf.js gives you positioned text fragments, not paragraphs. Lines are rebuilt from their positions, then grouped into blocks: headings (by font size and weight), list items, paragraphs and, when the columns line up, tables.

Then comes the cleanup that makes or breaks the result:

Word, ODT, RTF and EPUB files go through a reader that turns them into HTML. Headings, lists, tables and images come from the markup, which is much easier than a PDF.

2. Splitting into sentences

A naive split on ". " breaks constantly in Spanish legal and administrative text: "Sr. García", "art. 37.2", "EE. UU.", "J. Pérez". The splitter only cuts after ., !, ? or … followed by a space and an uppercase letter or a digit. Then it rejects the cut if the word before the dot is a known abbreviation, a single capital letter (an initial), a dotted group like "EE.UU", or a short number in a short fragment (a numbered list). The abbreviation list covers Spanish, Catalan, Galician, Basque, Aranese and English.

3. From words to vectors

Each sentence becomes a sparse vector:

4. The similarity graph

Two sentences are similar when their vectors point the same way (cosine similarity). Comparing every pair is O(N²), so the graph is built from an inverted index: for each term, the sentences that contain it. Only pairs that share at least one term are compared. Terms that appear in too many sentences (more than 40, or 20 % of the document) are skipped, since they connect everything to everything and mean little. Edges weaker than 0.04 are dropped.

5. TextRank

TextRank is PageRank applied to sentences. Each sentence passes its score to the sentences it resembles, in proportion to how similar they are. A sentence ends up important if it is similar to many sentences that are themselves important, which usually means it states a central idea. The core loop is short:

let rank = new Float64Array(N).fill(1 / N);
for (let it = 0; it < 40; it++) {
  const next = new Float64Array(N).fill(0.15 / N);
  for (let i = 0; i < N; i++) {
    if (!out[i]) continue;                    // sum of i's edge weights
    const r = 0.85 * rank[i] / out[i];
    for (const [j, w] of adj[i]) next[j] += r * w;
  }
  rank = next;
}

The damping factor is the classic 0.85. Forty iterations is more than enough to converge on documents of any size I've tried.

6. Everything that isn't TextRank

TextRank alone picks some odd sentences, so the final score mixes four signals, each scaled from 0 to 1:

score = f × (0.45·TextRank + 0.20·topic + 0.20·position + 0.15·headings)

These weights were tuned by hand on real documents: official gazettes, reports, contracts and guides. They are not learned. That is a limitation, but it also means anyone can read them, and the tool shows them.

7. Choosing sentences without repeating ideas

Taking the top-scoring sentences gives you the same idea three times. Selection uses Maximal Marginal Relevance: at each step it takes the sentence with the best 0.75 × score − 0.25 × redundancy, where redundancy is the highest similarity to anything already chosen. A sentence more than 0.65 similar to a chosen one is skipped outright. It stops at 10, 20 or 30 % of the words. Once there are 3 sentences, nothing below 12 % of the best score is added, so a document that is mostly tables gets a shorter summary instead of a padded one.

The chosen sentences are then shown in their original order. The "key points" list is the same procedure with a fixed count, clamp(round(log2 N) − 1, 3, 7), shown in order of importance.

8. Keeping tables and images

A summary of a report that drops the one table everybody cares about is not much use. Tables and images are kept as separate blocks: their text never competes with sentences. A figure goes into the summary when:

Getting images out of a PDF means walking the page's operator list and tracking the transformation matrix, to know where each image is drawn and how big. Tiny images (icons, bullets) and page-sized ones (backgrounds, scans) are ignored. An image that appears on 3 or more pages is treated as a logo and skipped. Each image is placed before the first text block below it.

There is one trap I hit in testing. A Spanish official gazette that is essentially a 766-row table of local holidays left almost no prose once tables were set aside, so it was rejected as "too short". The fix: when that happens, the tables are read as text again, as before.

9. Performance

The inverted index keeps the graph sparse. A synthetic document of 123,840 words (5,220 sentences) is analyzed in about 2.9 seconds on my laptop, in the browser, with no server and no Web Worker. Ordinary documents take a fraction of a second; reading and rendering the PDF usually takes longer than summarizing it.

10. What it doesn't do

Those limits are the reason for the highlighting and the scores. A summary you can't check is a summary you have to trust, and I'd rather not ask for that.

Try it

The summarizer is free, needs no account and never uploads your file. It works in English, Spanish, Catalan, Galician, Basque and Aranese. If you find a document where the summary is bad, I'd genuinely like to hear about it on the contact page.

Open the document summarizer