Instances.java 10.2 KB
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
package is2.data;

import java.util.Arrays;
import java.util.BitSet;

public class Instances {

	public IEncoder m_encoder;

	protected int size = 0;

	protected int capacity;

	public int[][] forms;

	public int[][] plemmas;
	public int[][] glemmas;

	public short[][] heads;
	public short[][] pheads;

	public short[][] labels;
	public short[][] plabels;

	public short[][] gpos;
	public short[][] pposs;

	public short[][][] feats;

	public int[][] predicat;

	public short[][] predicateId;

	public short[][] semposition;

	public short[][][] arg;

	public short[][][] argposition;

	public BitSet[] pfill;

	public short[][] gfeats;
	public short[][] pfeats;

	public Instances() {
	}

	public static int m_unkown = 0;
	public static int m_count = 0;

	public static boolean m_report;
	public static boolean m_found = false;

	final public void setForm(int i, int p, String x) {

		forms[i][p] = m_encoder.getValue(PipeGen.WORD, x);
		if (forms[i][p] == -1) {
			if (m_report)
				System.out.println("unkwrd " + x);
			m_unkown++;
			m_found = true;
		}
		m_count++;
	}

	final public void setRel(int i, int p, String x) {
		labels[i][p] = (short) m_encoder.getValue(PipeGen.REL, x);

	}

	final public void setHead(int i, int c, int p) {
		heads[i][c] = (short) p;
	}

	final public int size() {
		return size;
	}

	public void setSize(int n) {
		size = n;
	}

	public void init(int ic, IEncoder mf) {
		init(ic, mf, -1);
	}

	public void init(int ic, IEncoder mf, int version) {
		capacity = ic;
		m_encoder = mf;

		forms = new int[capacity][];
		plemmas = new int[capacity][];
		glemmas = new int[capacity][];
		pposs = new short[capacity][];

		gpos = new short[capacity][];
		labels = new short[capacity][];
		heads = new short[capacity][];
		plabels = new short[capacity][];
		pheads = new short[capacity][];
		feats = new short[capacity][][];
		gfeats = new short[capacity][];
		pfeats = new short[capacity][];

		predicat = new int[ic][];
		predicateId = new short[ic][];
		semposition = new short[ic][];
		arg = new short[ic][][];
		argposition = new short[ic][][];

		pfill = new BitSet[ic];
	}

	public int length(int i) {
		return forms[i].length;
	}

	public int createInstance09(int length) {

		forms[size] = new int[length];
		plemmas[size] = new int[length];
		glemmas[size] = new int[length];

		pposs[size] = new short[length];

		gpos[size] = new short[length];

		labels[size] = new short[length];
		heads[size] = new short[length];

		this.pfill[size] = new BitSet(length);

		feats[size] = new short[length][];
		gfeats[size] = new short[length];
		pfeats[size] = new short[length];
		plabels[size] = new short[length];
		pheads[size] = new short[length];

		size++;

		return size - 1;

	}

	/*
	 * public final void setPPos(int i, int p, String x) { ppos[i][p] =
	 * (short)m_encoder.getValue(PipeGen.POS,x);
	 * 
	 * }
	 */

	public final void setPPoss(int i, int p, String x) {
		pposs[i][p] = (short) m_encoder.getValue(PipeGen.POS, x);

	}

	public final void setGPos(int i, int p, String x) {
		gpos[i][p] = (short) m_encoder.getValue(PipeGen.POS, x);
	}

	public void setLemma(int i, int p, String x) {
		plemmas[i][p] = m_encoder.getValue(PipeGen.WORD, x);
	}

	public void setGLemma(int i, int p, String x) {
		glemmas[i][p] = m_encoder.getValue(PipeGen.WORD, x);
	}

	public void setFeats(int i, int p, String[] fts) {
		if (fts == null) {
			feats[i][p] = null;
			return;
		}
		feats[i][p] = new short[fts.length];

		for (int k = 0; k < fts.length; k++) {
			feats[i][p][k] = (short) m_encoder.getValue(PipeGen.FEAT, fts[k]);
		}

	}

