forked from grangier/python-goose
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoutputformatters.py
More file actions
138 lines (115 loc) · 4.61 KB
/
outputformatters.py
File metadata and controls
138 lines (115 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*-
"""\
This is a python port of "Goose" orignialy licensed to Gravity.com
under one or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.
Python port was written by Xavier Grangier for Recrutae
Gravity.com licenses this file
to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from six.moves.html_parser import HTMLParser
from goose.text import innerTrim
class OutputFormatter(object):
def __init__(self, config, article):
# config
self.config = config
# article
self.article = article
# parser
self.parser = self.config.get_parser()
# stopwords class
self.stopwords_class = config.stopwords_class
# top node
self.top_node = None
def get_language(self):
"""\
Returns the language is by the article or
the configuration language
"""
# we don't want to force the target language
# so we use the article.meta_lang
if self.config.use_meta_language:
if self.article.meta_lang:
return self.article.meta_lang[:2]
return self.config.target_language
def get_top_node(self):
return self.top_node
def get_formatted_text(self):
self.top_node = self.article.top_node
self.remove_negativescores_nodes()
self.links_to_text()
self.add_newline_to_br()
self.replace_with_text()
self.remove_fewwords_paragraphs()
return self.convert_to_text()
def convert_to_text(self):
txts = []
for node in list(self.get_top_node()):
txt = self.parser.getText(node)
if txt:
txt = HTMLParser().unescape(txt)
txt_lis = innerTrim(txt).split(r'\n')
txts.extend(txt_lis)
return '\n\n'.join(txts)
def add_newline_to_br(self):
for e in self.parser.getElementsByTag(self.top_node, tag='br'):
e.text = r'\n'
def links_to_text(self):
"""\
cleans up and converts any nodes that
should be considered text into text
"""
self.parser.stripTags(self.get_top_node(), 'a')
def remove_negativescores_nodes(self):
"""\
if there are elements inside our top node
that have a negative gravity score,
let's give em the boot
"""
gravity_items = self.parser.css_select(self.top_node, "*[gravityScore]")
for item in gravity_items:
score = self.parser.getAttribute(item, 'gravityScore')
score = int(score, 0)
if score < 1:
item.getparent().remove(item)
def replace_with_text(self):
"""\
replace common tags with just
text so we don't have any crazy formatting issues
so replace <br>, <i>, <strong>, etc....
with whatever text is inside them
code : http://lxml.de/api/lxml.etree-module.html#strip_tags
"""
self.parser.stripTags(self.get_top_node(), 'b', 'strong', 'i', 'br', 'sup')
def remove_fewwords_paragraphs(self):
"""\
remove paragraphs that have less than x number of words,
would indicate that it's some sort of link
"""
all_nodes = self.parser.getElementsByTags(self.get_top_node(), ['*'])
all_nodes.reverse()
for el in all_nodes:
tag = self.parser.getTag(el)
text = self.parser.getText(el)
stop_words = self.stopwords_class(language=self.get_language()).get_stopword_count(text)
if (tag != 'br' or text != '\\r') and stop_words.get_stopword_count() < 3 \
and len(self.parser.getElementsByTag(el, tag='object')) == 0 \
and len(self.parser.getElementsByTag(el, tag='embed')) == 0:
self.parser.remove(el)
# TODO
# check if it is in the right place
else:
trimmed = self.parser.getText(el)
if trimmed.startswith("(") and trimmed.endswith(")"):
self.parser.remove(el)
class StandardOutputFormatter(OutputFormatter):
pass