jusText 4.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. justext/__init__.py +15 -0
  2. justext/__main__.py +329 -0
  3. justext/_compat.py +53 -0
  4. justext/core.py +462 -0
  5. justext/paragraph.py +66 -0
  6. justext/stoplists/Afrikaans.txt +209 -0
  7. justext/stoplists/Albanian.txt +529 -0
  8. justext/stoplists/Arabic.txt +2762 -0
  9. justext/stoplists/Aragonese.txt +153 -0
  10. justext/stoplists/Armenian.txt +2908 -0
  11. justext/stoplists/Aromanian.txt +499 -0
  12. justext/stoplists/Asturian.txt +656 -0
  13. justext/stoplists/Azerbaijani.txt +3739 -0
  14. justext/stoplists/Basque.txt +2274 -0
  15. justext/stoplists/Belarusian.txt +4418 -0
  16. justext/stoplists/Belarusian_Taraskievica.txt +4098 -0
  17. justext/stoplists/Bengali.txt +1089 -0
  18. justext/stoplists/Bishnupriya_Manipuri.txt +55 -0
  19. justext/stoplists/Bosnian.txt +2234 -0
  20. justext/stoplists/Breton.txt +290 -0
  21. justext/stoplists/Bulgarian.txt +1134 -0
  22. justext/stoplists/Catalan.txt +227 -0
  23. justext/stoplists/Cebuano.txt +10 -0
  24. justext/stoplists/Chuvash.txt +2588 -0
  25. justext/stoplists/Croatian.txt +2463 -0
  26. justext/stoplists/Czech.txt +3208 -0
  27. justext/stoplists/Danish.txt +559 -0
  28. justext/stoplists/Dutch.txt +290 -0
  29. justext/stoplists/English.txt +503 -0
  30. justext/stoplists/Esperanto.txt +564 -0
  31. justext/stoplists/Estonian.txt +6295 -0
  32. justext/stoplists/Finnish.txt +7317 -0
  33. justext/stoplists/French.txt +414 -0
  34. justext/stoplists/Galician.txt +449 -0
  35. justext/stoplists/Georgian.txt +5433 -0
  36. justext/stoplists/German.txt +692 -0
  37. justext/stoplists/Greek.txt +626 -0
  38. justext/stoplists/Gujarati.txt +404 -0
  39. justext/stoplists/Haitian.txt +33 -0
  40. justext/stoplists/Hebrew.txt +4270 -0
  41. justext/stoplists/Hindi.txt +373 -0
  42. justext/stoplists/Hungarian.txt +3446 -0
  43. justext/stoplists/Icelandic.txt +721 -0
  44. justext/stoplists/Ido.txt +46 -0
  45. justext/stoplists/Igbo.txt +149 -0
  46. justext/stoplists/Indonesian.txt +919 -0
  47. justext/stoplists/Irish.txt +216 -0
  48. justext/stoplists/Italian.txt +569 -0
  49. justext/stoplists/Javanese.txt +610 -0
  50. justext/stoplists/Kannada.txt +7060 -0
  51. justext/stoplists/Kazakh.txt +3671 -0
  52. justext/stoplists/Korean.txt +13061 -0
  53. justext/stoplists/Kurdish.txt +576 -0
  54. justext/stoplists/Kyrgyz.txt +3872 -0
  55. justext/stoplists/Latin.txt +1904 -0
  56. justext/stoplists/Latvian.txt +3611 -0
  57. justext/stoplists/Lithuanian.txt +4964 -0
  58. justext/stoplists/Lombard.txt +127 -0
  59. justext/stoplists/Low_Saxon.txt +172 -0
  60. justext/stoplists/Luxembourgish.txt +387 -0
  61. justext/stoplists/Macedonian.txt +928 -0
  62. justext/stoplists/Malay.txt +877 -0
  63. justext/stoplists/Malayalam.txt +11782 -0
  64. justext/stoplists/Maltese.txt +1388 -0
  65. justext/stoplists/Marathi.txt +2151 -0
  66. justext/stoplists/Neapolitan.txt +300 -0
  67. justext/stoplists/Nepali.txt +1540 -0
  68. justext/stoplists/Newar.txt +88 -0
  69. justext/stoplists/Norwegian_Bokmal.txt +474 -0
  70. justext/stoplists/Norwegian_Nynorsk.txt +350 -0
  71. justext/stoplists/Occitan.txt +499 -0
  72. justext/stoplists/Persian.txt +485 -0
  73. justext/stoplists/Piedmontese.txt +71 -0
  74. justext/stoplists/Polish.txt +3570 -0
  75. justext/stoplists/Portuguese.txt +490 -0
  76. justext/stoplists/Quechua.txt +266 -0
  77. justext/stoplists/Romanian.txt +993 -0
  78. justext/stoplists/Russian.txt +5088 -0
  79. justext/stoplists/Samogitian.txt +1759 -0
  80. justext/stoplists/Serbian.txt +1700 -0
  81. justext/stoplists/Serbo_Croatian.txt +2418 -0
  82. justext/stoplists/Sicilian.txt +514 -0
  83. justext/stoplists/Simple_English.txt +280 -0
  84. justext/stoplists/Slovak.txt +2417 -0
  85. justext/stoplists/Slovenian.txt +2343 -0
  86. justext/stoplists/Spanish.txt +304 -0
  87. justext/stoplists/Sundanese.txt +868 -0
  88. justext/stoplists/Swahili.txt +385 -0
  89. justext/stoplists/Swedish.txt +737 -0
  90. justext/stoplists/Tagalog.txt +148 -0
  91. justext/stoplists/Tamil.txt +6281 -0
  92. justext/stoplists/Telugu.txt +4566 -0
  93. justext/stoplists/Turkish.txt +3082 -0
  94. justext/stoplists/Turkmen.txt +3204 -0
  95. justext/stoplists/Ukrainian.txt +5025 -0
  96. justext/stoplists/Urdu.txt +233 -0
  97. justext/stoplists/Uzbek.txt +5927 -0
  98. justext/stoplists/Vietnamese.txt +292 -0
  99. justext/stoplists/Volapuk.txt +75 -0
  100. justext/stoplists/Walloon.txt +178 -0
  101. justext/stoplists/Waray_Waray.txt +11 -0
  102. justext/stoplists/Welsh.txt +550 -0
  103. justext/stoplists/West_Frisian.txt +251 -0
  104. justext/stoplists/Western_Panjabi.txt +260 -0
  105. justext/stoplists/Yoruba.txt +307 -0
  106. justext/utils.py +66 -0
  107. justext-4.0.0.dist-info/METADATA +126 -0
  108. justext-4.0.0.dist-info/RECORD +110 -0
  109. justext-4.0.0.dist-info/WHEEL +4 -0
  110. justext-4.0.0.dist-info/licenses/LICENSE.rst +24 -0
justext/__init__.py ADDED
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ """
4
+ Copyright (c) 2011 Jan Pomikalek
5
+
6
+ This software is licensed as described in the file LICENSE.rst.
7
+ """
8
+
9
+ from __future__ import absolute_import
10
+
11
+ from .utils import get_stoplists, get_stoplist
12
+ from .core import justext
13
+
14
+
15
+ __version__ = "4.0.0"
justext/__main__.py ADDED
@@ -0,0 +1,329 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from __future__ import absolute_import
4
+ from __future__ import division, print_function, unicode_literals
5
+
6
+ import codecs
7
+ import os
8
+ import re
9
+ import sys
10
+
11
+ from ._compat import escape, PY3, URLError, urllib
12
+ from .core import *
13
+ from .utils import get_stoplist, get_stoplists
14
+
15
+
16
+ def usage():
17
+ return """Usage: %(progname)s -s STOPLIST [OPTIONS] [HTML_FILE]
18
+ Convert HTML to plain text and remove boilerplate.
19
+
20
+ -o OUTPUT_FILE if not specified, output is written to stdout
21
+ --encoding=... default character encoding to be used if not specified
22
+ in the HTML meta tags (default: %(default_encoding)s)
23
+ --enc-force force specified encoding, ignore HTML meta tags
24
+ --enc-errors=... errors handling for character encoding conversion:
25
+ strict: fail on error
26
+ ignore: ignore characters which can't be converted
27
+ replace: replace characters which can't be converted
28
+ with U+FFFD unicode replacement characters
29
+ (default: %(default_enc_errors)s)
30
+ --format=... output format; possible values:
31
+ default: one paragraph per line, each preceded with
32
+ <p> or <h> (headings)
33
+ boilerplate: same as default, except for boilerplate
34
+ paragraphs are included, too, preceded
35
+ with <b>
36
+ detailed: one paragraph per line, each preceded with
37
+ <p> tag containing detailed information
38
+ about classification as attributes
39
+ krdwrd: KrdWrd compatible format
40
+ --no-headings disable special handling of headings
41
+ --list-stoplists print a list of inbuilt stoplists and exit
42
+ -V, --version print version information and exit
43
+ -h, --help display this help and exit
44
+
45
+ If no HTML_FILE specified, input is read from stdin.
46
+
47
+ STOPLIST must be one of the following:
48
+ - one of the inbuilt stoplists; see:
49
+ %(progname)s --list-stoplists
50
+ - path to a file with the most frequent words for given language,
51
+ one per line, in UTF-8 encoding
52
+ - None - this activates a language-independent mode
53
+
54
+ Advanced options:
55
+ --length-low=INT (default %(length_low)i)
56
+ --length-high=INT (default %(length_high)i)
57
+ --stopwords-low=FLOAT (default %(stopwords_low)f)
58
+ --stopwords-high=FLOAT (default %(stopwords_high)f)
59
+ --max-link-density=FLOAT (default %(max_link_density)f)
60
+ --max-heading-distance=INT (default %(max_heading_distance)i)
61
+ --max-good-distance=INT (default no limit)
62
+ """ % {
63
+ 'progname': os.path.basename(os.path.basename(sys.argv[0])),
64
+ 'length_low': LENGTH_LOW_DEFAULT,
65
+ 'length_high': LENGTH_HIGH_DEFAULT,
66
+ 'stopwords_low': STOPWORDS_LOW_DEFAULT,
67
+ 'stopwords_high': STOPWORDS_HIGH_DEFAULT,
68
+ 'max_link_density': MAX_LINK_DENSITY_DEFAULT,
69
+ 'max_heading_distance': MAX_HEADING_DISTANCE_DEFAULT,
70
+ 'default_encoding': DEFAULT_ENCODING,
71
+ 'default_enc_errors': DEFAULT_ENC_ERRORS,
72
+ }
73
+
74
+
75
+ def output_default(paragraphs, fp=sys.stdout, no_boilerplate=True):
76
+ """
77
+ Outputs the paragraphs as:
78
+ <tag> text of the first paragraph
79
+ <tag> text of the second paragraph
80
+ ...
81
+ where <tag> is <p>, <h> or <b> which indicates
82
+ standard paragraph, heading or boilerplate respecitvely.
83
+ """
84
+ for paragraph in paragraphs:
85
+ if paragraph.class_type == 'good':
86
+ if paragraph.heading:
87
+ tag = 'h'
88
+ else:
89
+ tag = 'p'
90
+ elif no_boilerplate:
91
+ continue
92
+ else:
93
+ tag = 'b'
94
+
95
+ print('<%s> %s' % (tag, escape(paragraph.text, quote=False)), file=fp)
96
+
97
+
98
+ def output_detailed(paragraphs, fp=sys.stdout):
99
+ """
100
+ Same as output_default, but only <p> tags are used and the following
101
+ attributes are added: class, cfclass and heading.
102
+ """
103
+ for paragraph in paragraphs:
104
+ output = '<p class="%s" cfclass="%s" heading="%i" xpath="%s"> %s' % (
105
+ paragraph.class_type,
106
+ paragraph.cf_class,
107
+ int(paragraph.heading),
108
+ paragraph.xpath,
109
+ escape(paragraph.text, quote=False)
110
+ )
111
+ print(output, file=fp)
112
+
113
+
114
+ def output_krdwrd(paragraphs, fp=sys.stdout):
115
+ """
116
+ Outputs the paragraphs in a KrdWrd compatible format:
117
+ class<TAB>first text node
118
+ class<TAB>second text node
119
+ ...
120
+ where class is 1, 2 or 3 which means
121
+ boilerplate, undecided or good respectively. Headings are output as
122
+ undecided.
123
+ """
124
+ for paragraph in paragraphs:
125
+ if paragraph.class_type in ('good', 'neargood'):
126
+ if paragraph.heading:
127
+ cls = 2
128
+ else:
129
+ cls = 3
130
+ else:
131
+ cls = 1
132
+
133
+ for text_node in paragraph.text_nodes:
134
+ print('%i\t%s' % (cls, text_node.strip()), file=fp)
135
+
136
+
137
+ def main():
138
+ import getopt
139
+ from justext import __version__ as VERSION
140
+
141
+ try:
142
+ opts, args = getopt.getopt(sys.argv[1:], "o:s:hV", ["encoding=",
143
+ "enc-force", "enc-errors=", "format=",
144
+ "no-headings", "help", "version", "length-low=", "length-high=",
145
+ "stopwords-low=", "stopwords-high=", "max-link-density=",
146
+ "max-heading-distance=", "max-good-distance=", "list-stoplists"])
147
+ except getopt.GetoptError as e:
148
+ print(e, file=sys.stderr)
149
+ print(usage(), file=sys.stderr)
150
+ sys.exit(1)
151
+
152
+ stream_writer = codecs.lookup('utf8')[-1]
153
+ fp_in = sys.stdin
154
+ if PY3:
155
+ fp_out = stream_writer(sys.stdout.buffer)
156
+ else:
157
+ fp_out = stream_writer(sys.stdout)
158
+ stoplist = None
159
+ format = 'default'
160
+ no_headings = False
161
+ length_low = LENGTH_LOW_DEFAULT
162
+ length_high = LENGTH_HIGH_DEFAULT
163
+ stopwords_low = STOPWORDS_LOW_DEFAULT
164
+ stopwords_high = STOPWORDS_HIGH_DEFAULT
165
+ max_link_density = MAX_LINK_DENSITY_DEFAULT
166
+ max_heading_distance = MAX_HEADING_DISTANCE_DEFAULT
167
+ max_good_distance = MAX_GOOD_DISTANCE_DEFAULT
168
+ encoding = None
169
+ default_encoding = DEFAULT_ENCODING
170
+ force_default_encoding = False
171
+ enc_errors = DEFAULT_ENC_ERRORS
172
+
173
+ try:
174
+ for o, a in opts:
175
+ if o in ("-h", "--help"):
176
+ print(usage())
177
+ sys.exit(0)
178
+ if o in ("-V", "--version"):
179
+ print("%s: jusText v%s\n\nCopyright (c) 2011 Jan Pomikalek <jan.pomikalek@gmail.com>" % (
180
+ os.path.basename(sys.argv[0]), VERSION))
181
+ sys.exit(0)
182
+ elif o == "--list-stoplists":
183
+ print("\n".join(sorted(get_stoplists())))
184
+ sys.exit(0)
185
+ elif o == "-o":
186
+ try:
187
+ fp_out = codecs.open(a, 'w', 'utf8')
188
+ except IOError as e:
189
+ raise JustextInvalidOptions(
190
+ "Can't open %s for writing: %s" % (a, e))
191
+ elif o == "-s":
192
+ if a.lower() == 'none':
193
+ stoplist = set()
194
+ else:
195
+ if os.path.isfile(a):
196
+ try:
197
+ fp_stoplist = codecs.open(a, 'r', 'utf8')
198
+ stoplist = set([line.strip() for line in fp_stoplist])
199
+ fp_stoplist.close()
200
+ except IOError as e:
201
+ raise JustextInvalidOptions(
202
+ "Can't open %s for reading: %s" % (a, e))
203
+ except UnicodeDecodeError as e:
204
+ raise JustextInvalidOptions(
205
+ "Unicode decoding error when reading "
206
+ "the stoplist (probably not in UTF-8): %s" % e)
207
+ elif a in get_stoplists():
208
+ stoplist = get_stoplist(a)
209
+ else:
210
+ if re.match(r'^\w*$', a):
211
+ # only alphabetical chars, probably misspelled or
212
+ # unsupported language
213
+ raise JustextInvalidOptions(
214
+ "Unknown stoplist: %s\nAvailable stoplists:\n%s" % (
215
+ a, '\n'.join(sorted(get_stoplists()))))
216
+ else:
217
+ # probably incorrectly specified path
218
+ raise JustextInvalidOptions("File not found: %s" % a)
219
+ elif o == "--encoding":
220
+ try:
221
+ default_encoding = a
222
+ ''.encode(default_encoding)
223
+ except LookupError:
224
+ raise JustextInvalidOptions("Uknown character encoding: %s" % a)
225
+ elif o == "--enc-force":
226
+ force_default_encoding = True
227
+ elif o == "--enc-errors":
228
+ if a.lower() in ['strict', 'ignore', 'replace']:
229
+ enc_errors = a.lower()
230
+ else:
231
+ raise JustextInvalidOptions("Invalid --enc-errors value: %s" % a)
232
+ elif o == "--format":
233
+ if a in ['default', 'boilerplate', 'detailed', 'krdwrd']:
234
+ format = a
235
+ else:
236
+ raise JustextInvalidOptions("Uknown output format: %s" % a)
237
+ elif o == "--no-headings":
238
+ no_headings = True
239
+ elif o == "--length-low":
240
+ try:
241
+ length_low = int(a)
242
+ except ValueError:
243
+ raise JustextInvalidOptions(
244
+ "Invalid value for %s: '%s'. Integer expected." % (o, a))
245
+ elif o == "--length-high":
246
+ try:
247
+ length_high = int(a)
248
+ except ValueError:
249
+ raise JustextInvalidOptions(
250
+ "Invalid value for %s: '%s'. Integer expected." % (o, a))
251
+ elif o == "--stopwords-low":
252
+ try:
253
+ stopwords_low = float(a)
254
+ except ValueError:
255
+ raise JustextInvalidOptions(
256
+ "Invalid value for %s: '%s'. Float expected." % (o, a))
257
+ elif o == "--stopwords-high":
258
+ try:
259
+ stopwords_high = float(a)
260
+ except ValueError:
261
+ raise JustextInvalidOptions(
262
+ "Invalid value for %s: '%s'. Float expected." % (o, a))
263
+ elif o == "--max-link-density":
264
+ try:
265
+ max_link_density = float(a)
266
+ except ValueError:
267
+ raise JustextInvalidOptions(
268
+ "Invalid value for %s: '%s'. Float expected." % (o, a))
269
+ elif o == "--max-heading-distance":
270
+ try:
271
+ max_heading_distance = int(a)
272
+ except ValueError:
273
+ raise JustextInvalidOptions(
274
+ "Invalid value for %s: '%s'. Integer expected." % (o, a))
275
+ elif o == "--max-good-distance":
276
+ try:
277
+ max_good_distance = int(a)
278
+ except ValueError:
279
+ raise JustextInvalidOptions(
280
+ "Invalid value for %s: '%s'. Integer expected." % (o, a))
281
+
282
+ if force_default_encoding:
283
+ encoding = default_encoding
284
+
285
+ if stoplist is None:
286
+ raise JustextInvalidOptions("No stoplist specified.")
287
+
288
+ if not stoplist:
289
+ # empty stoplist, switch to language-independent mode
290
+ stopwords_high = 0
291
+ stopwords_low = 0
292
+
293
+ if args:
294
+ try:
295
+ if re.match(r"[^:/]+://", args[0]):
296
+ fp_in = urllib.urlopen(args[0])
297
+ else:
298
+ fp_in = open(args[0], 'r')
299
+ except (IOError, URLError) as e:
300
+ raise JustextInvalidOptions(
301
+ "Can't open %s for reading: %s" % (args[0], e))
302
+
303
+ html_text = fp_in.read()
304
+ if fp_in is not sys.stdin:
305
+ fp_in.close()
306
+
307
+ paragraphs = justext(html_text, stoplist, length_low, length_high,
308
+ stopwords_low, stopwords_high, max_link_density, max_heading_distance,
309
+ no_headings, encoding, default_encoding, enc_errors,
310
+ max_good_distance=max_good_distance)
311
+ if format == "default":
312
+ output_default(paragraphs, fp_out)
313
+ elif format == "boilerplate":
314
+ output_default(paragraphs, fp_out, no_boilerplate=False)
315
+ elif format == "detailed":
316
+ output_detailed(paragraphs, fp_out)
317
+ elif format == "krdwrd":
318
+ output_krdwrd(paragraphs, fp_out)
319
+ else:
320
+ # this should not happen; format checked when parsing options
321
+ raise AssertionError("Unknown format: %s" % format)
322
+
323
+ except JustextError as e:
324
+ print("%s: %s" % (os.path.basename(sys.argv[0]), e), file=sys.stderr)
325
+ sys.exit(1)
326
+
327
+
328
+ if __name__ == "__main__":
329
+ main()
justext/_compat.py ADDED
@@ -0,0 +1,53 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from __future__ import absolute_import
4
+ from __future__ import division, print_function, unicode_literals
5
+
6
+ from sys import version_info
7
+
8
+
9
+ PY3 = version_info[0] == 3
10
+
11
+
12
+ if PY3:
13
+ bytes = bytes
14
+ unicode = str
15
+ else:
16
+ bytes = str
17
+ unicode = unicode
18
+ string_types = (bytes, unicode,)
19
+
20
+
21
+ # zip must be lazy, Python 2 zip builds the whole list
22
+ try:
23
+ from itertools import izip as zip
24
+ except ImportError:
25
+ zip = zip
26
+
27
+
28
+ if PY3:
29
+ import urllib.request as urllib
30
+ from urllib.error import URLError
31
+ else:
32
+ import urllib2 as urllib
33
+ URLError = urllib.URLError
34
+
35
+
36
+ try:
37
+ from contextlib import ignored
38
+ except ImportError:
39
+ from contextlib import contextmanager
40
+
41
+ @contextmanager
42
+ def ignored(*exceptions):
43
+ try:
44
+ yield
45
+ except tuple(exceptions):
46
+ pass
47
+
48
+
49
+ # note that cgi is depecrated and removed since 3.8
50
+ try:
51
+ from html import escape
52
+ except ImportError:
53
+ from cgi import escape