📄 dependencypipe.java
字号:
fv = add("JJ="+head,1.0,fv); //this fv = add("KK="+child+" "+childP,1.0,fv); fv = add("LL="+childP,1.0,fv); fv = add("MM="+child,1.0,fv); //this if(head.length() > 5 || child.length() > 5) { int hL = head.length(); int cL = child.length(); head = hL > 5 ? head.substring(0,5) : head; child = cL > 5 ? child.substring(0,5) : child; all = head + " " + headP + " " + child + " " + childP; hPos = headP + " " + child + " " + childP; cPos = head + " " + headP + " " + childP; hP = headP + " " + child; cP = head + " " + childP; oPos = headP + " " + childP; oLex = head + " " + child; fv = add("SA="+all+attDist,1.0,fv); //this fv = add("SF="+oLex+attDist,1.0,fv); //this fv = add("SAA="+all,1.0,fv); //this fv = add("SFF="+oLex,1.0,fv); //this if(cL > 5) { fv = add("SB="+hPos+attDist,1.0,fv); fv = add("SD="+hP+attDist,1.0,fv); fv = add("SK="+child+" "+childP+attDist,1.0,fv); fv = add("SM="+child+attDist,1.0,fv); //this fv = add("SBB="+hPos,1.0,fv); fv = add("SDD="+hP,1.0,fv); fv = add("SKK="+child+" "+childP,1.0,fv); fv = add("SMM="+child,1.0,fv); //this } if(hL > 5) { fv = add("SC="+cPos+attDist,1.0,fv); fv = add("SE="+cP+attDist,1.0,fv); fv = add("SH="+head+" "+headP+attDist,1.0,fv); fv = add("SJ="+head+attDist,1.0,fv); //this fv = add("SCC="+cPos,1.0,fv); fv = add("SEE="+cP,1.0,fv); fv = add("SHH="+head+" "+headP,1.0,fv); fv = add("SJJ="+head,1.0,fv); //this } } return fv; } public FeatureVector createFeatureVector(String[] toks, String[] pos, String[] posA, int word, String type, boolean attR, boolean childFeatures, FeatureVector fv) { if(!labeled) return fv; String att = ""; if(attR) att = "RA"; else att = "LA"; att+="&"+childFeatures; String w = toks[word]; String wP = pos[word]; String wPm1 = word > 0 ? pos[word-1] : "STR"; String wPp1 = word < pos.length-1 ? pos[word+1] : "END"; fv = add("NTS1="+type+"&"+att,1.0,fv); fv = add("ANTS1="+type,1.0,fv); for(int i = 0; i < 2; i++) { String suff = i < 1 ? "&"+att : ""; suff = "&"+type+suff; fv = add("NTH="+w+" "+wP+suff,1.0,fv); fv = add("NTI="+wP+suff,1.0,fv); fv = add("NTIA="+wPm1+" "+wP+suff,1.0,fv); fv = add("NTIB="+wP+" "+wPp1+suff,1.0,fv); fv = add("NTIC="+wPm1+" "+wP+" "+wPp1+suff,1.0,fv); fv = add("NTJ="+w+suff,1.0,fv); //this } return fv; } 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); } } return fv; } public FeatureVector add(String feat, double val, FeatureVector fv) { int num = dataAlphabet.lookupIndex(feat); if(num >= 0) return new FeatureVector(num,val,fv); 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; int childInt = attR ? w2 : w1; int parInt = attR ? w1 : w2; 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(FeatureVector curr = inst.fv; curr.next != null; curr = curr.next) out.writeInt(curr.index); out.writeInt(-4); out.writeObject(inst.sentence); out.writeInt(-5); out.writeObject(inst.pos); out.writeInt(-6); out.writeObject(inst.labs); out.writeInt(-7); out.writeObject(inst.actParseTree); out.writeInt(-1); out.reset(); } catch (IOException e) {} } public DependencyInstance getFeatureVector(ObjectInputStream in, DependencyInstance inst, FeatureVector[][][] fvs, double[][][] probs, FeatureVector[][][][] nt_fvs, double[][][][] nt_probs, Parameters params) throws IOException { int length = inst.length; // Get production crap. for(int w1 = 0; w1 < length; w1++) { for(int w2 = w1+1; w2 < length; w2++) { for(int ph = 0; ph < 2; ph++) { FeatureVector prodFV = new FeatureVector(-1,-1.0,null); int indx = in.readInt(); while(indx != -2) { prodFV = new FeatureVector(indx,1.0,prodFV); indx = in.readInt(); } double prodProb = params.getScore(prodFV); fvs[w1][w2][ph] = prodFV; probs[w1][w2][ph] = prodProb; } } } int last = in.readInt(); if(last != -3) { System.out.println("Error reading file."); System.exit(0); } if(labeled) { for(int w1 = 0; w1 < length; w1++) { for(int t = 0; t < types.length; t++) { String type = types[t]; for(int ph = 0; ph < 2; ph++) { for(int ch = 0; ch < 2; ch++) { FeatureVector prodFV = new FeatureVector(-1,-1.0,null); int indx = in.readInt(); while(indx != -2) { prodFV = new FeatureVector(indx,1.0,prodFV); indx = in.readInt(); } double nt_prob = params.getScore(prodFV); nt_fvs[w1][t][ph][ch] = prodFV; nt_probs[w1][t][ph][ch] = nt_prob; } } } } last = in.readInt(); if(last != -3) { System.out.println("Error reading file."); System.exit(0); } } FeatureVector nfv = new FeatureVector(-1,-1.0,null); int next = in.readInt(); while(next != -4) { nfv = new FeatureVector(next,1.0,nfv); next = in.readInt(); } String[] toks = null; String[] pos = null; String[] labs = null; String actParseTree = null; try { toks = (String[])in.readObject(); next = in.readInt(); pos = (String[])in.readObject(); next = in.readInt(); labs = (String[])in.readObject(); next = in.readInt(); actParseTree = (String)in.readObject(); next = in.readInt(); } catch(ClassNotFoundException e) { System.out.println("Error reading file."); System.exit(0); } if(next != -1) { System.out.println("Error reading file."); System.exit(0); } DependencyInstance pti = new DependencyInstance(toks,pos,labs,nfv); pti.actParseTree = actParseTree; return pti; } public void getFeatureVector(DependencyInstance inst, FeatureVector[][][] fvs, double[][][] probs, FeatureVector[][][][] nt_fvs, double[][][][] nt_probs, Parameters params) { 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); } // Get production crap. 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; int childInt = attR ? w2 : w1; int parInt = attR ? w1 : w2; FeatureVector prodFV = createFeatureVector(toks,pos,posA,w1,w2,attR, new FeatureVector(-1,-1.0,null)); double prodProb = params.getScore(prodFV); fvs[w1][w2][ph] = prodFV; probs[w1][w2][ph] = prodProb; } } } 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)); double nt_prob = params.getScore(prodFV); nt_fvs[w1][t][ph][ch] = prodFV; nt_probs[w1][t][ph][ch] = nt_prob; } } } } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -