1 package oeg.tagger.main;
3 import edu.stanford.nlp.util.logging.RedwoodConfiguration;
4 import java.io.BufferedWriter;
6 import java.io.FileOutputStream;
7 import java.io.FileReader;
8 import java.io.OutputStream;
9 import java.io.OutputStreamWriter;
10 import java.text.DateFormat;
11 import java.text.SimpleDateFormat;
12 import java.util.Arrays;
13 import java.util.Calendar;
14 import java.util.Date;
16 import org.apache.commons.cli.BasicParser;
17 import org.apache.commons.cli.CommandLine;
18 import org.apache.commons.cli.CommandLineParser;
19 import org.apache.commons.cli.HelpFormatter;
20 import org.apache.commons.cli.Options;
21 import org.apache.commons.io.FileUtils;
22 import java.io.PrintStream;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
27 import org.apache.maven.model.Model;
28 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
38 static final Logger logger = Logger.getLogger(
Main.class.getName());
39 static boolean logs =
false;
40 static String lang =
"es";
41 static String domain =
"standard";
42 static String date =
"";
43 static String format =
"timeml";
44 static String outpfilename =
null;
46 public static void main(String[] args) {
49 PrintStream systemerr = System.err;
53 if (args.length != 0) {
56 System.out.println(res);
60 System.setErr(systemerr);
63 public static void init(String[] args) {
64 logs = Arrays.asList(args).contains(
"-logs");
70 MavenXpp3Reader reader =
new MavenXpp3Reader();
71 Model model = reader.read(
new FileReader(
"pom.xml"));
72 String welcome = model.getArtifactId() +
" " + model.getVersion() +
"\n-----------------------------------------------------\n";
74 }
catch (Exception e) {
79 public static String
parsear(String[] args) {
81 StringBuilder res =
new StringBuilder();
82 CommandLineParser parser =
null;
83 CommandLine cmd =
null;
86 Options options =
new Options();
87 options.addOption(
"nologs",
false,
"OPTION to disables logs");
88 options.addOption(
"logs",
false,
"OPTION to enable logs");
89 options.addOption(
"lang",
true,
"OPTION to change language [ES, EN] (Spanish by default)");
90 options.addOption(
"domain",
true,
"OPTION to change domain [standard, legal] (Standard by default)");
91 options.addOption(
"date",
true,
"OPTION to add an anchor date in the format yyyy-mm-dd (today by default)");
92 options.addOption(
"text",
true,
"COMMAND to parse a text");
93 options.addOption(
"f",
true,
"COMMAND to parse a file");
94 options.addOption(
"outf",
true,
"COMMAND to save the output to a file");
95 options.addOption(
"format",
true,
"COMMAND to choose the output format [timeml,json,nif] (TimeML by default)");
96 options.addOption(
"help",
false,
"COMMAND to show help (Help)");
97 parser =
new BasicParser();
98 cmd = parser.parse(options, args);
101 if (cmd.hasOption(
"help") || args.length == 0) {
102 new HelpFormatter().printHelp(
Main.class.getCanonicalName(), options);
104 if (cmd.hasOption(
"lang")) {
105 lang = cmd.getOptionValue(
"lang");
107 if (cmd.hasOption(
"domain")) {
108 domain = cmd.getOptionValue(
"domain");
113 if (cmd.hasOption(
"date")) {
114 date = cmd.getOptionValue(
"date");
116 if (cmd.hasOption(
"format")) {
117 format = cmd.getOptionValue(
"format");
119 if (cmd.hasOption(
"f")) {
120 String filename = cmd.getOptionValue(
"f");
121 logger.info(
"Trying to parse the file " + filename);
122 outp =
parse(filename);
124 if (cmd.hasOption(
"text")) {
125 String text = cmd.getOptionValue(
"text");
126 logger.info(
"Trying to process the text " + text);
129 if(cmd.hasOption(
"outf")){
130 outpfilename = cmd.getOptionValue(
"outf");
132 logger.warning(
"Error while writing.");
134 logger.info(
"Output correctly written to " + outpfilename);
139 System.out.println(
"\n----------------\n");
141 System.out.println(outp);
143 System.out.println(
"\n----------------\n");
147 }
catch (Exception e) {
148 System.out.println(e.toString());
151 return res.toString();
154 public static String
parse(String filename) {
157 File f =
new File(filename);
158 logger.info(
"parsing the folder " + filename);
159 String input = FileUtils.readFileToString(f,
"UTF-8");
162 }
catch (Exception e) {
163 logger.warning(
"error opening file");
166 logger.info(
"Parsing correct\n\n");
174 if (!date.matches(
"\\d\\d\\d\\d-(1[012]|0\\d)-(3[01]|[012]\\d)"))
176 logger.info(
"No correct date provided, ");
177 dct = Calendar.getInstance().getTime();
178 DateFormat df =
new SimpleDateFormat(
"yyyy-MM-dd");
179 date = df.format(dct);
184 if(domain.equalsIgnoreCase(
"standard")){
185 if(lang.equalsIgnoreCase(
"ES")){
189 else if(lang.equalsIgnoreCase(
"EN")){
193 logger.warning(
"error in language; for now, just available ES and EN");
196 }
else if(domain.equalsIgnoreCase(
"legal")){
197 if(lang.equalsIgnoreCase(
"ES")){
201 else if(lang.equalsIgnoreCase(
"EN")){
205 logger.warning(
"error in language; for now, just available ES and EN");
209 logger.warning(
"error in domain; for now, just available standard and legal");
213 if(format.equalsIgnoreCase(
"timeml")){
214 res = annotador.
annotate(txt, date);
215 }
else if(format.equalsIgnoreCase(
"nif")){
216 res = annotador.
annotateNIF(txt, date,
"ref", lang);
217 }
else if(format.equalsIgnoreCase(
"json")){
220 logger.warning(
"Incorrect format; TimeML will be used.");
221 res = annotador.
annotate(txt, date);
224 }
catch (Exception e) {
225 logger.warning(
"error processing text:\n" + res);
228 logger.info(
"Text processing correct:\n" + res);
237 initLoggerDisabled();
246 private static void initLoggerDisabled() {
247 Logger.getLogger(
"").setLevel(Level.FINEST);
257 PrintStream falseerr =
new PrintStream(
new OutputStream(){
258 public void write(
int b) {
261 System.setErr(falseerr);
264 RedwoodConfiguration.current().clear().apply();
284 private static void initLoggerDebug() {
328 public static boolean writeFile(String input, String path) {
330 FileOutputStream fos =
new FileOutputStream(path);
331 OutputStreamWriter w =
new OutputStreamWriter(fos,
"UTF-8");
332 BufferedWriter bw =
new BufferedWriter(w);
337 }
catch (Exception ex) {
338 java.util.logging.Logger.getLogger(
Annotador.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);