-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmltowords.py
executable file
·65 lines (46 loc) · 1.13 KB
/
htmltowords.py
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
#!/usr/bin/env python3
##
# @name htmltowords
# @desc NA
##
### Imports
import sys
import re
import urllib.parse
import string
import io
import os
### Main Execution
if __name__ == '__main__':
INCLUDE_HOST = False
# Parse Command Line
if len(sys.argv) == 2:
if sys.argv[1] == '-h':
INCLUDE_HOST = True
# Add UTF-8 Support
stream = io.TextIOWrapper(sys.stdin.buffer, encoding='iso-8859-1')
# Grab Standard Input
for line in stream:
line = line.rstrip()
# Grab Words Between Tags
word_groups = re.findall('>([^<]+)|^([^<]+)', line)
# Grab Group of Words
for group1, group2 in word_groups:
# Split into Words
words = []
words.extend(group1.split())
words.extend(group2.split())
for word in words:
# Remove Punctuation
word.translate(str.maketrans('', '', string.punctuation))
# Make Sure Valid
word = word.rstrip()
if word.isnumeric() or not word.isalpha(): continue
if len(word) < 3: continue
# Print
if not INCLUDE_HOST:
print(word.lower())
else:
in_file = os.getenv('mapreduce_map_input_file')
print(f'{in_file}\t{word.lower()}')
sys.exit(0)