Révision 3626

TXM/trunk/org.txm.groovy.core/src/groovy/org/txm/macro/annotation/InjectWordPropTableMacro.groovy (revision 3626)
27 27
@Field @Option(name="debug", usage="Debug de the macro", widget="Boolean", required=false, def="false")
28 28
def debug
29 29

  
30
if (!ParametersDialog.open(this)) return;
30
if (!ParametersDialog.open(this)) return false;
31 31

  
32 32
def split = properties.split(",")
33 33
if (split.length == 0) {
34
	println "ERROR: no property given"
34
	println "** no property given"
35 35
	return false
36 36
}
37 37
properties = []
......
45 45

  
46 46
outputDirectory.mkdir()
47 47
if (!outputDirectory.exists()) {
48
	println "ERROR: could not create or read $outputDirectory directory"
48
	println "** Could not create or read $outputDirectory directory"
49 49
	return false
50 50
}
51 51

  
......
57 57
def header = Arrays.asList(records.getHeaders())
58 58
if (debug) println "CSV header: $header"
59 59
for (def s : properties) if (!header.contains(s)) {
60
	println "Missing annotation property : $s"
61
	return;
60
	println "** Missing annotation property: $s"
61
	return false;
62 62
}
63
for (def s : properties) {
64
	def s2 = AsciiUtils.buildAttributeId(s);
65
	 
66
	if (!s.equals(s2)) {
67
		println "** Wrong property name format: '$s'. Use '$s2' instead."
68
		return false;
69
	}
70
}
63 71
for (def s : ["text_id", "id"]) if (!header.contains(s)) {
64
	println "Missing annotation property : $s"
65
	return;
72
	println "** Missing mandatory property: $s"
73
	return false;
66 74
}
67 75

  
68 76
def injections = [:]
69 77
int no = 1;
70 78
while (records.readRecord()) {
79
	
71 80
	String textid = records.get("text_id")
72
	if (!injections.containsKey(textid)) injections[textid] = [:]
81
	String wid = records.get("id")
73 82
	
74
	def props = [:]
75
	for (def s : properties) props[s] = records.get(s)
76
	if (records.get("id") == "") {
77
		 print "ERROR in record "+no
83
	if (wid == "") {
84
		 println "ERROR in record n="+no+": 'id' is empty"
85
	} else if (textid == "") {
86
		 println "ERROR in record n="+no+": no 'text_id' is empty"
78 87
	} else {
79
		injections[textid][records.get("id")] = props
88
		if (!injections.containsKey(textid)) injections[textid] = [:]
89
		
90
		def props = [:]
91
		for (def s : properties) {
92
			props[s] = records.get(s)
93
		}
94
		injections[textid][wid] = props
80 95
	}
81 96
	no++;
82 97
}
......
123 138
cpb.done()
124 139

  
125 140
println "New XML-TXM files are saved in ${outputDirectory}."
141
return true;
TXM/trunk/org.txm.groovy.core/src/groovy/org/txm/macro/annotation/BuildWordPropTableMacro.groovy (revision 3626)
49 49

  
50 50
CQPCorpus corpus = corpusViewSelection
51 51

  
52

  
53 52
@Field @Option(name="properties", usage="columns to inject separated by commas", widget="String", required=true, def="p1, p2, ... , pn")
54 53
		def properties
55 54

  
......
127 126
		if (ref.contains("_")) {
128 127
			def split2 = ref.split("_",2)
129 128
			def refp = corpus.getStructuralUnit(split2[0].trim()).getProperty(split2[1].trim())
130
			if (ref != "text_id")
129
			if (ref != "text_id") {
131 130
				referencePattern.addProperty(refp)
131
			}
132 132
			refProperties << refp
133 133
		} else {
134 134
			def p = corpus.getProperty(ref)
135
			if (p != null) referencePattern.addProperty(p)
135
			if (p != null) {
136
				referencePattern.addProperty(p)
137
			}
136 138
		}
137
	} catch(Exception e) { println "Error while parsing references: "+ref+" "+e}
