Class: Parser
- Inherits:
-
Object
- Object
- Parser
- Defined in:
- lib/my_precious/parser.rb
Constant Summary collapse
- WRITER_FILE =
'output.rb'- PUT_KEYWORDS =
KEYWORDS LINE_KEYWORDS = [‘,’, ‘.’, ‘?’, ‘!’, ‘;’]
['bring forth the ring', 'says', 'screams', 'exclaims', 'sobbs', 'coughs']
- COMMENT_KEYWORDS =
['second breakfast', 'wear the ring']
- ASSIGNMENT_KEYWORDS =
['is', 'are', 'was', 'has']
- INCREMENT_KEYWORDS =
['eats lembas bread', 'fortifies stronghold', 'rests', 'recieves Evenstar', 'reforges Narsil']
- DECREMENT_KEYWORDS =
['runs out of lembas bread', 'lost', 'hunted by orcs']
- ADDITION_KEYWORDS =
['joins', 'join', 'and', 'accompanies']
- SUBTRACTION_KEYWORDS =
['leaves the fellowship', 'stabs', 'banishes', 'steals']
- MULTIPLICATION_KEYWORDS =
['gives aid to', 'procreates', 'bolsters']
- DIVISION_KEYWORDS =
['decapitates', 'dismembers']
- COMPARISON_KEYWORDS =
['equal', 'same', 'similar']
- CONDITION_KEYWORDS =
['does', 'if', 'will']
- END_KEYWORDS =
['you shall not pass']
- TRUE_KEYWORDS =
['precious']
- LOOP_KEYWORDS =
['whilst', 'during the journey']
- GREATER_THAN_KEYWORDS =
['stronger than', 'more']
- LESS_THAN_KEYWORDS =
['weaker than', 'less']
- FUNCTION_DEF_KEYWORDS =
['transcribe', 'tell a story']
- FUNCTION_CALL_KEYWORDS =
["theyre taking the hobbits to"]
- PARAM_KEYWORDS =
['with']
- NEGATION_KEYWORDS =
['not']
- CLASS_KEYWORDS =
['chapter']
- OPERATOR_KEYWORDS =
['#', '+', '-', '=', '*', '/', '+=1', '-=1', 'puts', '==', 'if', 'end', 'true', 'while', '>', '<', '!', 'def', '(', ')', 'class']
- ALICIA_KEYS =
[END_KEYWORDS, PUT_KEYWORDS, ASSIGNMENT_KEYWORDS, INCREMENT_KEYWORDS, DECREMENT_KEYWORDS, ADDITION_KEYWORDS, SUBTRACTION_KEYWORDS, DIVISION_KEYWORDS, MULTIPLICATION_KEYWORDS, COMPARISON_KEYWORDS, CONDITION_KEYWORDS, TRUE_KEYWORDS, LOOP_KEYWORDS, GREATER_THAN_KEYWORDS, LESS_THAN_KEYWORDS, NEGATION_KEYWORDS, FUNCTION_DEF_KEYWORDS, FUNCTION_CALL_KEYWORDS, CLASS_KEYWORDS]
- MAP =
[{'puts': PUT_KEYWORDS}, {'#': COMMENT_KEYWORDS}, {'=': ASSIGNMENT_KEYWORDS}, {'+=1': INCREMENT_KEYWORDS}, {'-=1': DECREMENT_KEYWORDS}, {'+': ADDITION_KEYWORDS}, {'-': SUBTRACTION_KEYWORDS}, {'*': MULTIPLICATION_KEYWORDS}, {'/': DIVISION_KEYWORDS}, {'==': COMPARISON_KEYWORDS}, {'if': CONDITION_KEYWORDS}, {'end': END_KEYWORDS}, {'true': TRUE_KEYWORDS}, {'while': LOOP_KEYWORDS}, {'>': GREATER_THAN_KEYWORDS}, {'<': LESS_THAN_KEYWORDS}, {'!': NEGATION_KEYWORDS}, {'def': FUNCTION_DEF_KEYWORDS}, {'': FUNCTION_CALL_KEYWORDS},{'(': PARAM_KEYWORDS}, {'class': CLASS_KEYWORDS}]
Class Method Summary collapse
- .check_for_keywords(line) ⇒ Object
- .find_replacement(keywords) ⇒ Object
- .format_function(line) ⇒ Object
- .get_quote(phrase) ⇒ Object
- .only_valuable_words(line) ⇒ Object
-
.parse(line, keywords) ⇒ Object
METHODS.
- .parse_file(file, output_file_name) ⇒ Object
- .parse_line(line, index) ⇒ Object
- .purify(word) ⇒ Object
- .remove_newline(line) ⇒ Object
- .store_important(phrase, key) ⇒ Object
- .valuable?(word) ⇒ Boolean
- .write(str, writer_file_name) ⇒ Object
Class Method Details
.check_for_keywords(line) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/my_precious/parser.rb', line 121 def self.check_for_keywords(line) ALICIA_KEYS.each do |keywords| line = parse(line, keywords) end line end |
.find_replacement(keywords) ⇒ Object
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/my_precious/parser.rb', line 201 def self.find_replacement(keywords) MAP.each do |hash| hash.each do |key, value| if value == keywords return key.to_s end end end return nil end |
.format_function(line) ⇒ Object
212 213 214 215 216 217 218 |
# File 'lib/my_precious/parser.rb', line 212 def self.format_function(line) phrase_array = line.split('') index = phrase_array.find_index { |i| i == "("} phrase_array.delete_at(index - 1) phrase_array.delete_at(index - 2) phrase_array.join("") end |
.get_quote(phrase) ⇒ Object
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/my_precious/parser.rb', line 179 def self.get_quote(phrase) phrase_array = phrase.split('') index = phrase_array.find_index { |i| i == '"'} + 1 quote = "" while phrase_array[index] != '"' quote << phrase_array[index] index += 1 end quote end |
.only_valuable_words(line) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/my_precious/parser.rb', line 147 def self.only_valuable_words(line) important_words = [] line_array = line.split(" ") line_array.each do |word| valuable_word = valuable?(word) if valuable_word important_words << word end end line = important_words.join(" ") end |
.parse(line, keywords) ⇒ Object
METHODS
129 130 131 132 133 134 135 136 137 |
# File 'lib/my_precious/parser.rb', line 129 def self.parse(line, keywords) keywords.each do |keyword| if line.include? keyword replacement = find_replacement(keywords) line = line.gsub(keyword, replacement) end end return line end |
.parse_file(file, output_file_name) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/my_precious/parser.rb', line 32 def self.parse_file(file, output_file_name) str = "" writer_file_name = output_file_name file.each_with_index do |file, index| file.each_line do |line| line = parse_line(line, index) str << (line + "\n") end end write(str, writer_file_name) end |
.parse_line(line, index) ⇒ Object
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 |
# File 'lib/my_precious/parser.rb', line 44 def self.parse_line(line, index) #error handeling #ignore lines of length 1, its empty if line.length == 1 # write("#{line}") return line else #check if there are strings in line quote = "" if line.include? '"' quote = get_quote(line) line = line.gsub(quote, 'quote_placeholder') end #check if line is a comment comment = "" if COMMENT_KEYWORDS.any? { |word| line.include?(word) } line = parse(line, COMMENT_KEYWORDS) comment = store_important(line, '#') line = line.gsub(comment, 'comment_placeholder') end #check if line has params params = "" if ((PARAM_KEYWORDS.any? { |word| line.include?(word) }) && ((FUNCTION_DEF_KEYWORDS.any? { |word| line.include?(word) }) || (FUNCTION_CALL_KEYWORDS.any? { |word| line.include?(word) }))) line = parse(line, PARAM_KEYWORDS) params = remove_newline(store_important(line, '(')) line = line.gsub(params, 'param_placeholder ') + ')' end class_word = "" if CLASS_KEYWORDS.any? { |word| line.include?(word) } line = parse(line, CLASS_KEYWORDS) class_word = store_important(line, ':') line = line.gsub(class_word, ' class_word_placeholder') end #get rid of special chars line_array = line.split(" ") line_array.each_with_index do |word, index| line_array[index] = purify(word) end #check for keywords #calls if statements line = line_array.join(" ") line = check_for_keywords(line) #remove extra english words line = only_valuable_words(line).downcase #put string back into line if line.include? ('"quote_placeholder"') line = line.gsub('quote_placeholder', quote) end if line.include? ('comment_placeholder') line = line.gsub('comment_placeholder', comment) end if line.include? ('param_placeholder') line = line.gsub('param_placeholder', params.downcase) line = format_function(line) end if line.include? ('class_word_placeholder') line = line.gsub('class_word_placeholder', "#{class_word}") end # write("#{line}") return line end end |
.purify(word) ⇒ Object
139 140 141 |
# File 'lib/my_precious/parser.rb', line 139 def self.purify(word) word = word.gsub(/[!@%&.?,]/,'')# get rid of special chars end |
.remove_newline(line) ⇒ Object
143 144 145 |
# File 'lib/my_precious/parser.rb', line 143 def self.remove_newline(line) line.gsub(/\n/, "") end |
.store_important(phrase, key) ⇒ Object
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/my_precious/parser.rb', line 190 def self.store_important(phrase, key) phrase_array = phrase.split('') index = phrase_array.find_index { |i| i == key} + 1 string = "" while index < phrase_array.length string << phrase_array[index] index += 1 end string end |
.valuable?(word) ⇒ Boolean
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/my_precious/parser.rb', line 159 def self.valuable?(word) valuable = false if /[[:upper:]]/.match(word[0]) #if variable valuable = true elsif word == '"quote_placeholder"' #if string/quote valuable = true elsif word == '#comment_placeholder' #if comment valuable = true elsif word == '(param_placeholder' #if params valuable = true elsif word == 'class_word_placeholder' #if class valuable = true elsif word.to_i.to_s == word #if num valuable = true elsif OPERATOR_KEYWORDS.include? word #if operator valuable = true end valuable end |
.write(str, writer_file_name) ⇒ Object
220 221 222 223 |
# File 'lib/my_precious/parser.rb', line 220 def self.write(str, writer_file_name) writer_file = File.open(writer_file_name, 'w') writer_file.write(str) end |