package se.lth.cs.nlp.opinions; import java.io.Serializable; import java.util.*; import se.lth.cs.nlp.nlputils.ml_long.SparseVector; public class FeatureTemplateSet implements Serializable { private static final long serialVersionUID = 0L; private FeatureTemplate[][] templates; private long idOffset; public FeatureTemplateSet(String s, int offset) { s = s.replaceAll("#.*", " "); s = s.trim(); if(s.endsWith(",")) s = s.substring(0, s.length() - 1); s = s.trim(); String[] ts = s.equals("")? new String[0]: s.split(","); //System.out.println(Arrays.toString(ts)); idOffset = (long) offset; if(ts.length >= 1L << (64L - idOffset)) throw new IllegalArgumentException("Too many feature templates"); ArrayList> l = new ArrayList(); for(int i = 0; i < ts.length; i++) { FeatureTemplate t = new FeatureTemplate(ts[i], offset); t.id = (long) (i + 1) << idOffset; int r = t.getRange(); if(r >= l.size()) { for(int j = l.size(); j <= r; j++) l.add(new ArrayList()); } l.get(r).add(t); } templates = new FeatureTemplate[l.size()][]; for(int i = 0; i < templates.length; i++) templates[i] = l.get(i).toArray(new FeatureTemplate[0]); } public void extractFeatures(SparseVector sv, int[][] ifs, int[] ofs, int index, int range) { if(range >= templates.length) return; for(FeatureTemplate t: templates[range]) sv.put(t.encodeFeature(ifs, ofs, index), 1.0); } public String svToString(SparseVector sv) { StringBuilder out = new StringBuilder(); for(int i = 0; i < sv.index; i++) { out.append(featureToString(sv.keys[i])); out.append(":" + sv.values[i]); if(i < sv.index - 1) out.append(" "); } return out.toString(); } public String featureToString(long l) { System.out.println("templates.length = " + templates.length); long mask = ((long) -1) << idOffset; long id = l & mask; for(int i = 0; i < templates.length; i++) { System.out.println("templates[" + i + "].length = " + templates[i].length); //System.out.println("checking range " + i); for(FeatureTemplate t: templates[i]) { System.out.println("testing id " + t.id); if(t.id == id) return t.featureToString(l); } } throw new IllegalArgumentException("feature id " + id + " not found"); } public int getMaximumRange() { return templates.length; } }