-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathParser.java
More file actions
76 lines (57 loc) · 2.48 KB
/
Parser.java
File metadata and controls
76 lines (57 loc) · 2.48 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
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Parser {
private String groceryListData;
public Parser() {
this.groceryListData = loadFile();
}
private String loadFile(){
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("RawData.txt").getFile());
StringBuilder result = new StringBuilder("");
try(Scanner scanner = new Scanner(file)){
while(scanner.hasNextLine()){
String line = scanner.nextLine();
result.append(line).append("\n");
}
scanner.close();
}catch(IOException e){
e.printStackTrace();
}
return result.toString();
}
public String dataParser(){
String parsedString = "";
Pattern pattern = Pattern.compile("(?i)(name)");
Matcher matcher = pattern.matcher(loadFile());
parsedString = matcher.replaceAll("{name");
Pattern pattern1 = Pattern.compile("(?i)(##)");
Matcher matcher1 = pattern1.matcher(parsedString);
parsedString = matcher1.replaceAll("},\n");
Pattern cookies = Pattern.compile("(?i)(C[o0][o0]kies[;])");
Matcher matcherCookies = cookies.matcher(parsedString);
parsedString = matcherCookies.replaceAll("Cookies,");
Pattern milk = Pattern.compile("(?i)(milk[;])");
Matcher matcherMilk = milk.matcher(parsedString);
parsedString = matcherMilk.replaceAll("Milk,");
Pattern bread = Pattern.compile("(?i)(bread[;])");
Matcher matcherBread = bread.matcher(parsedString);
parsedString = matcherBread.replaceAll("Bread,");
Pattern apple = Pattern.compile("(?i)(apples)");
Matcher matcherApple = apple.matcher(parsedString);
parsedString = matcherApple.replaceAll("Apples");
Pattern pattern5 = Pattern.compile("(?i)[;,](price)");
Matcher matcher5 = pattern5.matcher(parsedString);
parsedString = matcher5.replaceAll(",price");
Pattern pattern6 = Pattern.compile("(?i)(;type)");
Matcher matcher6 = pattern6.matcher(parsedString);
parsedString = matcher6.replaceAll(",type");
Pattern pattern7 = Pattern.compile("(?i)(food)[!;@^*%]");
Matcher matcher7 = pattern7.matcher(parsedString);
parsedString = matcher7.replaceAll("Food,");
return parsedString;
}
}