-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormulaParser.java
More file actions
250 lines (235 loc) · 6.7 KB
/
FormulaParser.java
File metadata and controls
250 lines (235 loc) · 6.7 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.HashMap;
// Format of formulas: [(](shorthand|longhand)[count][)][count]
//
// shorthand is case sensitive and is the symbol notation (e.g. C for Carbon, Co for Cobalt and Hg for Lead)
// longhand is the full element name and is not case sensitive (e.g. Carbon, CARBON, CaRBoN, lead)
// If a count is left out, it's implicitly '1', else you can put a count after a shorthand or longhand element
// You can group element with parenthesis and apply counts to the groups. (e.g. C2(HO)2)
// Any other symbols are stripped from the formula (e.g. -, +). Leaving them in does no harm
//
// The output is a map from the capitalized element name as a string, to it's count
public class FormulaParser {
public static String symbols_map =
"H Hydrogen\n" +
"He Helium\n" +
"Li Lithium\n" +
"Be Beryllium\n" +
"B Boron\n" +
"C Carbon\n" +
"N Nitrogen\n" +
"O Oxygen\n" +
"F Fluorine\n" +
"Ne Neon\n" +
"Na Sodium\n" +
"Mg Magnesium\n" +
"Al Aluminum\n" +
"Si Silicon\n" +
"P Phosphorus\n" +
"S Sulfur\n" +
"Cl Chlorine\n" +
"Ar Argon\n" +
"K Potassium\n" +
"Ca Calcium\n" +
"Sc Scandium\n" +
"Ti Titanium\n" +
"V Vanadium\n" +
"Cr Chromium\n" +
"Mn Manganese\n" +
"Fe Iron\n" +
"Co Cobalt\n" +
"Ni Nickel\n" +
"Cu Copper\n" +
"Zn Zinc\n" +
"Ga Gallium\n" +
"Ge Germanium\n" +
"As Arsenic\n" +
"Se Selenium\n" +
"Br Bromine\n" +
"Kr Krypton\n" +
"Rb Rubidium\n" +
"Sr Strontium\n" +
"Y Yttrium\n" +
"Zr Zirconium\n" +
"Nb Niobium\n" +
"Mo Molybdenum\n" +
"Tc Technetium\n" +
"Ru Ruthenium\n" +
"Rh Rhodium\n" +
"Pd Palladium\n" +
"Ag Silver\n" +
"Cd Cadmium\n" +
"In Indium\n" +
"Sn Tin\n" +
"Sb Antimony\n" +
"Te Tellurium\n" +
"I Iodine\n" +
"Xe Xenon\n" +
"Cs Cesium\n" +
"Ba Barium\n" +
"La Lanthanum\n" +
"Ce Cerium\n" +
"Pr Praseodymium\n" +
"Nd Neodymium\n" +
"Pm Promethium\n" +
"Sm Samarium\n" +
"Eu Europium\n" +
"Gd Gadolinium\n" +
"Tb Terbium\n" +
"Dy Dysprosium\n" +
"Ho Holmium\n" +
"Er Erbium\n" +
"Tm Thulium\n" +
"Yb Ytterbium\n" +
"Lu Lutetium\n" +
"Hf Hafnium\n" +
"Ta Tantalum\n" +
"W Tungsten\n" +
"Re Rhenium\n" +
"Os Osmium\n" +
"Ir Iridium\n" +
"Pt Platinum\n" +
"Au Gold\n" +
"Hg Mercury\n" +
"Tl Thallium\n" +
"Pb Lead\n" +
"Bi Bismuth\n" +
"Po Polonium\n" +
"At Astatine\n" +
"Rn Radon\n" +
"Fr Francium\n" +
"Ra Radium\n" +
"Ac Actinium\n" +
"Th Thorium\n" +
"Pa Protactinium\n" +
"U Uranium\n" +
"Np Neptunium\n" +
"Pu Plutonium\n" +
"Am Americium\n" +
"Cm Curium\n" +
"Bk Berkelium\n" +
"Cf Californium\n" +
"Es Einsteinium\n" +
"Fm Fermium\n" +
"Md Mendelevium\n" +
"No Nobelium\n" +
"Lr Lawrencium\n" +
"Rf Rutherfordium\n" +
"Db Dubnium\n" +
"Sg Seaborgium\n" +
"Bh Bohrium\n" +
"Hs Hassium\n" +
"Mt Meitnerium\n" +
"Ds Darmstadtium\n" +
"Rg Roentgenium\n" +
"Cn Copernicium\n" +
"Uut Ununtrium\n" +
"Fl Flerovium\n" +
"Uup Ununpentium\n" +
"Lv Livermorium\n" +
"Uus Ununseptium\n" +
"Uuo Ununoctium";
public static Map<String,String> getSymbolMap(){
Map<String, String> element_map = new HashMap<>();
Scanner in = new Scanner(symbols_map);
while(in.hasNext()){
String shortform = in.next();
String longform = in.next();
element_map.put(shortform, longform);
element_map.put(longform.toUpperCase(), longform);
}
return element_map;
}
public static Map<String,Integer> parseFormulaString(String formula, Map<String,String> element_map){
Map<String,Integer> element_counts = new HashMap<>();
/* formula = formula.toUpperCase(); */
System.out.println(formula);
int len = formula.length();
for(int i = 0; i < len;){
boolean is_group = false;
if(formula.charAt(i) == '('){
i++;
is_group = true;
}
int repeat_count = 1;
Map<String,Integer> atoms_in_group = new HashMap<>();
do{
int start = i;
int restore_i = 0;
String element = null;
String restore_element = null;
while(i < len && Character.isLetter(formula.charAt(i))){
i++;
element = formula.substring(start, i);
/* System.out.println( element); */
String element_from_map = element_map.get(element);
if(element_from_map == null)
element_from_map = element_map.get(element.toUpperCase());
if(element_from_map != null){
/* System.out.println("element from map: " + element_from_map); */
restore_i = i;
restore_element = element_from_map;
}
}
if(restore_element != null){
i = restore_i;
element = restore_element;
}
if(element == null || element == ""){
System.out.println("Parse error: could not detect an element where one was expected in formula string.");
System.out.println("Remaining formula to parse: " + formula.substring(i));
System.exit(1);
}
start = i;
while(i < len && Character.isDigit(formula.charAt(i))){
i++;
}
int count;
try{
count = Integer.parseInt(formula.substring(start, i));
}catch(NumberFormatException e){
count = 1;
}
/* System.out.println("element: "+element); */
atoms_in_group.put(element, count);
if(i < len && formula.charAt(i) == ')'){
if(!is_group)
System.out.println("Parse error: unmatched parenthesis detected...");
i++;
is_group = false;
start = i;
while(i < len && Character.isDigit(formula.charAt(i)))
i++;
try{
repeat_count = Integer.parseInt(formula.substring(start, i));
}catch(NumberFormatException e){
repeat_count = 1;
}
}
}while(is_group == true);
for(String atom_type : atoms_in_group.keySet()){
int current_value = 0;
if(element_counts.containsKey(atom_type))
current_value = element_counts.get(atom_type);
element_counts.put(atom_type, current_value + atoms_in_group.get(atom_type) * repeat_count);
}
}
for(Map.Entry<String,Integer> entry : element_counts.entrySet()){
System.out.println(entry.getKey() + ": " + entry.getValue());
}
return element_counts;
}
public static final void main(String args[]) {
Map<String,String> element_map = getSymbolMap();
Scanner in = new Scanner(System.in);
System.out.print("Enter Chemical Formula: ");
String formula = in.nextLine();
formula = formula.replaceAll("[^a-zA-Z0-9()]", "");
if(formula.length() == 0)
formula = "CH4";
Map<String,Integer> element_counts = parseFormulaString(formula, element_map);
}
}