private boolean sanityCheckValue(Feature f) { if (f.getValue() instanceof Number) { return true; } if (f.getName().equals(Constants.ID_FEATURE_NAME)) { return false; } try { Double.valueOf((String) f.getValue()); } catch (Exception e) { throw new IllegalArgumentException( "Feature [" + f.getName() + "] has a non-numeric value [" + f.getValue() + "]", e); } return false; }
protected boolean sanityCheckValue(Feature f) { if (f.getValue() instanceof Number) { return true; } if (f.getName().equals(Constants.ID_FEATURE_NAME)) { return false; } try { Double.valueOf((String) f.getValue()); } catch (Exception e) { throw new IllegalArgumentException( "Feature [" + f.getName() + "] has a non-numeric value [" + f.getValue() + "]", e); } return false; }
private void recordInstanceId(Instance instance, int i, Map<String, String> index2instanceId) { Collection<Feature> features = instance.getFeatures(); for (Feature f : features) { if (f.getName().equals(Constants.ID_FEATURE_NAME)) { index2instanceId.put(i + "", f.getValue() + ""); return; } } }
private void recordInstanceId(Instance instance, int i, Map<String, String> index2instanceId) { Collection<Feature> features = instance.getFeatures(); for (Feature f : features) { if (f.getName().equals(Constants.ID_FEATURE_NAME)) { index2instanceId.put(i + "", f.getValue() + ""); return; } } }
private String getValue(Feature feature) { if (feature.getType().equals(FeatureType.STRING) || feature.getType().equals(FeatureType.NOMINAL)) { String value = feature.getValue().toString(); String idx = stringToIntegerMap.get(value); if (idx == null) { stringToIntegerMap.put(value, "" + maxStringId++); idx = stringToIntegerMap.get(value); } return idx.toString(); } return feature.getValue().toString(); }
protected void recordInstanceId(Instance instance, int i, Map<String, String> index2instanceId) { Collection<Feature> features = instance.getFeatures(); for (Feature f : features) { if (f.getName().equals(Constants.ID_FEATURE_NAME)) { index2instanceId.put(i + "", f.getValue() + ""); return; } } }
public void collectMetaData(List<Instance> instances) { featureNames = new TreeSet<>(); for (Feature f : instances.get(0).getFeatures()) { featureNames.add(f.getName()); if (!featDesc.containsKey(f.getName())) { featDesc.put(f.getName(), f.getType()); } if (f.getType() == FeatureType.NOMINAL) { enumFeatureName.put(f.getName(), f.getValue().getClass().getName()); } } didCollect = true; }
public void collectMetaData(List<Instance> instances) { featureNames = new TreeSet<>(); for (Feature f : instances.get(0).getFeatures()) { featureNames.add(f.getName()); if (!featDesc.containsKey(f.getName())) { featDesc.put(f.getName(), f.getType()); } if (f.getType() == FeatureType.NOMINAL) { enumFeatureName.put(f.getName(), f.getValue().getClass().getName()); } } didCollect = true; }
private File createInputFile(JCas jcas) throws Exception { File tempFile = FileUtil.createTempFile("libsvm", ".txt"); tempFile.deleteOnExit(); BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(tempFile), "utf-8")); InstanceExtractor extractor = new InstanceExtractor(featureMode, featureExtractors, true); List<Instance> instances = extractor.getInstances(jcas, true); for (Instance instance : instances) { bw.write(OUTCOME_PLACEHOLDER); bw.write(injectSequenceId(instance)); for (Feature f : instance.getFeatures()) { if (!sanityCheckValue(f)) { continue; } bw.write("\t"); bw.write(featureMapping.get(f.getName()) + ":" + f.getValue()); } bw.write("\n"); } bw.close(); return tempFile; }
@Override public void process(JCas jcas) throws AnalysisEngineProcessException { Collection<TextClassificationSequence> sequences = JCasUtil.select(jcas, TextClassificationSequence.class); for (TextClassificationSequence seq : sequences) { int id = seq.getId(); for (TextClassificationTarget unit : JCasUtil.selectCovered(jcas, TextClassificationTarget.class, seq)) { String idString; try { idString = (String) InstanceIdFeature.retrieve(jcas, unit, id).getValue(); ContextMetaCollectorUtil.addContext(jcas, unit, idString, bw); } catch (Exception e) { throw new AnalysisEngineProcessException(e); } } } } }
@Override public void process(JCas jcas) throws AnalysisEngineProcessException { Collection<TextClassificationSequence> sequences = JCasUtil.select(jcas, TextClassificationSequence.class); for (TextClassificationSequence seq : sequences) { int id = seq.getId(); for (TextClassificationTarget unit : JCasUtil.selectCovered(jcas, TextClassificationTarget.class, seq)) { String idString; try { idString = (String) InstanceIdFeature.retrieve(jcas, unit, id).getValue(); ContextMetaCollectorUtil.addContext(jcas, unit, idString, bw); } catch (Exception e) { throw new AnalysisEngineProcessException(e); } } } } }
@Override public Set<Feature> extract(JCas jcas, TextClassificationTarget unit) throws TextClassificationException { try { String idString = (String) InstanceIdFeature.retrieve(jcas, unit).getValue(); ContextMetaCollectorUtil.addContext(jcas, unit, idString, bw); if (DocumentMetaData.get(jcas).getIsLastSegment() == true) { bw.close(); } } catch (IOException e) { throw new TextClassificationException(e); } return new HashSet<Feature>(); }
@Override public Set<Feature> extract(JCas jcas, TextClassificationTarget unit) throws TextClassificationException { try { String idString = (String) InstanceIdFeature.retrieve(jcas, unit).getValue(); ContextMetaCollectorUtil.addContext(jcas, unit, idString, bw); if (DocumentMetaData.get(jcas).getIsLastSegment() == true) { bw.close(); } } catch (IOException e) { throw new TextClassificationException(e); } return new HashSet<Feature>(); }
/** * * @param expectedName * expected * @param expectedValue * actual * @param features * features */ public static void assertFeatures(String expectedName, int expectedValue, Set<Feature> features) { Assert.assertNotNull(features); boolean found = false; for (Feature f : features) { if (f.getName().equals(expectedName)) { found = true; Assert.assertEquals(expectedValue, (int) f.getValue()); } } Assert.assertTrue(found); } }
/** * * @param expectedName * expected * @param expectedValue * actual * @param features * features */ public static void assertFeatures(String expectedName, int expectedValue, Set<Feature> features) { Assert.assertNotNull(features); boolean found = false; for (Feature f : features) { if (f.getName().equals(expectedName)) { found = true; Assert.assertEquals(expectedValue, (int) f.getValue()); } } Assert.assertTrue(found); } }
/** * * @param expectedName * expected * @param expectedValue * actual * @param features * feature * @param epsilon * epsilon */ public static void assertFeatures(String expectedName, double expectedValue, Set<Feature> features, double epsilon) { Assert.assertNotNull(features); boolean found = false; for (Feature f : features) { if (f.getName().equals(expectedName)) { found = true; Assert.assertEquals(expectedValue, (Double) f.getValue(), epsilon); } } Assert.assertTrue(found); }
/** * Shortcut for JUnit assert that test whether a feature has the correct name and value * * @param expectedName * expected * @param expectedValue * actual * @param actualFeature * feature */ public static void assertFeature(String expectedName, Object expectedValue, Feature actualFeature) { Assert.assertNotNull(actualFeature); Assert.assertEquals(expectedName, actualFeature.getName()); Assert.assertEquals(expectedValue, actualFeature.getValue()); }
/** * Shortcut for JUnit assert that test whether a feature has the correct name and value * * @param expectedName * expected * @param expectedValue * actual * @param actualFeature * feature */ public static void assertFeature(String expectedName, Object expectedValue, Feature actualFeature) { Assert.assertNotNull(actualFeature); Assert.assertEquals(expectedName, actualFeature.getName()); Assert.assertEquals(expectedValue, actualFeature.getValue()); }
/** * Shortcut for JUnit assert that test whether a feature has the correct name and double value * (compared using the epsilon) * * @param expectedName * expected * @param expectedValue * actual * @param actualFeature * feature * @param epsilon * epsilon */ public static void assertFeature(String expectedName, double expectedValue, Feature actualFeature, double epsilon) { Assert.assertNotNull(actualFeature); Assert.assertEquals(expectedName, actualFeature.getName()); Assert.assertEquals(expectedValue, (Double) actualFeature.getValue(), epsilon); }
/** * Shortcut for JUnit assert that test whether a feature has the correct name and double value * (compared using the epsilon) * * @param expectedName * expected * @param expectedValue * actual * @param actualFeature * feature * @param epsilon * epsilon */ public static void assertFeature(String expectedName, double expectedValue, Feature actualFeature, double epsilon) { Assert.assertNotNull(actualFeature); Assert.assertEquals(expectedName, actualFeature.getName()); Assert.assertEquals(expectedValue, (Double) actualFeature.getValue(), epsilon); }