In the blog system's article list, in order to more effectively present the article content, so that readers can select to read more specifically, the article title and abstract are usually provided at the same time. In the blog system's article list, in order to more effectively present the article content, so that readers can select to read more specifically, the article title and abstract are usually provided at the same time.
The content of an article can be in plain text format, but in today's popular network, it is mostly in HTML format. Regardless of the format, the abstract is generally the content at the beginning of the article, which can be extracted according to the specified number of words.
Plain text summarization
First, we extract the plain text Abstract. the plain text document is a long string and can easily extract the abstract:
#! /Usr/bin/env python #-*-coding: UTF-8-*-"Get a summary of the TEXT-format document" "def get_summary (text, count ): u "Get the first 'count' characters from 'text' >>> text = u'welcome this is an article about Python '>>> get_summary (text, 12) = u'welcome this is an article 'True "assert (isinstance (text, unicode) return text [0: count] if _ name _ = '_ main _': import doctestdoctest. testmod ()
HTML abstract
HTML documents contain a large number of tags (for example,
And so on), these characters are Mark commands, and usually appear in pairs, simple text truncation will destroy the HTML document structure, and thus lead to improper display of the abstract in the browser.
While following the HTML document structure, you must parse the HTML document and intercept the content. In Python, you can use the standard library HTMLParser.
The simplest abstract extraction function is to extract only native text inside the tag while ignoring the HTML tag. Python implementation similar to this function is as follows:
#!/usr/bin/env python# -*- coding: utf-8 -*-"""Get a raw summary of the HTML-format document"""from HTMLParser import HTMLParserclass SummaryHTMLParser(HTMLParser):"""Parse HTML text to get a summary>>> text = u'Hi guys:
This is a example using SummaryHTMLParser.
'>>> parser = SummaryHTMLParser(10)>>> parser.feed(text)>>> parser.get_summary(u'...')u'Higuys:Thi...
'"""def __init__(self, count):HTMLParser.__init__(self)self.count = countself.summary = u''def feed(self, data):"""Only accept unicode `data"""`assert(isinstance(data, unicode))HTMLParser.feed(self, data)def handle_data(self, data):more = self.count - len(self.summary)if more > 0:# Remove possible whitespaces in `data`data_without_whitespace = u''.join(data.split())self.summary += data_without_whitespace[0:more]def get_summary(self, suffix=u'', wrapper=u'p'):return u'<{0}>{1}{2}
'.format(wrapper, self.summary, suffix)if __name__ == '__main__':import doctestdoctest.testmod()
The above is the [PYTHON Tutorial] to extract the content of the article abstract. For more information, please follow the PHP Chinese network (www.php1.cn )!