Wednesday, September 24, 2014

Text summarization with NLTK

The target of the automatic text summarization is to reduce a textual document to a summary that retains the pivotal points of the original document. The research about text summarization is very active and during the last years many summarization algorithms have been proposed.
In this post we will see how to implement a simple text summarizer using the NLTK library (which we also used in a previous post) and how to apply it to some articles extracted from the BBC news feed. The algorithm that we are going to see tries to extract one or more sentences that cover the main topics of the original document using the idea that, if a sentences contains the most recurrent words in the text, it probably covers most of the topics of the text. Here's the Python class that implements the algorithm:
from nltk.tokenize import sent_tokenize,word_tokenize
from nltk.corpus import stopwords
from collections import defaultdict
from string import punctuation
from heapq import nlargest

class FrequencySummarizer:
def __init__(self, min_cut=0.1, max_cut=0.9):
"""
Initilize the text summarizer.
Words that have a frequency term lower than min_cut
or higer than max_cut will be ignored.
"""
self._min_cut = min_cut
self._max_cut = max_cut
self._stopwords = set(stopwords.words('english') + list(punctuation))

def _compute_frequencies(self, word_sent):
"""
Compute the frequency of each of word.
Input:
word_sent, a list of sentences already tokenized.
Output:
freq, a dictionary where freq[w] is the frequency of w.
"""
freq = defaultdict(int)
for s in word_sent:
for word in s:
if word not in self._stopwords:
freq[word] += 1
# frequencies normalization and fitering
m = float(max(freq.values()))
for w in freq.keys():
freq[w] = freq[w]/m
if freq[w] >= self._max_cut or freq[w] <= self._min_cut:
del freq[w]
return freq

def summarize(self, text, n):
"""
Return a list of n sentences
which represent the summary of text.
"""
sents = sent_tokenize(text)
assert n <= len(sents)
word_sent = [word_tokenize(s.lower()) for s in sents]
self._freq = self._compute_frequencies(word_sent)
ranking = defaultdict(int)
for i,sent in enumerate(word_sent):
for w in sent:
if w in self._freq:
ranking[i] += self._freq[w]
sents_idx = self._rank(ranking, n)
return [sents[j] for j in sents_idx]

def _rank(self, ranking, n):
""" return the first n sentences with highest ranking """
return nlargest(n, ranking, key=ranking.get)

The FrequencySummarizer tokenizes the input into sentences then computes the term frequency map of the words. Then, the frequency map is filtered in order to ignore very low frequency and highly frequent words, this way it is able to discard the noisy words such as determiners, that are very frequent but don't contain much information, or words that occur only few times. And finally, the sentences are ranked according to the frequency of the words they contain and the top sentences are selected for the final summary.

To test the summarizer, let's create a function that extract the natural language from a html page using BeautifulSoup:
import urllib2
from bs4 import BeautifulSoup

def get_only_text(url):
"""
return the title and the text of the article
at the specified url
"""
soup = BeautifulSoup(page)
text = ' '.join(map(lambda p: p.text, soup.find_all('p')))
return soup.title.text, text

We can finally apply our summarizer on a set of articles extracted from the BBC news feed:
feed_xml = urllib2.urlopen('http://feeds.bbci.co.uk/news/rss.xml').read()
feed = BeautifulSoup(feed_xml.decode('utf8'))
to_summarize = map(lambda p: p.text, feed.find_all('guid'))

fs = FrequencySummarizer()
for article_url in to_summarize[:5]:
title, text = get_only_text(article_url)
print '----------------------------------'
print title
for s in fs.summarize(text, 2):
print '*',s

