-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextReader.py
156 lines (134 loc) · 5.74 KB
/
TextReader.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
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
#A CS 3110 Note to self
#WHY DO I PROGRAM? - I LOVE BUILDING PROJECTS, OBJECTS, THINGS I LOVE TO SEE WORK
#PROPERLY. I WANT TO CREATE THINGS THAT ARE COOL FOR MYSELF WHILE CHALLENGING MYSELF
from Constants import *
def textReaderDriver():
try:
filename = enterFileName()
return textReaderController(filename, True)
except IOError as e:
print("IO ERROR - no such txt file in your current directory - quitting program")
def textReaderController(filename, convertToSentences ):
"""
Header function, to be used with other modules
"""
fileString = readDocument(filename)
return format_text(fileString, convertToSentences)
def enterFileName(): # NOT TESTED YET
print("Please navigate to the correct directory where you want to run this program. ")
result = input("Please enter your file name with '.txt' at the end: ")
if '.txt' not in result:
print("No txt extension on file name. ")
return result
def readDocument(filename):
"""
Preconditions: the file must be a standard .txt file
File must be in the following format: question \n ANSWER: answer \n \n next Question Answer cycle, etc END
Reads the file, searching for "ANSWER"
"""
f = open(filename, 'r')
fileString = f.read()
return fileString
def format_text(string_text, convert_to_sentences):
"""
Formats text properly into dictionary
Precondition:
convert_to_sentences [bool] true if convert to sentences else don't
string_text is a non_empty [str] such that
it consists of blocks of questions with the block
QUESTION: asas ANSWER: asda END || followed by possibly another question
It must end in '||'
Returns: list of tuples where the first element in the tuple are the list of
question clues and the second is a string and is the answerline
"""
listOfQuestionBlocks = convert_text_to_block(string_text)
questionAnswerTuples = []
for question in listOfQuestionBlocks:
if not convert_to_sentences:
pair = question_answer(question)
questionAnswerTuples.append(([pair[0]], pair[1]))
else:
questionAnswerTuples.append(question_answer(question))
if not convert_to_sentences:
return questionAnswerTuples
finalQuestionAnswerList = []
for qaTuple in questionAnswerTuples:
questionBlock = qaTuple[0]
questionAnswer = qaTuple[1]
questionList = convert_block_to_list(questionBlock)
newTuple = (questionList, questionAnswer)
finalQuestionAnswerList.append(newTuple)
return finalQuestionAnswerList
def convert_text_to_block(string_text, list_of_blocks = []):
"""
tail recursive
every block of string must be ended by '||', and moreover, the string_text must end in '||' as well.
"""
if string_text == '':
return list_of_blocks
newLinePos = string_text.index('||')
beforeNewLines = string_text[:newLinePos]
afterNewLines = string_text[newLinePos + 2:] # plus one plus two more for both new lines
newListOfBlocks = list_of_blocks + [beforeNewLines]
return convert_text_to_block(afterNewLines, newListOfBlocks)
def question_answer(single_block):
"""
tail recursive
single Block must have QUESTION, ANSWER, and END in it
"""
single_block = single_block.strip()
q = single_block.index(QUESTION)
a = single_block.index(ANSWER)
e = single_block.index(END)
return (single_block[q + len(QUESTION): a].strip(), single_block[a + len(ANSWER): e].strip())
def convert_block_to_list(string_block):
"""
Wrapper Function
PostCondition: Returns a block of string into a list of strings
PreCondition: The block of strings has at least one period, with a period ending the string block.
String_block is not an empty string and is [str]
"""
stringLen = len(string_block)
if string_block[stringLen - 1] != '.':
string_block = string_block + '.'
newList = [element.strip() for element in block_to_sentence(string_block)]
return [element for element in newList if element != '.']
def block_to_sentence(string_block, sentence_list = []):
"""
Tail Recursive
PostCondition: Converts a block of string into a list of strings, separated by the periods
PreCondition: The block of strings has at least one period, with a period ending the string block.
String_block is not an empty string and is [str]
"""
if string_block == "":
return sentence_list
p = string_block.index(".")
beforePeriod = string_block[:p + 1]
afterPeriod = string_block[p + 1:]
newSentenceList = sentence_list + [beforePeriod]
return block_to_sentence(afterPeriod, newSentenceList)
##########DEAD OR DEPRECATED CODE. ONLY FIND PERIODS WAS TESTED#########
def find_periods(string_block, period_list = []):
"""
Tail Recursive
PostCondition: Returns a list of positions where periods are located in string_block
PreCondition: String_block is a [str] not empty, with at least one period, ending in a period
"""
if (string_block == "") or ('.' not in string_block):
return period_list
periodPos = string_block.rindex(".")
newPeriodList = [periodPos] + period_list
after_period_string = string_block[:periodPos]
return find_periods(after_period_string, newPeriodList)
def searchForAnswer(fileString):
"""
Recursively searches fileString for ANSWER, and will return a list of positions where ANSWER will be
"""
if fileString == []:
return []
answerList = []
answerPos = fileString.index(ANSWER)
answerList.append(answerPos)
return answerPos + searchForAnswer(fileString[answerPos + 1: ])
def searchForEnd(fileString):
return fileString.index(END)