-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.py
129 lines (105 loc) · 3.59 KB
/
line.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
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
import re
import logging
#from list import *
from source import Source, HasSource
from word import WordInstance
from letter import Letter, make_letters
from elision import make_elisions
from cluster import make_clusters
from syllable import make_syllables
from scan import make_scans, eliminate_redundant_scans
from command_line import command_line_arguments
from misc import lazy, trace, dd, Multiple, number_strs, flatten, intersperse, \
flatstr, str_per_command_line
alpha_only = re.compile('[^ a-z]')
def remove_spaces_and_nonalphabetics(s):
return ' '.join(alpha_only.sub('', s.lower()).split())
class Line(HasSource):
def __init__(self, original_text, source=None):
self.original_text = original_text
self.text = remove_spaces_and_nonalphabetics(self.original_text)
self.source = source
@property
@lazy
def word_instances(self):
return tuple(
WordInstance(w, Source(self, index))
for index, w in enumerate(self.text.split())
)
@lazy
def instances_of(self, dictionary_word):
return [
wi
for wi in self.word_instances
if wi.text == dictionary_word
]
def without_source(self):
return Line(self.original_text)
@property
@lazy
def letters(self):
return make_letters(self.word_instances)
@property
@lazy
def with_elisions(self):
return make_elisions(self.letters)
@property
@lazy
def with_clusters(self):
return make_clusters(self.with_elisions)
@property
@lazy
def syllables(self):
return make_syllables(self.with_clusters)
@property
@lazy
def scans(self):
return eliminate_redundant_scans(make_scans(self.syllables))
def __repr__(self):
return 'Line'
#TODO Add short version of index and source.
@property
@lazy
def line_num_str(self):
try:
return '%d. ' % self.source.index
except AttributeError:
return ''
def __str__(self):
result = []
if self.source:
result.append('%d. %s' % (self.source.index, self.original_text))
else:
result.append(self.original_text)
if len(self.scans):
for scan in self.scans:
result.append(' %s' % str(scan))
else:
result.append(' (Failed to scan.)')
return '\n'.join(result)
def str_per_command_line(self):
result = []
#result.append(self.text_per_command_line())
text_item = []
if command_line_arguments.original:
text_item.append(self.line_num_str + self.original_text)
if command_line_arguments.text:
text_item.append(self.line_num_str + self.text)
result.append('\n'.join(text_item))
if command_line_arguments.letters:
result.append(str_per_command_line(self.letters))
if command_line_arguments.elisions:
result.append(str_per_command_line(self.with_elisions))
if command_line_arguments.clusters:
result.append(str_per_command_line(self.with_clusters))
if command_line_arguments.syllables:
result.append(str_per_command_line(self.syllables))
if command_line_arguments.feet:
result.append(str_per_command_line(self.scans))
return '\n'.join(intersperse('', result))
def text_per_command_line(self):
return self.line_num_str() + (
self.original_text
if command_line_arguments.original
else self.text
)