And here are the results:
----------------------------------
BBC News - Scottish independence: Campaigns seize on Scotland powers pledge
* Speaking ahead of a visit to apprentices at an engineering firm in
Renfrew, Deputy First Minister Nicola Sturgeon said: Only a 'Yes' vote will
ensure we have full powers over job creation - enabling us to create more
and better jobs across the country.
* Asked if the move smacks of panic, Mr Alexander told BBC Breakfast:
I don't think there's any embarrassment about placing policies on the
front page of papers with just days two go.
----------------------------------
BBC News - US air strike supports Iraqi troops under attack
* Gabriel Gatehouse reports from the front line of Peshmerga-held territory
in northern Iraq The air strike south-west of Baghdad was the first taken as
part of our expanded efforts beyond protecting our own people and humanitarian
missions to hit Isil targets as Iraqi forces go on offence, as outlined in the
president's speech last Wednesday, US Central Command said.
* But Iran's Supreme Leader Ayatollah Ali Khamenei said on Monday that the US
had requested Iran's co-operation via the US ambassador to Iraq.
----------------------------------
BBC News - Passport delay victims deserve refund, say MPs
* British adult passport costs Normal service - £72.50 Check  Send -
Post Office staff check application correct and it is sent by Special Delivery
- £81.25 Fast-Track - Applicant attends Passport Office in person and passport
delivered within one week - £103 Premium - Passport available for collection
on same day applicant attends Passport Office - £128 In mid-June it announced
that - for people who could prove they were booked to travel within seven days
and had submitted passport applications more than three weeks earlier - there
would be a free upgrade to its fast-track service.
* The Passport Office has since cut the number of outstanding applications to
around 90,000, but the report said: A number of people have ended up
out-of-pocket due to HMPO's inability to meet its service standard.
----------------------------------
BBC News - UK inflation rate falls to 1.5%
* Howard Archer, chief UK and European economist at IHS Global Insight,
said: August's muted consumer price inflation is welcome news for consumers'
purchasing power as they currently continue to be hampered by very
low earnings growth.
* Consumer Price Index (CPI) inflation fell to 1.5% from 1.6% in August,
the Office for National Statistics said.
----------------------------------
BBC News - Thailand deaths: Police have 'number of suspects'
* The BBC's Jonathan Head, on Koh Tao, says police are focussing on the
island's Burmese community BBC south-east Asia correspondent Jonathan Head
said the police's focus on Burmese migrants would be quite controversial as
Burmese people were often scapegoated for crimes in Thailand.
* By Jonathan Head, BBC south-east Asia correspondent The shocking death of
the two young tourists has cast a pall over this scenic island resort Locals
say they can remember nothing like it happening before.

Of course, the evaluation a text summarizer is not an easy task. But, from the results above we note that the summarizer often picked quoted text reported in the original article and that the sentences picked by the summarizer often represent decent insights if we consider the title of the article.

1. Hello, this is an amazing program, can you tell me on which algorithm this is based on ? or is it your own ?
why did you choose min_cut =0.1 , and max_cut=0.9 , i mean to say that, why can't 0.2 be the lower cut off to declare that the word is not required ?

1. I'm pretty sure that you will very similar algorithms also in literature. min_cut and max_cut are parameters that should be tuned according to the data you have. 0.1 and 0.9 were just a guess.

2. Oh so min and max cut values u have provided is not ideal right ? I shall try and modify and see the results.

do you know any way apart from using domain experts to manually evaluate , how good the summary is ? , like some automation technique ?

3. Yes, the values may be not the best choice.

You really should look at the literature about text summarization evaluation, you could start from here:

http://www.cai.sk/ojs/index.php/cai/article/viewFile/37/24

2. Hey! great program. I have a small doubt though, the sentences are displayed in rank order, can they be displayed in order of occurrence?

1. I don't think it makes sense because I expect to have a very low number of duplicated sentences in a text.

3. Are you aware of any summarisation algorithm for online reviews? It would be different from news summaries as one needs to first identify the product features mentioned, then the sentiment associated with that feature.

1. You're looking for a cross document summary. You could use this algorithm giving it all the reviews in input. Of course, it is very naive, but the high frequency terms should be given by the product features mentioned by the reviewers.

4. It doesn't work with many wikipedia dump files like when we download wikipedia pages to a file and give that as input to the summarizer.

1. Yes, the parser proposed in the example is specific for the bbc website.

2. Why so? How can we make this code work for any link?

3. Yes you can. Just parse the html document and extract the body portion using BS4. Then feed that string to the FS object.

5. are the three blocks of code u provided suppose to be given in the same file?

1. Yes, I made them in the same file.

6. This comment has been removed by the author.

1. Hi nishita, python uses indentation to specify the code blocks (loops, functions, ifs). If you don't indent the code it won't work.

7. I keep gtng syntax error specifying the argument passed to get_only _text. I tried changing d url der and also in bs4.py. Bt seems like m missing out something

