| 
									
										
										
										
											2023-06-29 21:46:58 +08:00
										 |  |  | import org.antlr.v4.runtime.*;
 | 
					
						
							|  |  |  | import org.antlr.v4.runtime.tree.*;
 | 
					
						
							| 
									
										
										
										
											2024-12-13 15:46:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-30 22:12:44 +08:00
										 |  |  | import java.io.FileOutputStream;
 | 
					
						
							|  |  |  | import java.io.FileInputStream;
 | 
					
						
							|  |  |  | import java.io.OutputStreamWriter;
 | 
					
						
							|  |  |  | import java.nio.charset.StandardCharsets;
 | 
					
						
							| 
									
										
										
										
											2023-07-13 22:12:02 +08:00
										 |  |  | import java.nio.charset.Charset;
 | 
					
						
							| 
									
										
										
										
											2023-06-29 21:46:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-08 23:33:57 +08:00
										 |  |  | public class MainRunner {
 | 
					
						
							| 
									
										
										
										
											2024-12-13 15:46:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-30 22:12:44 +08:00
										 |  |  | 	private static void printHelp() {
 | 
					
						
							| 
									
										
										
										
											2024-12-13 15:46:28 +08:00
										 |  |  | 		System.out.println("NlpParser Usage");
 | 
					
						
							| 
									
										
										
										
											2024-12-08 23:33:57 +08:00
										 |  |  | 		System.out.println("NlpParser <src> <dest>");
 | 
					
						
							| 
									
										
										
										
											2023-06-30 22:12:44 +08:00
										 |  |  | 		System.out.println();
 | 
					
						
							| 
									
										
										
										
											2024-12-13 15:46:28 +08:00
										 |  |  | 		System.out.println("<src>  - the decoded NLP text file.");
 | 
					
						
							| 
									
										
										
										
											2023-06-30 22:12:44 +08:00
										 |  |  | 		System.out.println("<dest> - the output json file.");
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2024-12-13 15:46:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	private static class UserRequest {
 | 
					
						
							|  |  |  | 		public UserRequest(String input_filepath, String output_filepath) {
 | 
					
						
							|  |  |  | 			this.mInputFilePath = input_filepath;
 | 
					
						
							|  |  |  | 			this.mOutputFilePath = output_filepath;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		String mInputFilePath;
 | 
					
						
							|  |  |  | 		String mOutputFilePath;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		public String getInputFilePath() {
 | 
					
						
							|  |  |  | 			return this.mInputFilePath;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		public String getOutputFilePath() {
 | 
					
						
							|  |  |  | 			return this.mOutputFilePath;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	private static UserRequest resolveArguments(String[] args) throws Exception {
 | 
					
						
							|  |  |  | 		// Check parameter
 | 
					
						
							| 
									
										
										
										
											2023-06-30 22:12:44 +08:00
										 |  |  | 		if (args.length != 2) {
 | 
					
						
							| 
									
										
										
										
											2024-12-13 15:46:28 +08:00
										 |  |  | 			throw new Exception("Invalid arguments count!");
 | 
					
						
							| 
									
										
										
										
											2023-06-30 22:12:44 +08:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2024-12-13 15:46:28 +08:00
										 |  |  | 		// Return fetched argumnts
 | 
					
						
							|  |  |  | 		return new UserRequest(args[0], args[1]);
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	private static void executeWorker(UserRequest user_request) throws Exception {
 | 
					
						
							|  |  |  | 		// Use try-with-resources to safely manage file stream.
 | 
					
						
							|  |  |  | 		try (FileInputStream fin = new FileInputStream(user_request.getInputFilePath());
 | 
					
						
							|  |  |  | 				FileOutputStream fout = new FileOutputStream(user_request.getOutputFilePath());
 | 
					
						
							|  |  |  | 				OutputStreamWriter fw = new OutputStreamWriter(fout, StandardCharsets.UTF_8);) {
 | 
					
						
							|  |  |  | 			// Start lex and parse
 | 
					
						
							|  |  |  | 			CharStream input = CharStreams.fromStream(fin, Charset.forName("windows-1252"));
 | 
					
						
							|  |  |  | 			NlpLexer lexer = new NlpLexer(input);
 | 
					
						
							|  |  |  | 			CommonTokenStream tokens = new CommonTokenStream(lexer);
 | 
					
						
							|  |  |  | 			NlpParser parser = new NlpParser(tokens);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Walk tree to build json
 | 
					
						
							|  |  |  | 			ParseTree tree = parser.document();
 | 
					
						
							|  |  |  | 			ParseTreeWalker walker = new ParseTreeWalker();
 | 
					
						
							|  |  |  | 			JsonConverter converter = new JsonConverter();
 | 
					
						
							|  |  |  | 			walker.walk(converter, tree);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Write json
 | 
					
						
							|  |  |  | 			fw.write(converter.buildJsonString());
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public static void main(String[] args) throws Exception {
 | 
					
						
							|  |  |  | 		// Check argument
 | 
					
						
							|  |  |  | 		UserRequest user_request = null;
 | 
					
						
							| 
									
										
										
										
											2023-06-30 22:12:44 +08:00
										 |  |  | 		try {
 | 
					
						
							| 
									
										
										
										
											2024-12-13 15:46:28 +08:00
										 |  |  | 			user_request = resolveArguments(args);
 | 
					
						
							| 
									
										
										
										
											2023-06-30 22:12:44 +08:00
										 |  |  | 		} catch (Exception e) {
 | 
					
						
							| 
									
										
										
										
											2024-12-13 15:46:28 +08:00
										 |  |  | 			System.out.print("[Argument Error] ");
 | 
					
						
							|  |  |  | 			System.out.println(e.getMessage());
 | 
					
						
							| 
									
										
										
										
											2023-06-30 22:12:44 +08:00
										 |  |  | 			printHelp();
 | 
					
						
							| 
									
										
										
										
											2024-12-13 15:46:28 +08:00
										 |  |  | 			return;
 | 
					
						
							| 
									
										
										
										
											2023-06-30 22:12:44 +08:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-13 15:46:28 +08:00
										 |  |  | 		// Call converter
 | 
					
						
							|  |  |  | 		try {
 | 
					
						
							|  |  |  | 			executeWorker(user_request);
 | 
					
						
							|  |  |  | 		} catch (Exception e) {
 | 
					
						
							|  |  |  | 			System.out.print("[Converter Error] ");
 | 
					
						
							|  |  |  | 			System.out.println(e.getMessage());
 | 
					
						
							|  |  |  | 			return;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2023-06-29 21:46:58 +08:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 |