my_precious 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9363adf2030b310af9f66d7ba0263ce1cd7ddbafba5f1a02ae0f204c19afac5
4
- data.tar.gz: 1fb4a1c11be1593a8453d74d2f53949d6e1a38744b806f1e416b10edafd0955b
3
+ metadata.gz: e5c18ec277c010e718d3f52a2296caec5f556dc95f961a7f39e6f75abe43aabd
4
+ data.tar.gz: c750e66840fe41511b26a2aa4be59ecf611c096bfd3213731703d54d0f70da4b
5
5
  SHA512:
6
- metadata.gz: f56f4a2ca06f6c796ef6e3e673afa19b5806e61e60f3454703dc0b59bbb78c193f559aa41aa000297e25bcbd64605c917a0fe078e7bee07c2beb483482de2ae0
7
- data.tar.gz: b5db56234e72011bfa018bef2d36abaad362d4e95ae88ae8c3aa5ab1db80efbebf693346aff4fd083c8ff7b5763d70998a579646375995fd5ac6a17c998ed441
6
+ metadata.gz: 9eb640feaecaa15025a08d722ee751db911c23e8461e49c68784b856a10aad87bb28e2e59537f97efa47dbfe0abf91830c380b8d95206646f798112b6bff0c50
7
+ data.tar.gz: f77cd35b4a1463ddb01ae5cc9b043b7ad76281c9109fca8f3c73f18155d76743894266892b3f3a842afdac5126230244331cfbbf3547a646764c282081094bef
@@ -1,6 +1,6 @@
1
1
  require 'thor'
2
2
  require 'my_precious/version'
3
- require 'my_precious/parser'
3
+ require 'my_precious/parser_2'
4
4
 
5
5
  module MyPrecious
6
6
  class Error < StandardError; end
@@ -18,8 +18,8 @@ module MyPrecious
18
18
  exts_are_valid = check_reader_and_writer_exts(reader_file_name, writer_file_name)
19
19
  if exts_are_valid
20
20
  file = File.open(reader_file_name, 'r')
21
- Parser.parse_file file, writer_file_name
22
- puts "You have successfully transcribed #{reader_file_name}.precious into #{writer_file_name}.rb"
21
+ Parser2.parse_file file, writer_file_name
22
+ puts "You have successfully transcribed #{reader_file_name}.precious into #{writer_file_name}"
23
23
  file.close
24
24
  end
25
25
  end
@@ -11,7 +11,7 @@ class Parser
11
11
  SUBTRACTION_KEYWORDS = ['leaves the fellowship', 'stabs', 'banishes', 'steals']
12
12
  MULTIPLICATION_KEYWORDS = ['gives aid to', 'procreates', 'bolsters']
13
13
  DIVISION_KEYWORDS = ['decapitates', 'dismembers']
14
- COMPARISON_KEYWORDS = ['as', 'equal']
14
+ COMPARISON_KEYWORDS = ['same', 'equal']
15
15
  CONDITION_KEYWORDS = ['does', 'if', 'will']
16
16
  END_KEYWORDS = ['you shall not pass']
17
17
  TRUE_KEYWORDS = ['precious']