	public void setFeature(int i, int p, String feature) {
		if (feature == null)
			return;
		this.gfeats[i][p] = (short) m_encoder.getValue(PipeGen.FFEATS, feature);
		/*
		 * if (gfeats[i][p]==-1) { System.out.println("+"+feature); new
		 * Exception().printStackTrace(); System.exit(0); }
		 */
	}

	public void setPFeature(int i, int p, String feature) {
		if (feature == null)
			return;
		this.pfeats[i][p] = (short) m_encoder.getValue(PipeGen.FFEATS, feature);
	}

	public int getWValue(String v) {
		return m_encoder.getValue(PipeGen.WORD, v);
	}

	public final void setPRel(int i, int p, String x) {
		plabels[i][p] = (short) m_encoder.getValue(PipeGen.REL, x);
	}

	public final void setPHead(int i, int c, int p) {
		pheads[i][c] = (short) p;
	}

	/*
	 * public String toString(int c) { StringBuffer s = new StringBuffer();
	 * for(int i=0;i<length(c);i++) {
	 * s.append(i).append('\t').append(forms[c][i]).append("\t_\t").append(ppos[
	 * c][i]).append('\t'). append('\t').append(heads[c][i]).append('\n'); }
	 * 
	 * return s.toString(); }
	 */

	/*
	 * public void setPos(int i, int p, String x) { ppos[i][p] =
	 * (short)m_encoder.getValue(PipeGen.POS,x);
	 * 
	 * }
	 */

	/**
	 * Create the semantic representation
	 * 
	 * @param inst
	 * @param it
	 * @return
	 */
	public boolean createSem(int inst, SentenceData09 it) {

		boolean error = false;

		if (it.sem == null)
			return error;

		predicat[inst] = new int[it.sem.length];
		semposition[inst] = new short[it.sem.length];
		predicateId[inst] = new short[it.sem.length];

		if (it.sem != null) {
			arg[inst] = new short[it.sem.length][];
			argposition[inst] = new short[it.sem.length][];
		}
		if (it.sem == null)
			return error;

		// init sems

		for (int i = 0; i < it.sem.length; i++) {

			String pred;
			short predSense = 0;
			if (it.sem[i].indexOf('.') > 0) {
				pred = it.sem[i].substring(0, it.sem[i].indexOf('.'));
				predSense = (short) m_encoder.getValue(PipeGen.SENSE,
						it.sem[i].substring(it.sem[i].indexOf('.') + 1, it.sem[i].length()));
				// Short.parseShort(it.sem[i].substring(it.sem[i].indexOf('.')+1,
				// it.sem[i].length()));
			} else {
				pred = it.sem[i];
				predSense = (short) m_encoder.getValue(PipeGen.SENSE, "");
			}

			predicat[inst][i] = m_encoder.getValue(PipeGen.PRED, pred);
			predicateId[inst][i] = predSense;

			semposition[inst][i] = (short) it.semposition[i];

			// this can happen too when no arguments have values
			if (it.arg == null) {
				// DB.println("error arg == null "+i+" sem"+it.sem[i]+" inst
				// number "+inst);
				// error =true;
				continue;
			}

			// last pred(s) might have no argument
			if (it.arg.length <= i) {
				// DB.println("error in instance "+inst+" argument list and
				// number of predicates different arg lists: "+it.arg.length+"
				// preds "+sem.length);
				// error =true;
				continue;
			}

			// this happens from time to time, if the predicate has no arguments
			if (it.arg[i] == null) {
				// DB.println("error no args for pred "+i+" "+it.sem[i]+" length
				// "+it.ppos.length);
				// error =true;
				continue;
			}

			int argCount = it.arg[i].length;
			arg[inst][i] = new short[it.arg[i].length];
			argposition[inst][i] = new short[it.arg[i].length];

			// add the content of the argument
			for (int a = 0; a < argCount; a++) {
				arg[inst][i][a] = (short) m_encoder.getValue(PipeGen.ARG, it.arg[i][a]);
				argposition[inst][i][a] = (short) it.argposition[i][a];

				// System.out.print(" #"+a+" pos: "+argposition[inst][i][a]+"
				// "+it.arg[i][a]+" ");
			}
			// System.out.println("");

		}

		return error;

	}

