-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextEditing.py
More file actions
162 lines (132 loc) · 4.54 KB
/
textEditing.py
File metadata and controls
162 lines (132 loc) · 4.54 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Error codes:
# 1 - Not enough arguments.
# 2 - Can't format correctly.
import argparse
from sys import stdin, stdout
punctuation_marks = [',', '.', '?', '!', '-', ':', '’']
# Dividing text into paragraphs.
def parse_paragraphs(text):
paragraphs = []
current_paragraph = ''
prev_line = ''
for line in text:
if line == '' and not prev_line == '':
paragraphs.append(current_paragraph)
current_paragraph = ''
elif line == '' and prev_line == '':
pass
prev_line = line
if not line == '':
current_paragraph += line + '\n'
if current_paragraph != '':
paragraphs.append(current_paragraph)
return paragraphs
# Forming right words from the text
def my_parser(text):
words = []
current_word = ""
size = len(text)
index = 0
while (index < size):
char = text[index]
if (char.isalpha() or char.isdigit()) and index < size:
current_word += char
index += 1
elif char == ' ':
while char == ' ' and index < size:
index += 1
char = text[index % size]
if (index == size):
if current_word != "":
words.append(current_word)
current_word = ""
elif punctuation_marks.count(char) > 0:
while not (char.isalpha() or char.isdigit()) and index < size:
if punctuation_marks.count(char) > 0:
current_word += char
index += 1
char = text[index % size]
if current_word != "":
words.append(current_word)
current_word = ""
elif char.isalpha() or char.isdigit():
if current_word != "":
words.append(current_word)
current_word = ""
elif punctuation_marks.count(char) > 0:
if current_word == "":
exit(2)
index += 1
else:
while not (char.isalpha() or char.isdigit()) and index < size:
if punctuation_marks.count(char) > 0:
current_word += char
index += 1
char = text[index % size]
words.append(current_word)
current_word = ""
elif char == '\n':
index += 1
if current_word != "":
words.append(current_word)
current_word = ""
if current_word != "":
words.append(current_word)
return words
# Format paragraph as task required.
def make_paragraph_great_again(paragraph, b, w):
words = my_parser(paragraph)
new_paragraph = []
if b + len(words[0]) > w:
exit(2)
current_line = ''
for i in range(b):
current_line += ' '
for word in words:
if len(word) > w:
exit(2)
if len(current_line) + len(word) + int(len(current_line) > 0) \
* 1 > w:
new_paragraph.append(current_line)
current_line = word
elif len(current_line) + len(word) + int(len(current_line) > 0) \
* 1 <= w:
if current_line == b * " ":
current_line += word
else:
current_line += ' ' + word
if current_line != '':
new_paragraph.append(current_line)
return '\n'.join(new_paragraph)
# Parse arguments.
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=str, default=stdin,
help='Input source')
parser.add_argument('-o', '--output', type=str, default=stdout,
help='Output source')
parser.add_argument('-l', '--line-length', type=int, default=None,
help='Length of the line')
parser.add_argument('-p', '--paragraph-spaces', type=int, default=None,
help='Paragraph spaces number')
args = parser.parse_args()
# Checking arguments.
if args.line_length is None or args.paragraph_spaces is None:
exit(1)
b = args.paragraph_spaces
w = args.line_length
# Open sources.
if args.input != stdin:
fin = open(args.input)
else:
fin = stdin
if args.output != stdout:
fout = open(args.output, 'w')
else:
fout = stdout
# Writing to output.
paragraphs = parse_paragraphs([line.strip() for line in fin])
for (index, paragraph) in enumerate(paragraphs):
fout.write(make_paragraph_great_again(paragraph, b=b, w=w))
if index < len(paragraphs) - 1:
fout.write("\n")
fout.close()