📄 dependencypipe2o.java
字号:
package mstparser;import java.io.*;import gnu.trove.*;import java.util.*;public class DependencyPipe2O extends DependencyPipe { public DependencyPipe2O() throws IOException { super(); } public DependencyPipe2O(boolean createForest) throws IOException { super(createForest); } public FeatureVector createFeatureVector(String[] toks, String[] pos, String[] posA, int par, int ch1, int ch2, FeatureVector fv) { // ch1 is always the closes to par String dir = par > ch2 ? "RA" : "LA"; String par_pos = pos[par]; String ch1_pos = ch1 == par ? "STPOS" : pos[ch1]; String ch2_pos = pos[ch2]; String ch1_word = ch1 == par ? "STWRD" : toks[ch1]; String ch2_word = toks[ch2]; String pTrip = par_pos+"_"+ch1_pos+"_"+ch2_pos; fv = add("POS_TRIP="+pTrip+"_"+dir,1.0,fv); fv = add("APOS_TRIP="+pTrip,1.0,fv); return fv; } public FeatureVector createFeatureVectorSib(String[] toks, String[] pos, int ch1, int ch2, boolean isST, FeatureVector fv) { // ch1 is always the closes to par String dir = ch1 > ch2 ? "RA" : "LA"; String ch1_pos = isST ? "STPOS" : pos[ch1]; String ch2_pos = pos[ch2]; String ch1_word = isST ? "STWRD" : toks[ch1]; String ch2_word = toks[ch2]; fv = add("CH_PAIR="+ch1_pos+"_"+ch2_pos+"_"+dir,1.0,fv); fv = add("CH_WPAIR="+ch1_word+"_"+ch2_word+"_"+dir,1.0,fv); fv = add("CH_WPAIRA="+ch1_word+"_"+ch2_pos+"_"+dir,1.0,fv); fv = add("CH_WPAIRB="+ch1_pos+"_"+ch2_word+"_"+dir,1.0,fv); fv = add("ACH_PAIR="+ch1_pos+"_"+ch2_pos,1.0,fv); fv = add("ACH_WPAIR="+ch1_word+"_"+ch2_word,1.0,fv); fv = add("ACH_WPAIRA="+ch1_word+"_"+ch2_pos,1.0,fv); fv = add("ACH_WPAIRB="+ch1_pos+"_"+ch2_word,1.0,fv); int dist = Math.max(ch1,ch2)-Math.min(ch1,ch2); String distBool = "0"; if(dist > 1) distBool = "1"; if(dist > 2) distBool = "2"; if(dist > 3) distBool = "3"; if(dist > 4) distBool = "4"; if(dist > 5) distBool = "5"; if(dist > 10) distBool = "10"; fv = add("SIB_PAIR_DIST="+distBool+"_"+dir,1.0,fv); fv = add("ASIB_PAIR_DIST="+distBool,1.0,fv); fv = add("CH_PAIR_DIST="+ch1_pos+"_"+ch2_pos+"_"+distBool+"_"+dir,1.0,fv); fv = add("ACH_PAIR_DIST="+ch1_pos+"_"+ch2_pos+"_"+distBool,1.0,fv); return fv; } public FeatureVector createFeatureVector(String[] toks, String[] pos, int[] labs1, int[] deps) { String[] labs = new String[labs1.length]; for(int i = 0; i < labs.length; i++) labs[i] = types[labs1[i]]; return createFeatureVector(toks,pos,labs,deps); } public FeatureVector createFeatureVector(String[] toks, String[] pos, String[] labs, int[] deps) { String[] posA = new String[pos.length]; for(int i = 0; i < pos.length; i++) { posA[i] = pos[i].substring(0,1); } FeatureVector fv = new FeatureVector(-1,-1.0,null); for(int i = 0; i < toks.length; i++) { if(deps[i] == -1) continue; int small = i < deps[i] ? i : deps[i]; int large = i > deps[i] ? i : deps[i]; boolean attR = i < deps[i] ? false : true; fv = createFeatureVector(toks,pos,posA,small,large,attR,fv); if(labeled) { fv = createFeatureVector(toks,pos,posA,i,labs[i],attR,true,fv); fv = createFeatureVector(toks,pos,posA,deps[i],labs[i],attR,false,fv); } } // find all trip features for(int i = 0; i < toks.length; i++) { if(deps[i] == -1 && i != 0) continue; // right children int prev = i; for(int j = i+1; j < toks.length; j++) { if(deps[j] == i) { fv = createFeatureVector(toks,pos,posA,i,prev,j,fv); fv = createFeatureVectorSib(toks,pos,prev,j,prev==i,fv); prev = j; } } prev = i; for(int j = i-1; j >= 0; j--) { if(deps[j] == i) { fv = createFeatureVector(toks,pos,posA,i,prev,j,fv); fv = createFeatureVectorSib(toks,pos,prev,j,prev==i,fv); prev = j; } } } return fv; } /* public FeatureVector createFeatureVector(int[] deps, NewFeatureVector[][][] fvs, NewFeatureVector[][][] fvs_trips, NewFeatureVector[][][] fvs_sibs, NewFeatureVector[][][] fvs_gp) { FeatureVector fv = new FeatureVector(-1,-1.0,null); for(int i = 0; i < deps.length; i++) { if(deps[i] == -1) continue; int small = i < deps[i] ? i : deps[i]; int large = i > deps[i] ? i : deps[i]; int attR = i < deps[i] ? 1 : 0; fv = NewFeatureVector.cat(fvs[small][large][attR],fv); } // find all trip features for(int i = 0; i < deps.length; i++) { // right children if(deps[i] == -1 && i != 0) continue; int prev = i; for(int j = i+1; j < deps.length; j++) { if(deps[j] == i) { fv = NewFeatureVector.cat(fvs_trips[i][prev][j],fv); fv = NewFeatureVector.cat(fvs_sibs[prev][j][prev == i ? 0 : 1],fv); prev = j; } } prev = i; for(int j = i-1; j >= 0; j--) { if(deps[j] == i) { fv = NewFeatureVector.cat(fvs_trips[i][prev][j],fv); fv = NewFeatureVector.cat(fvs_sibs[prev][j][prev == i ? 0 : 1],fv); prev = j; } } } return fv; } */ public void possibleFeatures(DependencyInstance inst, ObjectOutputStream out) { String[] toks = inst.sentence; String[] pos = inst.pos; String[] labs = inst.labs; String[] posA = new String[pos.length]; for(int i = 0; i < pos.length; i++) { posA[i] = pos[i].substring(0,1); } try { for(int w1 = 0; w1 < toks.length; w1++) { for(int w2 = w1+1; w2 < toks.length; w2++) { for(int ph = 0; ph < 2; ph++) { boolean attR = ph == 0 ? true : false; FeatureVector prodFV = createFeatureVector(toks,pos,posA,w1,w2,attR, new FeatureVector(-1,-1.0,null)); for(FeatureVector curr = prodFV; curr != null; curr = curr.next) { if(curr.index >= 0) out.writeInt(curr.index); } out.writeInt(-2); } } } out.writeInt(-3); if(labeled) { for(int w1 = 0; w1 < toks.length; w1++) { for(int t = 0; t < types.length; t++) { String type = types[t]; for(int ph = 0; ph < 2; ph++) { boolean attR = ph == 0 ? true : false; for(int ch = 0; ch < 2; ch++) { boolean child = ch == 0 ? true : false; FeatureVector prodFV = createFeatureVector(toks,pos,posA,w1, type, attR,child, new FeatureVector(-1,-1.0,null)); for(FeatureVector curr = prodFV; curr != null; curr = curr.next) { if(curr.index >= 0) out.writeInt(curr.index); } out.writeInt(-2); } } } } out.writeInt(-3); } for(int w1 = 0; w1 < toks.length; w1++) { for(int w2 = w1; w2 < toks.length; w2++) { for(int w3 = w2+1; w3 < toks.length; w3++) { FeatureVector prodFV = createFeatureVector(toks,pos,posA,w1,w2,w3, new FeatureVector(-1,-1.0,null)); for(FeatureVector curr = prodFV; curr != null; curr = curr.next) { if(curr.index >= 0) out.writeInt(curr.index); } out.writeInt(-2); } } for(int w2 = w1; w2 >= 0; w2--) { for(int w3 = w2-1; w3 >= 0; w3--) { FeatureVector prodFV = createFeatureVector(toks,pos,posA,w1,w2,w3, new FeatureVector(-1,-1.0,null)); for(FeatureVector curr = prodFV; curr != null; curr = curr.next) { if(curr.index >= 0) out.writeInt(curr.index); } out.writeInt(-2); } } } out.writeInt(-3); for(int w1 = 0; w1 < toks.length; w1++) { for(int w2 = 0; w2 < toks.length; w2++) { for(int wh = 0; wh < 2; wh++) { if(w1 != w2) { FeatureVector prodFV = createFeatureVectorSib(toks,pos,w1,w2,wh == 0, new FeatureVector(-1,-1.0,null)); for(FeatureVector curr = prodFV; curr != null; curr = curr.next) { if(curr.index >= 0) out.writeInt(curr.index); } out.writeInt(-2); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -