@mtf/

censoring

Python

No description

fork
loading
Files
  • main.py
main.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
66
def line_splitter(document):
  lines = [line.split(' ') for line in document.split('\n')]
  return lines

def line_joiner(lines):
  return '\n'.join([' '.join(line) for line in lines])

def pruner(word):
  non_alpha_index = [i for i, x in enumerate(word) if not x.isalpha()]
  non_alpha = ''.join(filter(lambda x: not x.isalpha(), word))
  alpha = ''.join(filter(lambda x: x.isalpha(), word))

def censor(document, proprietary=[]):
  lines = line_splitter(document)
  for line in lines:
    for i, x in enumerate(line):
      word = ''.join(filter(lambda x: x.isalpha(), x))
      if word in proprietary:
        line[i] = "X" * len(word)
  return line_joiner(lines)



d = """
Environment and Climate Change Canada (ECCC) confirmed a weak EF0 tornado touched down two kilometres northeast of Crossfield at around 2:15 p.m. Sunday, marking the 12th confirmed twister of the season.

In its weather summary Sunday night, Environment Canada reported a holiday trailer had been destroyed after it flipped over. It estimated wind speeds associated with this tornado were probably between 90 and 130 km/h.

According to the weather agency, Alberta typically sees an average of 15 tornadoes per year with the peak season starting mid-to-late June and ending early-to-mid August.

So far in 2019, there have been 12 confirmed tornadoes in Alberta and three probable ones. There have also been many reports of funnel clouds.

The first tornado occurred on April 24, 2019 near Peers, Alta. which is quite early for a twister in Alberta.

Since 1984, the busiest tornado season was in 1988 when there were 26 confirmed tornadoes.

© 2019 Global News, a division of Corus Entertainment Inc.
"""
def details(document):
  lines = line_splitter(document)
  max_len = 0
  word_count = 0
  for index, line in enumerate(lines):
    n = len(line)
    if n > 1:
      word_count += n
    if n > max_len:
      max_len = n
      max_index = index
  print ('-----------------------')
  print ('line count:', len(lines))
  print ('longest line:', [max_index], max_len)
  print ('word count:', word_count)
  return '-----------------------'

'''
doc = line_joiner(lines)
print (doc)
'''
print (details(d))
print (censor(d, ['Crossfield', 'Sunday', 'Environment', 'Canada', 'June', 'August', 'Peers', 'Corus', 'Entertainment', 'Inc.']))
'''
raw_word = 'Peers,'
word = ''.join(filter(lambda x: x.isalpha(), raw_word))
# 'Peers'
'''