	public int predCount(int n) {
		return pfill[n].cardinality();
	}

	public void print2DIntArray(int[][] a, String name) {
		for(int i = 0; i < a.length; ++i)
			if(a[i] != null)
				for(int j = 0; j < a[i].length; ++j)
					System.out.println(name + "[" + i + "][" + j + "] = " + a[i][j]);
	}
	
	public void print2DShortArray(short[][] a, String name) {
		for(int i = 0; i < a.length; ++i)
			if(a[i] != null)
				for(int j = 0; j < a[i].length; ++j)
					System.out.println(name + "[" + i + "][" + j + "] = " + a[i][j]);
	}
	
	public void print3DShortArray(short[][][] a, String name) {
		for(int i = 0; i < a.length; ++i)
			if(a[i] != null)
				for(int j = 0; j < a[i].length; ++j)
					if(a[i][j] != null)
						System.out.println(name + "[" + i + "][" + j + "] = " + Arrays.toString(a[i][j]));
	}
	
	public void print() {
		System.out.println("-----m_encoder-----");
		m_encoder.print();
		System.out.println("-------------------");
		
		System.out.println("size = " + size);
		System.out.println("capacity = " + capacity);
		
		print2DIntArray(forms, "forms");
		print2DIntArray(plemmas, "plemmas");
		print2DIntArray(glemmas, "glemmas");
		print2DShortArray(heads, "heads");
		print2DShortArray(pheads, "pheads");
		print2DShortArray(labels, "labels");
		print2DShortArray(plabels, "plabels");
		print2DShortArray(gpos, "gpos");
		print2DShortArray(pposs, "pposs");
		print3DShortArray(feats, "feats");
		print2DIntArray(predicat, "predicat");
		print2DShortArray(predicateId, "predicateId");
		print2DShortArray(semposition, "semposition");
		print3DShortArray(arg, "arg");
		print3DShortArray(argposition, "argposition");
		for(int i = 0; i < pfill.length; ++i)
			System.out.println("pfill [" + i + "] = " + pfill[i]);
		print2DShortArray(gfeats, "gfeats");
		print2DShortArray(pfeats, "pfeats");
		
		System.out.println("m_unkown = " + m_unkown);
		System.out.println("m_count = " + m_count);
		System.out.println("m_report = " + m_report);
		System.out.println("m_found = " + m_found);
	}
	
	/**
	 * @param pscnt
	 * @return
	 */
	public String print(int pscnt) {
		StringBuilder s = new StringBuilder();

		for (int i = 0; i < this.length(pscnt); i++) {
			s.append(i + "\t" + forms[pscnt][i] + "\t" + this.glemmas[pscnt][i] + "\t" + this.plemmas[pscnt][i] + "\t"
					+ this.gpos[pscnt][i] + "\t" + this.pposs[pscnt][i] + "\t" + this.gfeats[pscnt][i] + "\t"
					+ (this.feats[pscnt][i] != null && this.feats[pscnt][i].length > 0 ? this.feats[pscnt][i][0] : null)
					+ "\t l " + (labels[pscnt] != null && labels[pscnt].length > i ? labels[pscnt][i] : null) + "\t"
					+ "\t" + heads[pscnt][i] + "\t"
					+ (plabels[pscnt] != null && plabels[pscnt].length > i ? plabels[pscnt][i] : null) + "\t"
					+ this.predicat[pscnt][i] + "\n");
		}
		return s.toString();
	}

	public String print1(int pscnt) {
		StringBuilder s = new StringBuilder();

		for (int i = 0; i < this.length(pscnt); i++) {
			s.append(i + "\t" + forms[pscnt][i] + "\t" + "\t" + this.plemmas[pscnt][i] + "\t" + +this.pposs[pscnt][i]
					+ "\t l " + (labels[pscnt] != null && labels[pscnt].length > i ? labels[pscnt][i] : null) + "\t"
					+ "\t" + heads[pscnt][i] + "\t"
					+ (plabels[pscnt] != null && plabels[pscnt].length > i ? plabels[pscnt][i] : null) + "\n");
		}
		return s.toString();
	}
}