@@ -0,0 +1,222 @@
1
+ class Parser2
2
+ WRITER_FILE = 'output.rb'
3
+ # KEYWORDS
4
+ #LINE_KEYWORDS = [',', '.', '?', '!', ';']
5
+ PUT_KEYWORDS = ['bring forth the ring', 'says', 'screams', 'exclaims', 'sobbs', 'coughs']
6
+ COMMENT_KEYWORDS = ['second breakfast', 'wear the ring']
7
+ ASSIGNMENT_KEYWORDS = ['is', 'are', 'was', 'has']
8
+ INCREMENT_KEYWORDS = ['eats lembas bread', 'fortifies stronghold', 'rests', 'recieves Evenstar', 'reforges Narsil']
9
+ DECREMENT_KEYWORDS = ['runs out of lembas bread', 'lost', 'hunted by orcs']
10
+ ADDITION_KEYWORDS = ['joins', 'join', 'and', 'accompanies']
11
+ SUBTRACTION_KEYWORDS = ['leaves the fellowship', 'stabs', 'banishes', 'steals']
12
+ MULTIPLICATION_KEYWORDS = ['gives aid to', 'procreates', 'bolsters']
13
+ DIVISION_KEYWORDS = ['decapitates', 'dismembers']
14
+ COMPARISON_KEYWORDS = ['equal', 'same', 'similar']
15
+ CONDITION_KEYWORDS = ['does', 'if', 'will']
16
+ END_KEYWORDS = ['you shall not pass']
17
+ TRUE_KEYWORDS = ['precious']
18
+ LOOP_KEYWORDS = ['whilst', 'during the journey']
19
+ GREATER_THAN_KEYWORDS = ['stronger than', 'more']
20
+ LESS_THAN_KEYWORDS = ['weaker than', 'less']
21
+ FUNCTION_DEF_KEYWORDS = ['transcribe', 'tell a story']
22
+ FUNCTION_CALL_KEYWORDS = ["theyre taking the hobbits to"]
23
+ PARAM_KEYWORDS = ['with']
24
+ NEGATION_KEYWORDS = ['not']
25
+ CLASS_KEYWORDS = ['chapter']
26
+ OPERATOR_KEYWORDS = ['#', '+', '-', '=', '*', '/', '+=1', '-=1', 'puts', '==', 'if', 'end', 'true', 'while', '>', '<', '!', 'def', '(', ')', 'class']
27
+
28
+ 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]
29
+
30
+ 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}]
31
+
32
+ def self.parse_file(file, output_file_name)
33
+ str = ""
34
+ writer_file_name = output_file_name
35
+ file.each_with_index do |file, index|
36
+ file.each_line do |line|
37
+ line = parse_line(line, index)
38
+ str << (line + "\n")
39
+ end
40
+ end
41
+ write(str, writer_file_name)
42
+ end
43
+
44
+ def self.parse_line(line, index)
45
+ #error handeling
46
+ #ignore lines of length 1, its empty
47
+ if line.length == 1
48
+ # write("#{line}")
49
+ return line
50
+ else
51
+
52
+ #check if there are strings in line
53
+ quote = ""
54
+ if line.include? '"'
55
+ quote = get_quote(line)
56
+ line = line.gsub(quote, 'quote_placeholder')
57
+ end
58
+
59
+ #check if line is a comment
60
+ comment = ""
61
+ if COMMENT_KEYWORDS.any? { |word| line.include?(word) }
62
+ line = parse(line, COMMENT_KEYWORDS)
63
+ comment = store_important(line, '#')
64
+ line = line.gsub(comment, 'comment_placeholder')
65
+ end
66
+
67
+ #check if line has params
68
+ params = ""
69
+ if ((PARAM_KEYWORDS.any? { |word| line.include?(word) }) &&
70
+ ((FUNCTION_DEF_KEYWORDS.any? { |word| line.include?(word) }) ||
71
+ (FUNCTION_CALL_KEYWORDS.any? { |word| line.include?(word) })))
72
+ line = parse(line, PARAM_KEYWORDS)
73
+ params = remove_newline(store_important(line, '('))
74
+ line = line.gsub(params, 'param_placeholder ') + ')'
75
+ end
76
+
77
+ class_word = ""
78
+ if CLASS_KEYWORDS.any? { |word| line.include?(word) }
79
+ line = parse(line, CLASS_KEYWORDS)
80
+ class_word = store_important(line, ':')
81
+ line = line.gsub(class_word, ' class_word_placeholder')
82
+ end
83
+
84
+ #get rid of special chars
85
+ line_array = line.split(" ")
86
+ line_array.each_with_index do |word, index|
87
+ line_array[index] = purify(word)
88
+ end
89
+
90
+ #check for keywords
91
+ #calls if statements
92
+ line = line_array.join(" ")
93
+ line = check_for_keywords(line)
94
+
95
+ #remove extra english words
96
+ line = only_valuable_words(line).downcase
97
+
98
+ #put string back into line
99
+ if line.include? ('"quote_placeholder"')
100
+ line = line.gsub('quote_placeholder', quote)
101
+ end
102
+
103
+ if line.include? ('comment_placeholder')
104
+ line = line.gsub('comment_placeholder', comment)
105
+ end
106
+
107
+ if line.include? ('param_placeholder')
108
+ line = line.gsub('param_placeholder', params.downcase)
109
+ line = format_function(line)
110
+ end
111
+
112
+ if line.include? ('class_word_placeholder')
113
+ line = line.gsub('class_word_placeholder', "#{class_word}")
114
+ end
115
+
116
+ # write("#{line}")
117
+ return line
118
+ end
119
+ end
120
+
121
+ def self.check_for_keywords(line)
122
+ ALICIA_KEYS.each do |keywords|
123
+ line = parse(line, keywords)
124
+ end
125
+ line
126
+ end
127
+
128
+ # METHODS
129
+ def self.parse(line, keywords)
130
+ keywords.each do |keyword|
131
+ if line.include? keyword
132
+ puts keyword
133
+ replacement = find_replacement(keywords)
134
+ line = line.gsub(keyword, replacement)
135
+ end
136
+ end
137
+ return line
138
+ end
139
+
140
+ def self.remove_newline(line)
141
+ line.gsub(/\n/, "")
142
+ end
143
+
144
+ def self.only_valuable_words(line)
145
+ important_words = []
146
+ line_array = line.split(" ")
147
+ line_array.each do |word|
148
+ valuable_word = valuable?(word)
149
+ if valuable_word
150
+ important_words << word
151
+ end
152
+ end
153
+ line = important_words.join(" ")
154
+ end
155
+
156
+ def self.valuable?(word)
157
+ valuable = false
158
+ if /[[:upper:]]/.match(word[0]) #if variable
159
+ valuable = true
160
+ elsif word == '"quote_placeholder"' #if string/quote
161
+ valuable = true
162
+ elsif word == '#comment_placeholder' #if comment
163
+ valuable = true
164
+ elsif word == '(param_placeholder' #if params
165
+ valuable = true
166
+ elsif word == 'class_word_placeholder' #if class
167
+ valuable = true
168
+ elsif word.to_i.to_s == word #if num
169
+ valuable = true
170
+ elsif OPERATOR_KEYWORDS.include? word #if operator
171
+ valuable = true
172
+ end
173
+ valuable
174
+ end
175
+
176
+ def self.get_quote(phrase)
177
+ phrase_array = phrase.split('')
178
+ index = phrase_array.find_index { |i| i == '"'} + 1
179
+ quote = ""
180
+ while phrase_array[index] != '"'
181
+ quote << phrase_array[index]
182
+ index += 1
183
+ end
184
+ quote
185
+ end
186
+
187
+ def self.store_important(phrase, key)
188
+ phrase_array = phrase.split('')
189
+ index = phrase_array.find_index { |i| i == key} + 1
190
+ string = ""
191
+ while index < phrase_array.length
192
+ string << phrase_array[index]
193
+ index += 1
194
+ end
195
+ string
196
+ end
197
+
198
+ def self.find_replacement(keywords)
199
+ MAP.each do |hash|
200
+ hash.each do |key, value|
201
+ if value == keywords
202
+ return key.to_s
203
+ end
204
+ end
205
+ end
206
+ return nil
207
+ end
208
+
209
+ def self.format_function(line)
210
+ phrase_array = line.split('')
211
+ index = phrase_array.find_index { |i| i == "("}
212
+ phrase_array.delete_at(index - 1)
213
+ phrase_array.delete_at(index - 2)
214
+ phrase_array.join("")
215
+ end
216
+
217
+ def self.write(str, writer_file_name)
218
+ writer_file = File.open(writer_file_name, 'w')
219
+ writer_file.write(str)
220
+ end
221
+
222
+ end #end of Parser Class
@@ -1,3 +1,3 @@
1
1
  module MyPrecious
2
- VERSION = "0.1.9"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: my_precious
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jessicabettsftw
@@ -79,6 +79,7 @@ files:
79
79
  - lib/my_precious.rb
80
80
  - lib/my_precious/cli.rb
81
81
  - lib/my_precious/parser.rb
82
+ - lib/my_precious/parser_2.rb
82
83
  - lib/my_precious/version.rb
83
84
  - middle_earth.precious
84
85
  - my_precious.gemspec