-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_sampler_to_corenlp_nndep.diff
767 lines (758 loc) · 25.5 KB
/
add_sampler_to_corenlp_nndep.diff
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
diff --git a/src/brendanutil/FastRandom.java b/src/brendanutil/FastRandom.java
new file mode 100644
index 0000000..fd58b4d
--- /dev/null
+++ b/src/brendanutil/FastRandom.java
@@ -0,0 +1,581 @@
+package brendanutil;
+
+import java.io.Serializable;
+import java.lang.management.ManagementFactory;
+import java.nio.ByteBuffer;
+import java.util.*;
+
+/**
+ * An RNG class that's faster and hopefully better than java.util.Random.
+ *
+ * (1) Is NOT synchronized! Thus NOT thread-safe. Therefore faster.
+ * In most applications I can think of, every thread should get its own RNG.
+ * Threadsafe can be nice for convenient utility functions; either wrap a FastRandom in a ThreadLocal,
+ * or use java.util.Random for that.
+ * (2) Uses the XORShift RNG algorithm, which is faster and more accurate (more random)
+ * than the algorithm in java.util.Random.
+ * (3) Folds in the high-level methods from Mallet's cc.mallet.util.Randoms
+ * (4) Leaves out some of the convenience methods that appear in java.util.Random but not Mallet.
+ *
+ * XORShift implementation from http://maths.uncommons.org/
+ * Mallet methods from http://mallet.cs.umass.edu/
+ * ... i have no idea what license status that leaves this file
+ *
+ * See discussions e.g.
+ * http://stackoverflow.com/questions/453479/how-good-is-java-util-random
+ * http://www.cs.gmu.edu/~sean/research/mersenne/
+ *
+ * Speed tests, compared to java.util.Random - see main():
+ * - 10.7 times faster for nextUniform()
+ * - 1.8 times faster for nextInt(1000)
+ * - I swapped this into an LDA collapsed Gibbs sampler, and got a 5% speedup of the entire program
+ *
+ * Looking for feedback / bug reports.
+ *
+ * @author Brendan O'Connor (http://brenocon.com), Jan 2012, https://gist.github.com/4561065
+ */
+public class FastRandom implements Serializable {
+ static final long serialVersionUID = -1L;
+
+ private static ThreadLocal<FastRandom> _rand = new ThreadLocal<FastRandom>() {
+ @Override
+ protected FastRandom initialValue() {
+ return new FastRandom();
+ }
+ };
+ /** Access a threadlocal FastRandom (presumably faster than synchronized global Random) */
+ public static FastRandom rand() { return _rand.get(); }
+
+
+ ///////////// RNG section, from http://maths.uncommons.org/
+ // additions are marked
+
+ // ============================================================================
+ // Copyright 2006-2012 Daniel W. Dyer
+ //
+ // Licensed under the Apache License, Version 2.0 (the "License");
+ // you may not use this file except in compliance with the License.
+ // You may obtain a copy of the License at
+ //
+ // http://www.apache.org/licenses/LICENSE-2.0
+ //
+ // Unless required by applicable law or agreed to in writing, software
+ // distributed under the License is distributed on an "AS IS" BASIS,
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ // See the License for the specific language governing permissions and
+ // limitations under the License.
+ //============================================================================
+ // package org.uncommons.maths.random;
+ // public class XORShiftRNG extends Random implements RepeatableRNG
+
+
+ private static final int SEED_SIZE_BYTES = 20; // Needs 5 32-bit integers.
+
+ // Previously used an array for state but using separate fields proved to be
+ // faster.
+ private int state1;
+ private int state2;
+ private int state3;
+ private int state4;
+ private int state5;
+
+ private final byte[] seed;
+
+
+
+ /**
+ * Creates a new RNG and seeds it using the default seeding strategy.
+ */
+ public FastRandom()
+ {
+ this(seedFromTimeHost());
+ }
+
+ public FastRandom(long seed) {
+ this(seedFromString("happyinteger" + seed));
+ }
+
+ static private volatile int uniquifier = 0; // so multiple RNG's created in the same process are forced unique
+
+ /** highly lame @author brendano */
+ static byte[] seedFromTimeHost() {
+ long t = System.nanoTime();
+ String s = t + " " + (++uniquifier) + " " + ManagementFactory.getRuntimeMXBean().getName();
+ return seedFromString(s);
+ }
+
+ static byte[] seedFromString(String s) {
+ ByteBuffer bb = ByteBuffer.allocate(20);
+ bb.putInt((s+"1").hashCode());
+ bb.putInt((s+"2").hashCode());
+ bb.putInt((s+"3").hashCode());
+ bb.putInt((s+"4").hashCode());
+ bb.putInt((s+"5").hashCode());
+ return bb.array();
+ }
+
+ /**
+ * Creates an RNG and seeds it with the specified seed data.
+ * @param seed The seed data used to initialise the RNG.
+ */
+ public FastRandom(byte[] seed)
+ {
+ if (seed == null || seed.length != SEED_SIZE_BYTES)
+ {
+ throw new IllegalArgumentException("XOR shift RNG requires 160 bits of seed data.");
+ }
+ this.seed = seed.clone();
+ int[] state = convertBytesToInts(seed);
+ this.state1 = state[0];
+ this.state2 = state[1];
+ this.state3 = state[2];
+ this.state4 = state[3];
+ this.state5 = state[4];
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public byte[] getSeed()
+ {
+ return seed.clone();
+ }
+
+
+ protected int next(int bits)
+ {
+ int t = (state1 ^ (state1 >> 7));
+ state1 = state2;
+ state2 = state3;
+ state3 = state4;
+ state4 = state5;
+ state5 = (state5 ^ (state5 << 6)) ^ (t ^ (t << 13));
+ int value = (state2 + state2 + 1) * state5;
+ return value >>> (32 - bits);
+ }
+
+ /// from BinaryUtils
+
+ // Mask for casting a byte to an int, bit-by-bit (with
+ // bitwise AND) with no special consideration for the sign bit.
+ private static final int BITWISE_BYTE_TO_INT = 0x000000FF;
+
+ /**
+ * Take four bytes from the specified position in the specified
+ * block and convert them into a 32-bit int, using the big-endian
+ * convention.
+ * @param bytes The data to read from.
+ * @param offset The position to start reading the 4-byte int from.
+ * @return The 32-bit integer represented by the four bytes.
+ */
+ public static int convertBytesToInt(byte[] bytes, int offset)
+ {
+ return (BITWISE_BYTE_TO_INT & bytes[offset + 3])
+ | ((BITWISE_BYTE_TO_INT & bytes[offset + 2]) << 8)
+ | ((BITWISE_BYTE_TO_INT & bytes[offset + 1]) << 16)
+ | ((BITWISE_BYTE_TO_INT & bytes[offset]) << 24);
+ }
+
+ /**
+ * Convert an array of bytes into an array of ints. 4 bytes from the
+ * input data map to a single int in the output data.
+ * @param bytes The data to read from.
+ * @return An array of 32-bit integers constructed from the data.
+ * @since 1.1
+ */
+ public static int[] convertBytesToInts(byte[] bytes)
+ {
+ if (bytes.length % 4 != 0)
+ {
+ throw new IllegalArgumentException("Number of input bytes must be a multiple of 4.");
+ }
+ int[] ints = new int[bytes.length / 4];
+ for (int i = 0; i < ints.length; i++)
+ {
+ ints[i] = convertBytesToInt(bytes, i * 4);
+ }
+ return ints;
+ }
+
+ ////////////////// END ///////////// RNG section, from http://maths.uncommons.org/
+
+ ///////////////// START java.util.Random section
+
+ public int nextInt(int n) {
+ if (n <= 0)
+ throw new IllegalArgumentException("n must be positive");
+
+ if ((n & -n) == n) // i.e., n is a power of 2
+ return (int)((n * (long)next(31)) >> 31);
+
+ int bits, val;
+ do {
+ bits = next(31);
+ val = bits % n;
+ } while (bits - val + (n-1) < 0);
+ return val;
+ }
+
+ ///////////////// END java.util.Random section
+
+ ///////////////// START cc.mallet.util.Randoms section
+
+ /* Copyright (C) 2002 Univ. of Massachusetts Amherst, Computer Science Dept.
+ This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
+ http://www.cs.umass.edu/~mccallum/mallet
+ This software is provided under the terms of the Common Public License,
+ version 1.0, as published by http://www.opensource.org. For further
+ information, see the file `LICENSE' included with this distribution. */
+
+
+
+ /** Return random integer from Poission with parameter lambda.
+ * The mean of this distribution is lambda. The variance is lambda. */
+ public int nextPoisson(double lambda) {
+ int i,j,v=-1;
+ double l=Math.exp(-lambda),p;
+ p=1.0;
+ while (p>=l) {
+ p*=nextUniform();
+ v++;
+ }
+ return v;
+ }
+
+ /** Return nextPoisson(1). */
+ public int nextPoisson() {
+ return nextPoisson(1);
+ }
+
+ /** Return a random boolean, equally likely to be true or false. */
+ public boolean nextBoolean() {
+ return (next(32) & 1 << 15) != 0;
+ }
+
+ /** Return a random boolean, with probability p of being true. */
+ public boolean nextBoolean(double p) {
+ double u=nextUniform();
+ if(u < p) return true;
+ return false;
+ }
+
+ /** Return a random BitSet with "size" bits, each having probability p of being true. */
+ public BitSet nextBitSet (int size, double p)
+ {
+ BitSet bs = new BitSet (size);
+ for (int i = 0; i < size; i++)
+ if (nextBoolean (p)) {
+ bs.set (i);
+ }
+ return bs;
+ }
+
+ /** Return a random double in the range 0 to 1, inclusive, uniformly sampled from that range.
+ * The mean of this distribution is 0.5. The variance is 1/12. */
+ public double nextUniform() {
+ long l = ((long)(next(26)) << 27) + next(27);
+ return l / (double)(1L << 53);
+ }
+
+ /** Return a random double in the range a to b, inclusive, uniformly sampled from that range.
+ * The mean of this distribution is (b-a)/2. The variance is (b-a)^2/12 */
+ public double nextUniform(double a,double b) {
+ return a + (b-a)*nextUniform();
+ }
+
+ /** Draw a single sample from multinomial "a". ASSUME SUMS to 1 ! */
+ public int nextDiscrete (double[] a) {
+ double b = 0, r = nextUniform();
+ for (int i = 0; i < a.length; i++) {
+ b += a[i];
+ if (b > r) {
+ return i;
+ }
+ }
+ return a.length-1;
+ }
+
+ /** draw a single sample from (unnormalized) multinomial "a", with normalizing factor "sum". */
+ public int nextDiscrete (double[] a, double sum) {
+ double b = 0, r = nextUniform() * sum;
+ for (int i = 0; i < a.length; i++) {
+ b += a[i];
+ if (b > r) {
+ return i;
+ }
+ }
+ return a.length-1;
+ }
+
+ private double nextGaussian;
+ private boolean haveNextGaussian = false;
+
+ /** Return a random double drawn from a Gaussian distribution with mean 0 and variance 1. */
+ public double nextGaussian() {
+ if (!haveNextGaussian) {
+ double v1=nextUniform(),v2=nextUniform();
+ double x1,x2;
+ x1=Math.sqrt(-2*Math.log(v1))*Math.cos(2*Math.PI*v2);
+ x2=Math.sqrt(-2*Math.log(v1))*Math.sin(2*Math.PI*v2);
+ nextGaussian=x2;
+ haveNextGaussian=true;
+ return x1;
+ }
+ else {
+ haveNextGaussian=false;
+ return nextGaussian;
+ }
+ }
+
+ /** Return a random double drawn from a Gaussian distribution with mean m and variance s2. */
+ public double nextGaussian(double mean, double var) {
+ assert var > 0;
+ return nextGaussian()*Math.sqrt(var) + mean;
+ }
+
+ // generate Gamma(1,1)
+ // E(X)=1 ; Var(X)=1
+ /** Return a random double drawn from a Gamma distribution with mean 1.0 and variance 1.0. */
+ public double nextGamma() {
+ return nextGamma(1,1,0);
+ }
+
+ /** Return a random double drawn from a Gamma distribution with mean alpha and variance 1.0. */
+ public double nextGamma(double alpha) {
+ return nextGamma(alpha,1,0);
+ }
+
+ /* Return a sample from the Gamma distribution, with parameter IA */
+ /* From Numerical "Recipes in C", page 292 */
+ public double oldNextGamma (int ia)
+ {
+ int j;
+ double am, e, s, v1, v2, x, y;
+
+ assert (ia >= 1) ;
+ if (ia < 6)
+ {
+ x = 1.0;
+ for (j = 1; j <= ia; j++)
+ x *= nextUniform ();
+ x = - Math.log (x);
+ }
+ else
+ {
+ do
+ {
+ do
+ {
+ do
+ {
+ v1 = 2.0 * nextUniform () - 1.0;
+ v2 = 2.0 * nextUniform () - 1.0;
+ }
+ while (v1 * v1 + v2 * v2 > 1.0);
+ y = v2 / v1;
+ am = ia - 1;
+ s = Math.sqrt (2.0 * am + 1.0);
+ x = s * y + am;
+ }
+ while (x <= 0.0);
+ e = (1.0 + y * y) * Math.exp (am * Math.log (x/am) - s * y);
+ }
+ while (nextUniform () > e);
+ }
+ return x;
+ }
+
+
+ /** Return a random double drawn from a Gamma distribution with mean alpha*beta and variance alpha*beta^2. */
+ public double nextGamma(double alpha, double beta) {
+ return nextGamma(alpha,beta,0);
+ }
+
+ /** Return a random double drawn from a Gamma distribution
+ * with mean alpha*beta+lamba and variance alpha*beta^2.
+ * Note that this means the pdf is:
+ * <code>frac{ x^{alpha-1} exp(-x/beta) }{ beta^alpha Gamma(alpha) }</code>
+ * in other words, beta is a "scale" parameter. An alternative
+ * parameterization would use 1/beta, the "rate" parameter.
+ */
+ public double nextGamma(double alpha, double beta, double lambda) {
+ double gamma=0;
+ if (alpha <= 0 || beta <= 0) {
+ throw new IllegalArgumentException ("alpha and beta must be strictly positive.");
+ }
+ if (alpha < 1) {
+ double b,p;
+ boolean flag = false;
+
+ b = 1 + alpha * Math.exp(-1);
+
+ while (!flag) {
+ p = b * nextUniform();
+ if (p > 1) {
+ gamma = -Math.log((b - p) / alpha);
+ if (nextUniform() <= Math.pow(gamma, alpha - 1)) {
+ flag = true;
+ }
+ }
+ else {
+ gamma = Math.pow(p, 1.0/alpha);
+ if (nextUniform() <= Math.exp(-gamma)) {
+ flag = true;
+ }
+ }
+ }
+ }
+ else if (alpha == 1) {
+ // Gamma(1) is equivalent to Exponential(1). We can
+ // sample from an exponential by inverting the CDF:
+
+ gamma = -Math.log (nextUniform ());
+
+ // There is no known closed form for Gamma(alpha != 1)...
+ }
+ else {
+
+ // This is Best's algorithm: see pg 410 of
+ // Luc Devroye's "non-uniform random variate generation"
+ // This algorithm is constant time for alpha > 1.
+
+ double b = alpha - 1;
+ double c = 3 * alpha - 0.75;
+
+ double u, v;
+ double w, y, z;
+
+ boolean accept = false;
+
+ while (! accept) {
+ u = nextUniform();
+ v = nextUniform();
+
+ w = u * (1 - u);
+ y = Math.sqrt( c / w ) * (u - 0.5);
+ gamma = b + y;
+
+ if (gamma >= 0.0) {
+ z = 64 * w * w * w * v * v; // ie: 64 * w^3 v^2
+
+ accept = z <= 1.0 - ((2 * y * y) / gamma);
+
+ if (! accept) {
+ accept = (Math.log(z) <=
+ 2 * (b * Math.log(gamma / b) - y));
+ }
+ }
+ }
+
+ /* // Old version, uses time linear in alpha
+ double y = -Math.log (nextUniform ());
+ while (nextUniform () > Math.pow (y * Math.exp (1 - y), alpha - 1))
+ y = -Math.log (nextUniform ());
+ gamma = alpha * y;
+ */
+ }
+ return beta*gamma+lambda;
+ }
+
+ /** Return a random double drawn from an Exponential distribution with mean 1 and variance 1. */
+ public double nextExp() {
+ return nextGamma(1,1,0);
+ }
+
+ /** Return a random double drawn from an Exponential distribution with mean beta and variance beta^2. */
+ public double nextExp(double beta) {
+ return nextGamma(1,beta,0);
+ }
+
+ /** Return a random double drawn from an Exponential distribution with mean beta+lambda and variance beta^2. */
+ public double nextExp(double beta,double lambda) {
+ return nextGamma(1,beta,lambda);
+ }
+
+ /** Return a random double drawn from an Chi-squarted distribution with mean 1 and variance 2.
+ * Equivalent to nextChiSq(1) */
+ public double nextChiSq() {
+ return nextGamma(0.5,2,0);
+ }
+
+ /** Return a random double drawn from an Chi-squared distribution with mean df and variance 2*df. */
+ public double nextChiSq(int df) {
+ return nextGamma(0.5*(double)df,2,0);
+ }
+
+ /** Return a random double drawn from an Chi-squared distribution with mean df+lambda and variance 2*df. */
+ public double nextChiSq(int df,double lambda) {
+ return nextGamma(0.5*(double)df,2,lambda);
+ }
+
+ /** Return a random double drawn from a Beta distribution with mean a/(a+b) and variance ab/((a+b+1)(a+b)^2). */
+ public double nextBeta(double alpha,double beta) {
+ if (alpha <= 0 || beta <= 0) {
+ throw new IllegalArgumentException ("alpha and beta must be strictly positive.");
+ }
+ if (alpha == 1 && beta == 1) {
+ return nextUniform ();
+ } else if (alpha >= 1 && beta >= 1) {
+ double A = alpha - 1,
+ B = beta - 1,
+ C = A + B,
+ L = C * Math.log (C),
+ mu = A / C,
+ sigma = 0.5 / Math.sqrt (C);
+ double y = nextGaussian (), x = sigma * y + mu;
+ while (x < 0 || x > 1) {
+ y = nextGaussian ();
+ x = sigma * y + mu;
+ }
+ double u = nextUniform ();
+ while (Math.log (u) >= A * Math.log (x / A) + B * Math.log ((1 - x) / B) + L + 0.5 * y * y) {
+ y = nextGaussian ();
+ x = sigma * y + mu;
+ while (x < 0 || x > 1) {
+ y = nextGaussian ();
+ x = sigma * y + mu;
+ }
+ u = nextUniform ();
+ }
+ return x;
+ } else {
+ double v1 = Math.pow (nextUniform (), 1 / alpha),
+ v2 = Math.pow (nextUniform (), 1 / beta);
+ while (v1 + v2 > 1) {
+ v1 = Math.pow (nextUniform (), 1 / alpha);
+ v2 = Math.pow (nextUniform (), 1 / beta);
+ }
+ return v1 / (v1 + v2);
+ }
+ }
+
+
+ ////////////////////////// END mallet section
+
+// static double qcauchy(double p, double center, double gamma) {
+// // http://en.wikipedia.org/wiki/Cauchy_distribution#Cumulative_distribution_function
+// return center + gamma * Math.tan(Math.PI * (p - 0.5));
+// }
+
+ //////////////////////
+
+ public static void main(String[] args) {
+ FastRandom r = new FastRandom();
+// java.util.Random r = new java.util.Random();
+ // Mallet "Randoms" uses java.util.Random's RNG (it's a subclass), and has nextUniform()
+// cc.mallet.util.Randoms r = new cc.mallet.util.Randoms();
+
+ int niter = (int) 5e8;
+ int lim = 32;
+ double t0 = (double) System.nanoTime();
+ for (int i=0; i < niter; i++) {
+// int y = r.nextInt(lim);
+ double y = r.nextUniform();
+// System.out.println(y);
+ }
+ double elapsed = System.nanoTime() - t0;
+ System.out.printf("%g s total, %g ns/iter\n", elapsed/1e9, elapsed/niter);
+
+ }
+}
diff --git a/src/edu/stanford/nlp/parser/nndep/DependencyParser.java b/src/edu/stanford/nlp/parser/nndep/DependencyParser.java
index edc98a6..f8f38d9 100644
--- a/src/edu/stanford/nlp/parser/nndep/DependencyParser.java
+++ b/src/edu/stanford/nlp/parser/nndep/DependencyParser.java
@@ -1,7 +1,8 @@
package edu.stanford.nlp.parser.nndep;
import edu.stanford.nlp.util.Generics;
import edu.stanford.nlp.util.logging.Redwood;
-
+//import brendanutil.Arr;
+import brendanutil.FastRandom;
import edu.stanford.nlp.international.Language;
import edu.stanford.nlp.io.IOUtils;
import edu.stanford.nlp.io.RuntimeIOException;
@@ -12,6 +13,7 @@ import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.ling.Word;
+import edu.stanford.nlp.math.ArrayMath;
import edu.stanford.nlp.process.DocumentPreprocessor;
import edu.stanford.nlp.stats.Counter;
import edu.stanford.nlp.stats.Counters;
@@ -101,6 +103,8 @@ public class DependencyParser {
return Collections.unmodifiableSet(foo);
}
+ double sampleTemp = 1.0;
+
/**
* Mapping from word / POS / dependency relation label to integer ID
*/
@@ -940,6 +944,29 @@ public class DependencyParser {
return c.tree;
}
+ private DependencyTree predictInnerSample(CoreMap sentence) {
+ int numTrans = system.numTransitions();
+
+ Configuration c = system.initialConfiguration(sentence);
+ while (!system.isTerminal(c)) {
+ if (Thread.interrupted()) { // Allow interrupting
+ throw new RuntimeInterruptedException();
+ }
+ double[] scores = classifier.computeScores(getFeatureArray(c));
+ for (int j = 0; j < numTrans; ++j) {
+ if (!system.canApply(c, system.transitions.get(j))) {
+ scores[j] = -1e10;
+ }
+ scores[j] *= sampleTemp;
+ }
+ scores = ArrayMath.softmax(scores); //they have the safe version
+ // U.pf("%s %s\n", Arr.max(scores), ArrayMath.entropy(scores));
+ int transition = FastRandom.rand().nextDiscrete(scores);
+ system.apply(c, system.transitions.get(transition));
+ }
+ return c.tree;
+ }
+
/**
* Determine the dependency parse of the given sentence using the loaded model.
* You must first load a parser before calling this method.
@@ -951,9 +978,11 @@ public class DependencyParser {
if (system == null)
throw new IllegalStateException("Parser has not been " +
"loaded and initialized; first load a model.");
-
DependencyTree result = predictInner(sentence);
+ return makeGS(result, sentence);
+ }
+ private GrammaticalStructure makeGS(DependencyTree result, CoreMap sentence) {
// The rest of this method is just busy-work to convert the
// package-local representation into a CoreNLP-standard
// GrammaticalStructure.
@@ -984,6 +1013,18 @@ public class DependencyParser {
TreeGraphNode rootNode = new TreeGraphNode(root);
return makeGrammaticalStructure(dependencies, rootNode);
}
+ public List<GrammaticalStructure> predictSample(CoreMap sentence, int numSamples) {
+ if (system == null)
+ throw new IllegalStateException("Parser has not been " +
+ "loaded and initialized; first load a model.");
+
+ List<GrammaticalStructure> results = new ArrayList<>();
+ for (int s=0; s<numSamples; s++) {
+ DependencyTree result = predictInnerSample(sentence);
+ results.add(makeGS(result,sentence));
+ }
+ return results;
+ }
private GrammaticalRelation makeGrammaticalRelation(String label) {
GrammaticalRelation stored;
@@ -1111,6 +1152,47 @@ public class DependencyParser {
}
return las;
}
+
+
+ /** new
+ */
+ public double testCoNLLSample(String testFile, String outFile, int numSamples) {
+ log.info("Test File: " + testFile);
+ List<CoreMap> testSents = new ArrayList<>();
+ List<DependencyTree> testTrees = new ArrayList<>();
+ Util.loadConllFile(testFile, testSents, testTrees, config.unlabeled, config.cPOS);
+
+ PrintWriter output;
+ try {
+ output = IOUtils.getPrintWriter(outFile);
+
+ for (CoreMap sent : testSents) {
+ List<DependencyTree> trees = new ArrayList<>();
+ for (int s=0; s<numSamples; s++) {
+ DependencyTree tree = predictInnerSample(sent);
+ trees.add(tree);
+ }
+ List<CoreLabel> tokens = sent.get(CoreAnnotations.TokensAnnotation.class);
+ for (int j = 1, size = tokens.size(); j <= size; ++j)
+ {
+ CoreLabel token = tokens.get(j - 1);
+ output.printf("%d\t%s\t_\t%s\t%s\t_\t",
+ j, token.word(), token.tag(), token.tag());
+
+ for (DependencyTree tree : trees) {
+ // "%d\t%s\t_\t_%n"
+ output.printf("%d %s\t",tree.getHead(j), tree.getLabel(j));
+ }
+ output.println();
+ }
+ output.println();
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ return 0;
+ }
private void parseTextFile(BufferedReader input, PrintWriter output) {
DocumentPreprocessor preprocessor = new DocumentPreprocessor(input);
@@ -1248,6 +1330,7 @@ public class DependencyParser {
* </table>
*/
public static void main(String[] args) {
+ System.out.println("BTO");
Properties props = StringUtils.argsToProperties(args, numArgs);
DependencyParser parser = new DependencyParser(props);
@@ -1256,12 +1339,21 @@ public class DependencyParser {
parser.train(props.getProperty("trainFile"), props.getProperty("devFile"), props.getProperty("model"),
props.getProperty("embedFile"), props.getProperty("preModel"));
+ if (props.containsKey("sampleTemp")) {
+ parser.sampleTemp = Double.parseDouble(props.getProperty("sampleTemp"));
+ }
boolean loaded = false;
// Test with CoNLL-X data
if (props.containsKey("testFile")) {
parser.loadModelFile(props.getProperty("model"));
loaded = true;
- parser.testCoNLL(props.getProperty("testFile"), props.getProperty("outFile"));
+ if (props.containsKey("numSamples")) {
+ int n = Integer.parseInt(props.getProperty("numSamples"));
+ parser.testCoNLLSample(props.getProperty("testFile"), props.getProperty("outFile"), n);
+ }
+ else {
+ parser.testCoNLL(props.getProperty("testFile"), props.getProperty("outFile"));
+ }
}
// Parse raw text data
@@ -1292,6 +1384,7 @@ public class DependencyParser {
throw new RuntimeIOException("Error opening output file", e);
}
+// parser.parseTextFileSample(input, output, 3);
parser.parseTextFile(input, output);
}
}