8. I’d like to this post.

Thanks
summarize online

9. Hey why do you say that it works only for BBC? If we feed it with a different url,say,a wikipedia page, wouldn't it extract details the same way it did for BBC?

1. The function was tested on the bbc website at the time I wrote the snippet. It may also work on other websites.

2. Oh okay thanks a lot I'll definitely try playing around with the code. And also I'd love it if you direct me to some papers or books that helped you write the code. I'm just a beginner and I want to explore more.

3. Another user asked a similar question on this post. Look at the reply he got.

10. Research papers are produced now in such mass quantity, and often universities and researchers are doing the same types of studies getting similar results, and each of their papers is published in a separate or different journal. online text summarizer

11. I think this post is really worth a try. I like to thank you for sharing this information. One of this days I will use your tips and see if I come up with much better result if not close to what you have been doing right now.

12. Thanks - this code is great. Only one strange thing for me - if I try the code as it is, I get an error:

in _compute_frequencies
for w in freq.keys():
RuntimeError: dictionary changed size during iteration

I'm new to Python, but I understand the error - the code is trying to modify the thing that control the iteration during the loop. Why don't others have this problem? If I take out that 'del freq[w]' it works, but obviosuly without the 'cut' functionality. Any ideas? I'm using Python3.
Thanks again for the code, though.

1. Hi, you copy freq.keys() into a separate list and use it for the iteration.

2. Thanks - great. Works perfectly.

3. you can use 'for w in list(freq)'. It works in Python3.

13. Hi, have you tried with other languages? Thanks.

1. Sorry to not specify more. I meant Spanish or Portuguese etc. Non English text.

2. No, I didn't. It would be interesting to do so.

14. when I try this code. error occurs- Frequency Summarizer instance has no attribute 'summarize' . can you resolve it.

15. hello, can you tell me on which algorithm this is based on ? is it Term Frequency algorithm ?

1. Hi Firda, if you look through the comments there are some references.

Term frequencies are a core part of the algorithm.

2. Thank you, so this core algorithm is term frequencies not TF-IDF algorithm ? sorry i just
rather confused. i ever heard there are some algorithm like sentences clustering, lexical chain or TF-IDF algorithm. is the algorithm you made not both of them?

3. This agorithm just uses frequencies.

4. this is an amazing program, but is it really important to determine the parameters? You choose the min_cut =0.1 , and max_cut=0.9. if the parameter checking was not used in the program, is it wrong? or make a problem for the summarization?

5. Hello again Friday, these parameters simply filter words with too high or too low frequencies. If you think that they're not a problem for your input data, you could simply set them to 0. It really depends on the language you're dealing with and if you applied some other types of normalization before using this algorithm.

6. thank you, but why you choose 0.9 as max_cut parameters? is there a specific reason?

7. It was just a guess based on the outcomes that I observed. In theory, you should tune them using a proper validation methodology (like cross validation for example).

8. hai, sorry for ask again :D why you not choose 1.0 as your max_cut parameters but 0.9?

9. This comment has been removed by the author.

16. This comment has been removed by the author.

17. This comment has been removed by the author.

18. when not using the list() before the map(), the following error is what i get... pls help me out

Traceback (most recent call last):
File "C:\Users\MAA\Desktop\text.py", line 77, in
for article_url in to_summarize[:5]:
TypeError: 'map' object is not subscriptable

19. I am using your program to summarize a set of disaster responses.
It is repeating same sentence multiple time and I am asking for 2 line summary,it is giving me more that two lines.What is the reason behind it?

20. Good work. The same algo is used by many online summary tools. I found the results similar. But there are better algorithms available. Full marks to you for implementing this algo and sharing with people.

21. This comment has been removed by the author.

22. how to link three blocks like all in the same file or different and then link them? And can we do it with .csv or .json files?

23. Nice post. i am getting the below error, any idea what it might be??
TypeError: 'map' object is not subscriptable

1. Hi Ganesh, you can switch to Python 2 or give a look at this: http://stackoverflow.com/questions/6800481/python-map-object-is-not-subscriptable

24. Hey I was trying to run the code but it is showing various types of errors even after installing all the required modules .

25. Traceback (most recent call last):
File "check3.py", line 1, in