139
	} catch(Exception e) { 
140
		println "Error while parsing references: "+ref+" "+e
141
		return false;
142
	}
138 143
}
139 144

  
140 145
// compute the concordance with contexts of 15 words on each side of the keyword
......
161 166
int chunk = 1000;
162 167
for (int i = 0 ; i < concordance.getNLines() ; i += chunk) {
163 168
	List<Line> lines
164
	if (i+chunk > NLines)
169
	if (i+chunk > NLines) {
165 170
		lines = concordance.getLines(i, concordance.getNLines()-1)
166
	else
171
	} else {
167 172
		lines = concordance.getLines(i, i+chunk-1) // e.g. from 0 to 999
168

  
173
	}
174
	
169 175
	int n = 1
170 176
	for (Line l : lines) {
171 177
		String ntext = l.getViewRef().getValue(text_id)
......
174 180
		String rcontext = l.rightContextToString()
175 181
		String ids = l.getViewRef().getValue(id)
176 182
		String refValue = "";
177
		for (def refp : refProperties) refValue += " "+l.getViewRef().getValue(refp)
183
		for (def refp : refProperties) {
184
			refValue += " "+l.getViewRef().getValue(refp)
185
		}
178 186

  
179 187
		String poss = "";
180 188
		for (def annot : annots) {
......
190 198
writer.close()
191 199

  
192 200
concordance.delete()
201

  
202
return true
TXM/trunk/org.txm.groovy.core/src/groovy/org/txm/macro/annotation/ExportWordPropertiesToTableMacro.groovy (revision 3626)
134 134
			def p = corpus.getProperty(ref)
135 135
			if (p != null) referencePattern.addProperty(p)
136 136
		}
137
	} catch(Exception e) { println "Error while parsing references: "+ref+" "+e}
137
	} catch(Exception e) { 
138
		println "Error while parsing references: "+ref+" "+e
139
		return false;
140
	}
138 141
}
139 142

  
140 143
// compute the concordance with contexts of 15 words on each side of the keyword
......
190 193
writer.close()
191 194

  
192 195
concordance.delete()
196
return true
TXM/trunk/org.txm.groovy.core/src/groovy/org/txm/macro/annotation/ImportWordPropertiesFromTableMacro.groovy (revision 3626)
22 22
def debug
23 23

  
24 24
if (!(corpusViewSelection instanceof MainCorpus)) {
25
	
25
	println "Error: selection must be a corpus (not: $corpusViewSelection)"
26 26
	return;
27 27
}
28 28

  
29
if (!ParametersDialog.open(this)) return;
29
if (!ParametersDialog.open(this)) return false;
30 30

  
31 31
println "Importing the $properties properties in the $corpusViewSelection corpus"
32 32

  
33 33
File inputDirectory = new File(corpusViewSelection.getParent().getProjectDirectory(), "txm/"+corpusViewSelection.getName())
34 34

  
35 35
// insert the section in the TRS files
36
gse.runMacro(InjectWordPropTableMacro, ["inputDirectory": inputDirectory
36
def ret = gse.runMacro(InjectWordPropTableMacro, ["inputDirectory": inputDirectory
37 37
	, "outputDirectory": inputDirectory
38 38
	, "csvFile": csvFile
39 39
	, "properties": properties
40 40
	, "debug":debug] ,
41 41
)
42 42

  
43
org.txm.rcp.commands.workspace.UpdateCorpus.update(corpusViewSelection, update_edition)
43
if (ret) {
44
	org.txm.rcp.commands.workspace.UpdateCorpus.update(corpusViewSelection, update_edition)
45
	return true
46
} else {
47
	println "** Aborting. See above for errors."
48
	return false
49
}
50

  
51

  

Formats disponibles : Unified diff