SentenceData09.java 11.9 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
package is2.data;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.StringWriter;
import java.util.ArrayList;

import is2.io.CONLLWriter09;
import is2.io.IOGenerals;

public class SentenceData09 {

	public String[] id;
	public String[] forms;

	public String[] lemmas;
	public String[] plemmas;

	public int[] heads;
	public int[] pheads;

	public String[] labels;
	public String[] plabels;

	public String[] gpos; // gold pos
	public String[] ppos;

	public String feats[][];
	// public String[] split_lemma;

	public String[] sem;
	public int[] semposition;

	// predicate number, argument number -> argument string
	public String[][] arg;
	public int[][] argposition;

	public String[] fillp;

	public String[] ofeats;
	public String[] pfeats;

	public SentenceData09() {
	}

	public SentenceData09(String[] forms, String[] postags, String[] labs, int[] heads) {
		this.forms = forms;
		gpos = postags;
		labels = labs;
		this.heads = heads;
	}

	public SentenceData09(String[] forms, String[] lemmas, String[] postags, String[] labs, int[] heads) {
		this.forms = forms;
		gpos = postags;
		// ppos = postags;

		labels = labs;
		this.heads = heads;
		this.plemmas = lemmas;
	}

	public SentenceData09(String[] forms, String[] lemmas, String[] gpos, String[] ppos, String[] labs, int[] heads) {
		this.forms = forms;
		this.gpos = gpos;
		this.ppos = ppos;

		labels = labs;
		this.heads = heads;
		this.plemmas = lemmas;

	}

	public SentenceData09(String[] forms, String[] lemmas, String[] gpos, String[] ppos, String[] labs, int[] heads,
			String[] fillpred) {
		this.forms = forms;
		this.gpos = gpos;
		this.ppos = ppos;

		labels = labs;
		this.heads = heads;
		this.plemmas = lemmas;

		fillp = fillpred;
	}

	public SentenceData09(String[] forms, String[] lemmas, String[] olemmas, String[] gpos, String[] ppos,
			String[] labs, int[] heads, String[] fillpred) {
		this.forms = forms;
		this.gpos = gpos;
		this.ppos = ppos;

		labels = labs;
		this.heads = heads;
		this.plemmas = lemmas;
		this.lemmas = olemmas;
		fillp = fillpred;
	}

	public SentenceData09(String[] forms, String[] olemmas, String[] lemmas, String[] gpos, String[] ppos,
			String[] labs, int[] heads, String[] fillpred, String[] of, String[] pf) {
		this.forms = forms;
		this.gpos = gpos;
		this.ppos = ppos;

		labels = labs;
		this.heads = heads;
		this.pheads = heads;
		this.plabels = labs;
		this.plemmas = lemmas;
		this.lemmas = olemmas;

		this.ofeats = of;
		this.pfeats = pf;
		fillp = fillpred;
	}

	/**
	 * Create an instance without root of the input instance
	 * 
	 * @param instance
	 */
	public SentenceData09(SentenceData09 i) {

		int length = i.length() - 1;

		forms = new String[length];
		gpos = new String[length];
		ppos = new String[length];
		plemmas = new String[length];
		plabels = new String[length];
		lemmas = new String[length];
		heads = new int[length];
		pheads = new int[length];
		ofeats = new String[length];
		pfeats = new String[length];
		labels = new String[length];
		fillp = new String[length];
		id = new String[length];

		for (int j = 0; j < length; j++) {
			forms[j] = i.forms[j + 1];
			ppos[j] = i.ppos[j + 1];
			gpos[j] = i.gpos[j + 1];

			labels[j] = i.labels[j + 1];
			heads[j] = i.heads[j + 1];

			if (i.pheads != null)
				pheads[j] = i.pheads[j + 1];
			if (i.plabels != null)
				plabels[j] = i.plabels[j + 1];

			if (i.lemmas != null)
				lemmas[j] = i.lemmas[j + 1];

			plemmas[j] = i.plemmas[j + 1];

			if (i.ofeats != null)
				ofeats[j] = i.ofeats[j + 1];
			if (i.pfeats != null)
				pfeats[j] = i.pfeats[j + 1];

			if (i.fillp != null)
				fillp[j] = i.fillp[j + 1];
			if (i.id != null)
				id[j] = i.id[j + 1];
		}

	}

	public void setPPos(String[] pos) {
		ppos = pos;
	}

	public void setLemmas(String[] lemmas) {
		this.plemmas = lemmas;
	}

	public void setFeats(String[] fts) {
		feats = new String[fts.length][];
		for (int i = 0; i < fts.length; i++) {
			feats[i] = fts[i].split("\\|");
		}
		pfeats = fts;
	}

