Intro
This post is a practical look at generating summaries from text. At first I had little idea how it worked: a library can return results immediately, but then its behavior is difficult to understand or tune. “What I cannot create, I do not understand,” so I decided to investigate. My understanding is still introductory, so this is not an in-depth treatment.
Survey Of Text Summarization
There are two main approaches (this post focuses on extractive). First is abstractive, second is extractive. Extractive just pulls out the sentences from the original text that seem important (what “important” means is up to you). Abstractive is fancier — it actually generates new text using paraphrasing, substitution, compression, etc. Extractive is more mature, but it has its problems: some extracted sentences end up way longer than average, some sentences lose their meaning without surrounding context, and debate-style content (opposing viewpoints) doesn’t extract well either. Methods used in extractive text summarization include:
- TF-IDF
- Cluster Based Model
- Graph theoretic approach
- Machine Learning approach
- LSA Method
- An approach to concept-obtained text summarization
- Neural networks
- Automatic text summarization based on fuzzy logic
- Text summarization using regression for estimating
feature weights - Multi-document extractive summarization
- Query based extractive text summarization
- Multilingual Extractive Text summarization
Also PageRank and TextRank both fit in here.
NLP Basics
To establish some terminology: tokenization, tagging, training, keyword extraction, named-entity recognition, and text classification are all basic NLP tasks. Each has several implementation options; tokenization alone can use N-grams, CRF analysis, or custom dictionaries. Most libraries handle these details already. The HanLP README provides a useful overview. Below I focus on TF-IDF and N-grams.
TF-IDF
Term Frequency-Inverse Document Frequency is actually pretty intuitive. What’s “inverse document frequency”? Basically: words that show up less often get higher weight, words that show up everywhere get lower weight. You calculate term frequency, then inverse document frequency, then TF-IDF = TF * IDF, where IDF = log(total documents in corpus / documents containing the term + 1). In Python’s sklearn library it’s even simpler.
# https://stackoverflow.com/questions/34449127/sklearn-tfidf-transformer-how-to-get-tf-idf-values-of-given-words-in-documen |
N-GRAM
A picture is worth a thousand words; the example on the Wikipedia page makes the idea clear.
Word2vec, Doc2Vec, Sentence2Vec
Word Embedding
Here’s a good explanation from Zhihu:
Word embedding means finding a mapping or function that generates a representation in a new space — that representation is the word representation. In plain terms: you map words from space X into multi-dimensional vectors in space Y. Those vectors are “embedded” into space Y, one word per slot.
This is an important concept, way more than a sentence or two can cover — check the reference links for a proper deep dive.
to vec
- word2vec uses two methods: skipgram and cbow.

The difference is:
The skipgram model learns to predict a target word thanks to a nearby word. On the other hand, the cbow model predicts the target word according to its context.
- sentence2vec
def sentence2vec(sentences): |
- Also Doc2vec….
PageRank And TextRank
PageRank is well known; I first encountered it while writing crawlers. Applying it to text summarization was new to me.
Cosine similarity Or NN
Once words, sentences, or documents have been converted into vectors, cosine similarity measures the angle between them. While experimenting with fastText, I also found that KNN can be used to compute similarity.
PageRank And TextRank
Since I was doing Chinese text summarization, the TextRank library I used was TextRank4ZH. TextRank was just a quick test; the main approach was PageRank. Haven’t found the specific paper yet so I’ll skip the link for now. 
# coding: utf-8 |
In Action: FastText
FastText Basic Useage
Installation is simple: run make, and the CLI commands also have Python bindings.
- Train a model (supports skipgram or cbow, unsupervised learning, Word Representations)
fasttext skipgram -input traningText -ouput trainedModel |
import fasttext |
- Output vectors (word vectors or sentence vectors)
fasttext print-word-vectors trainedModel.bin < yourFile |
- Text classification (supervised learning)
fasttext supervised -input train.txt -output model #(train.txt is a text file containing a training sentence per line along with the labels.) |
Envy➜ data : master ✘ :✭ ᐅ head amazon_review_polarity.train |
FastText Pybinding
Avoid pip install fasttext: it requires Cython to be installed first with pip install Cython, and that package cannot load fastText trained model files (model.bin). Install from source instead by running pip install . inside the fastText directory; the official documentation covers this approach.
|
Rouge And Automatic Evaluation of Summaries
ROUGE is a tool for evaluating automatically generated summaries. I do not cover its evaluation methodology or internals here; a paper is linked below. It is written in Perl, which makes installation somewhat awkward, but it works reliably once configured.
cpan install XML::DOMexport ROUGE_EVAL_HOME=/usr/local/ROUGE-1.5.4/data
After installing, run the test file, then install the Python binding withpip install pyrouge. Write your generated summaries and the reference summaries into the specified files, then use the test code below.
# coding:utf-8 |
Other:
Show your GPU memory info
nvidia-smi -l 1outputs info every 1 second.gensimis useful.How to implement a paper’s project
References
- Text Summarization Techniques: A Brief Survey
- A Survey of Text Summarization Extractive Techniques
- 文本摘要自动生成综述
- fasttext
- fasttext python library document
- fasttext Word representations
- Bag of Tricks for Efficient Text Classification
- Enriching Word Vectors with Subword Information
- 抽取式文档摘要方法(一)
- 抽取式文档摘要方法(二)
- All Our N-gram are Belong to You
- Python自然语言处理
- 有谁可以解释下word embedding? 知乎
- Vector Representations of Words
- Get Sentences vec from word2vec
- ROUGE: A Package for Automatic Evaluation of Summaries