Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / nlp / melt / SxPipe.groovy @ 1000

History | View | Annotate | Download (3.5 kB)

1

    
2
package org.txm.scripts.tal.melt
3

    
4
import java.io.File;
5
import java.io.BufferedReader;
6
import java.io.IOException;
7
import java.io.InputStreamReader;
8

    
9
import org.txm.tal.Tagger
10
import org.txm.tal.SlashFile2TTRez
11
import org.txm.utils.StreamHog;
12

    
13
class SxPipe {
14

    
15
        Process process = null;
16
        InputStream meltis
17
        OutputStream meltos
18
        boolean debug = false
19

    
20
        BufferedReader reader
21
        StringBuffer buffer = new StringBuffer();
22

    
23
        public SxPipe() {
24
        }
25

    
26
        public boolean start() {
27
                // Call Melt
28
                def args = [];
29
                args << "MElt"
30
                args << "-t"
31
                args << "-C"
32
                ProcessBuilder pb = new ProcessBuilder(args);
33
                try {
34
                        process = pb.start();
35
                } catch (IOException e) {
36
                        System.err.println(e);
37
                        return false
38
                }
39
                meltis = process.getInputStream();
40
                meltos = process.getOutputStream();
41

    
42
                new StreamHog(process.getErrorStream(), debug) // read Error Stream
43
                //new StreamHog(process.getInputStream(), true) // read Error Stream
44
                reader = new BufferedReader(new InputStreamReader(meltis));
45
                return true;
46
        }
47

    
48
        public boolean processText(String text) {
49
                if (debug) println "Send text: $text"
50
                process << text
51
                process << "\n"
52
                meltos.flush()
53
                return true;
54
        }
55

    
56
        public def readStream() {
57
                if (debug) println "reading lines..."
58

    
59
                try {
60
                        String line = null;
61
                        while ((line = reader.readLine()) != null) {
62
                                buffer << line
63
                                buffer << "\n"
64
                        }
65
                } catch (IOException e) {
66
                        org.txm.utils.logger.Log.printStackTrace(e);
67
                }
68

    
69
                return "end"
70
        }
71

    
72
        public boolean stop() {
73
                meltos.close()
74
                readStream()
75

    
76
                int e = 0;
77
                try {
78
                        e = process.waitFor();
79
                } catch (Exception err) {        }
80

    
81
                if (e != 0) {
82
                        System.err .println("Process exited abnormally with code " + e )
83
                }
84
        }
85

    
86
        public def getResult() {
87
                return buffer.toString()
88
                //return SlashFile2TTRez.processString(buffer.toString())
89
        }
90

    
91
        public void printResult() {
92
                def segs = getResult().split("\n")// \\{\\\$\\\$\\\$\\\$\\} \\\$\\\$\\\$\\\$
93

    
94
                int i = 0;
95
                for (def seg : segs) {
96
                        def tokens = []
97
                        for(def str :  seg.replaceAll("\\{", "\n{").tokenize("\n")) {
98
                                if (str.startsWith("{"))
99
                                        println str.substring(1, str.indexOf("}")).replace("_ACC_O", "{").replace("_ACC_F", "}")
100
                        }
101
                }
102
        }
103

    
104
        public def getResultTokens() {
105
                def segs = getResult().split("\n")
106
                def tokens = []
107

    
108
                int i = 0, idx = -1;
109
                for (def seg : segs) {
110
                        for(def str :  seg.replaceAll("\\{", "\n{").tokenize("\n")) {
111
                                if (str.startsWith("{")) {
112
                                        idx = str.indexOf("}")
113
                                        try{
114
                                                tokens << [str.substring(1, idx).replace("_ACC_O", "{").replace("_ACC_F", "}"),
115
                                                        str.substring(idx+1)]
116
                                        } catch(Exception e) {e.printStackTrace();println "STRING IS : '$str'"}
117
                                }
118
                        }
119

    
120
                }
121
                return tokens
122
        }
123

    
124
        public def getResultSegments() {
125
                def segs = getResult().split("\n")
126
                def segsFinal = []
127

    
128
                int i = 0, idx = -1;
129
                for (def seg : segs) {
130
                        def tokens = []
131
                        for(def str :  seg.replaceAll("\\{", "\n{").tokenize("\n")) {
132
                                if (str.startsWith("{")) {
133
                                        idx = str.indexOf("}")
134
                                        try{
135
                                                tokens << [str.substring(1, idx).replace("_ACC_O", "{").replace("_ACC_F", "}"),
136
                                                        str.substring(idx+1)]
137
                                        } catch(Exception e) {e.printStackTrace();println "STRING IS : '$str'"}
138
                                }
139
                        }
140
                        segsFinal << tokens
141
                }
142
                return segsFinal
143
        }
144

    
145

    
146
        public static void main(String[] args) {
147
                SxPipe melt = new SxPipe()
148
                melt.start()
149
                melt.processText("""Il y a {du} pain \$\$\$\$ c'├ętait bon. Y a-t-il assez ?
150
Un autre bout, de texte : fin.""")
151
                melt.stop()
152

    
153
                melt.printResult()
154

    
155

    
156

    
157
                //                println "\nMELT DONE\n"
158
                //                for (def r : melt.getResult()) {
159
                //                        for (def rr : r)
160
                //                                print rr+"\t"
161
                //                        println ""
162
                //                }
163
        }
164
}