	public int length() {
		return forms.length;
	}

	@Override
	public String toString() {
		// prepare the output
		StringWriter sw = new StringWriter();
		CONLLWriter09 snt2str = new is2.io.CONLLWriter09(sw);
		try {
			snt2str.write(this, CONLLWriter09.NO_ROOT);
			snt2str.finishWriting();
			return sw.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// backup
		StringBuffer sb = new StringBuffer();
		for (int k = 0; k < forms.length; k++)
			sb.append(k + 1).append('\t').append(forms[k]).append('\t').append(heads[k]).append('\t').append(labels[k])
					.append('\n');
		return sw.toString();
	}

	final public void write(DataOutputStream out) throws IOException {

		out.writeInt(forms.length);
		for (int k = 0; k < forms.length; k++) {
			out.writeUTF(forms[k]);
			out.writeUTF(ppos[k]);
			out.writeUTF(gpos[k]);
			out.writeInt(heads[k]);
			out.writeUTF(labels[k]);
			out.writeUTF(lemmas[k]);
			out.writeUTF(plemmas[k]);
			out.writeUTF(ofeats[k]); // needed for mtag
			out.writeUTF(fillp[k]);
		}

		// out.writeUTF(actParseTree);

	}

	final public void read(DataInputStream dis) throws IOException {

		int l = dis.readInt();

		forms = new String[l];
		lemmas = new String[l];
		plemmas = new String[l];
		ppos = new String[l];
		gpos = new String[l];
		labels = new String[l];
		heads = new int[l];
		fillp = new String[l];
		ofeats = new String[l];

		for (int k = 0; k < l; k++) {
			forms[k] = dis.readUTF();
			ppos[k] = dis.readUTF();
			gpos[k] = dis.readUTF();
			heads[k] = dis.readInt();
			labels[k] = dis.readUTF();
			lemmas[k] = dis.readUTF();
			plemmas[k] = dis.readUTF();
			ofeats[k] = dis.readUTF();
			fillp[k] = dis.readUTF();

		}
	}

	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
		forms = (String[]) in.readObject();
		plemmas = (String[]) in.readObject();
		ppos = (String[]) in.readObject();
		heads = (int[]) in.readObject();
		labels = (String[]) in.readObject();
	}

	public void addPredicate(int i, String s) {

		int predId;
		if (sem == null) {
			predId = 0;
			sem = new String[1];
			semposition = new int[1];
		} else {
			predId = sem.length;
			String p[] = new String[sem.length + 1];
			System.arraycopy(sem, 0, p, 0, sem.length);
			int id[] = new int[sem.length + 1];
			System.arraycopy(semposition, 0, id, 0, semposition.length);
			sem = p;
			semposition = id;
		}
		sem[predId] = s;
		semposition[predId] = i;
	}

	/**
	 * Add an argument
	 * 
	 * @param i
	 *            the instance (the child)
	 * @param predId
	 *            the id of the predicate (the head)
	 * @param a
	 *            the label of the argument
	 */
	public void addArgument(int i, int predId, String a) {

		if (a == null || a.equals("_"))
			return;

		// ensure the space for the argument in the data structure
		if (arg == null) {
			arg = new String[predId + 1][];
			argposition = new int[predId + 1][];
		} else if (arg.length <= predId) {
			String p[][] = new String[predId + 1][];
			System.arraycopy(arg, 0, p, 0, arg.length);
			arg = p;

			int id[][] = new int[predId + 1][];
			System.arraycopy(argposition, 0, id, 0, argposition.length);
			argposition = id;
		}

		int aId;
		if (arg[predId] == null) {
			aId = 0;
			arg[predId] = new String[1];
			argposition[predId] = new int[1];
		} else {
			aId = arg[predId].length;
			String args[] = new String[arg[predId].length + 1];
			System.arraycopy(arg[predId], 0, args, 0, arg[predId].length);
			arg[predId] = args;

			int argsId[] = new int[argposition[predId].length + 1];
			System.arraycopy(argposition[predId], 0, argsId, 0, argposition[predId].length);
			argposition[predId] = argsId;
		}

		arg[predId][aId] = a;
		argposition[predId][aId] = i;

	}

	public int[] getParents() {
		return heads;
	}

	public String[] getLabels() {
		return labels;
	}

	public String printSem() {

		if (sem == null)
			return "";
		StringBuilder s = new StringBuilder();

		for (int k = 0; k < sem.length; k++) {
			s.append(sem[k]).append("\n");

			if (arg == null) {
				s.append("arg == null");
			} else if (arg.length <= k) {
				s.append("args.length <=k arg.length:" + arg.length + " k:" + k);
			} else if (arg[k] != null) {
				for (int a = 0; a < arg[k].length; a++) {
					s.append("  ").append(arg[k][a]);
				}
			} else {
				s.append("args == null ");
			}
			s.append('\n');
		}
		return s.toString();
	}

