forked from cgirardi/newsparser
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTextproParser.java
88 lines (73 loc) · 2.88 KB
/
TextproParser.java
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
import java.io.*;
import java.util.*;
import fbk.hlt.parsing.CGTDocumentInterface;
/**
* Author: Christian Girardi (cgirardi@fbk.eu)
* Date: 04-apr-2013
*/
public class TextproParser implements CGTDocumentInterface {
static String TEXTPRO_PATH = null;
public TextproParser() {
try {
//load the TextPro home dir path from the file ./conf/textpro.properties
Properties properties = new Properties();
properties.load(new FileInputStream("./conf/textpro.properties"));
TEXTPRO_PATH = properties.getProperty("textpropath");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public LinkedHashMap getHeader(String url, String text, LinkedHashMap header) {
return null;
}
public String getBody(String url, String text, LinkedHashMap header) {
if (text != null && text.length() > 0) {
File tmpFile = new File("/tmp/aa");
OutputStreamWriter out = null;
try {
// write the input
out = new OutputStreamWriter(new FileOutputStream(tmpFile), "UTF8");
out.write(text);
out.close();
// run TextPro
String[] CONFIG = {"TEXTPRO=" + TEXTPRO_PATH, "PATH=" + "/usr/bin/" + ":."};
String[] cmd = {"/bin/tcsh", "-c", "perl " + TEXTPRO_PATH + "/textpro.pl -l eng -y "+tmpFile};
Process process = run(cmd, CONFIG);
process.waitFor();
//read the TextPro's output
BufferedReader txpFile = new BufferedReader (new InputStreamReader (new FileInputStream (tmpFile.getCanonicalPath() + ".txp")));
StringBuilder result = new StringBuilder();
String line;
while ((line = txpFile.readLine()) != null) {
result.append(line).append("\n");
}
txpFile.close();
return result.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "";
}
/** Runs executable command
* @param command
* @param config the configuration setting
* @exception IOException
*/
private Process run(String[] command, String[] config) throws IOException {
try {
Runtime rt = Runtime.getRuntime();
return rt.exec(command, config);
} catch(Exception e) {
throw new IOException("Run process error: " + e.getMessage());
}
}
}