	/**
	 * Initialize a instance so that a tagger, parser, etc. could be applied
	 * 
	 * @param forms
	 */
	public void init(String[] forms) {
		this.forms = forms;
		heads = new int[forms.length];
		gpos = new String[forms.length];
		ppos = new String[forms.length];
		plemmas = new String[forms.length];
		feats = new String[forms.length][0];
		labels = new String[forms.length];
	}

	/**
	 * @param instance
	 * @param fillp2
	 * @param i09
	 */
	public void createSemantic(SentenceData09 instance) {

		this.sem = instance.sem;
		this.semposition = instance.semposition;

		if (instance.semposition != null)
			for (int k = 0; k < instance.semposition.length; k++) {
				this.semposition[k] = instance.semposition[k] - 1;
			}

		this.arg = instance.arg;

		this.argposition = instance.argposition;

		if (this.argposition != null)
			for (int p = 0; p < instance.argposition.length; p++) {
				if (this.argposition[p] != null)
					for (int a = 0; a < instance.argposition[p].length; a++)
						this.argposition[p][a] = instance.argposition[p][a] - 1;
			}

	}

	/**
	 *
	 */
	public String oneLine() {

		StringBuffer o = new StringBuffer();
		for (int i = 1; i < this.length(); i++) {

			if (i != 1)
				o.append(" ");
			o.append(this.forms[i]);
		}
		return o.toString();
	}

	/**
	 * Get the children of this instance
	 * 
	 * @param head
	 * @return children of the head
	 */
	public ArrayList<Integer> getChildren(int head) {

		ArrayList<Integer> children = new ArrayList<Integer>();
		for (int i = 0; i < length(); i++) {
			if (heads[i] == head)
				children.add(i);
		}
		return children;
	}

	public void createWithRoot(SentenceData09 i) {

		int length = i.length();
		int offset = 0;
		if (!i.forms[0].equals(IOGenerals.ROOT)) {
			length++;
			offset = -1;
		}

		forms = new String[length];
		gpos = new String[length];
		ppos = new String[length];
		plemmas = new String[length];
		plabels = new String[length];
		lemmas = new String[length];
		heads = new int[length];
		pheads = new int[length];
		ofeats = new String[length];
		pfeats = new String[length];
		labels = new String[length];
		fillp = new String[length];
		id = new String[length];
		feats = new String[forms.length][];

		for (int j = 1; j < length; j++) {
			forms[j] = i.forms[j + offset];
			ppos[j] = i.ppos[j + offset];
			gpos[j] = i.gpos[j + offset];

			labels[j] = i.labels[j + offset];
			heads[j] = i.heads[j + offset];

			if (i.pheads != null)
				pheads[j] = i.pheads[j + offset];
			if (i.plabels != null)
				plabels[j] = i.plabels[j + offset];

			if (i.lemmas != null)
				lemmas[j] = i.lemmas[j + offset];

			plemmas[j] = i.plemmas[j + offset];

			// if (i.ofeats!=null) ofeats[j] = i.ofeats[j+offset];

			ofeats[j] = i.ofeats[j + offset].equals(CONLLWriter09.DASH) ? "_" : i.ofeats[j + offset];

			// if (i.pfeats!=null) pfeats[j] = i.pfeats[j+offset];

			if (i.pfeats != null && i.pfeats[j + offset] != null) {
				if (i.pfeats[j + offset].equals(CONLLWriter09.DASH))
					feats[j] = null;
				else {
					feats[j] = i.pfeats[j + offset].split(IOGenerals.PIPE);

					// if (info[7].equals(CONLLWriter09.DASH)) it.feats[i]=null;
					// else {
					// it.feats[i] =info[7].split(PIPE);
					pfeats[j] = i.pfeats[j + offset];
					// }
				}
			}

			if (i.fillp != null)
				fillp[j] = i.fillp[j + offset];
			if (i.id != null)
				id[j] = i.id[j + offset];
		}

		forms[0] = IOGenerals.ROOT;
		plemmas[0] = IOGenerals.ROOT_LEMMA;
		fillp[0] = "N";
		lemmas[0] = IOGenerals.ROOT_LEMMA;

		gpos[0] = IOGenerals.ROOT_POS;
		ppos[0] = IOGenerals.ROOT_POS;
		labels[0] = IOGenerals.NO_TYPE;
		heads[0] = -1;
		plabels[0] = IOGenerals.NO_TYPE;
		pheads[0] = -1;
		ofeats[0] = IOGenerals.NO_TYPE;
		id[0] = "0";
	}

}