Package org.apache.mahout.classifier

Examples of org.apache.mahout.classifier.ClassifierResult


        double[] probs   = categorizer.categorize(tokens); //<co id="tmt.categorize"/>
        String label     = categorizer.getBestCategory(probs);
        int    bestIndex = categorizer.getIndex(label);
        double score     = probs[bestIndex];

        ClassifierResult result //<co id="tmt.collect"/>
          = new ClassifierResult(label, score);
        resultAnalyzer.addInstance(parts[0], result);
      }
      in.close();
    }
   
View Full Code Here


     
      File[] inputFiles = FileUtil.buildFileList(f);
     
      String line = null;
      //<start id="lucene.examples.mlt.test"/>
      final ClassifierResult UNKNOWN = new ClassifierResult("unknown",
              1.0);
     
      ResultAnalyzer resultAnalyzer = //<co id="co.mlt.ra"/>
        new ResultAnalyzer(categorizer.getCategories(),
            UNKNOWN.getLabel());

      for (File ff: inputFiles) { //<co id="co.mlt.read"/>
        BufferedReader in =
            new BufferedReader(
                new InputStreamReader(
                    new FileInputStream(ff),
                    "UTF-8"));
        while ((line = in.readLine()) != null) {
          String[] parts = line.split("\t");
          if (parts.length != 2) {
            continue;
          }
         
          CategoryHits[] hits //<co id="co.mlt.cat"/>
            = categorizer.categorize(new StringReader(parts[1]));
          ClassifierResult result = hits.length > 0 ? hits[0] : UNKNOWN;
          resultAnalyzer.addInstance(parts[0], result); //<co id="co.mlt.an"/>
        }
       
        in.close();
      }
View Full Code Here

  public void classifyDocument(SolrInputDocument doc) throws IOException {
    try {
      //<start id="mahout.bayes.classify"/>
      SolrInputField field = doc.getField(inputField);
      String[] tokens = tokenizeField(inputField, field);
      ClassifierResult result = ctx.classifyDocument(tokens,
              defaultCategory);
      if (result != null && result.getLabel() != NO_LABEL) {
        doc.addField(outputField, result.getLabel());
      }
      //<end id="mahout.bayes.classify"/>
    }
    catch (InvalidDatastoreException e) {
      throw new IOException("Invalid Classifier Datastore", e);
View Full Code Here

          for (Map.Entry<String,List<String>> stringListEntry : document.entrySet()) {
            String correctLabel = stringListEntry.getKey();
            List<String> strings = stringListEntry.getValue();
            TimingStatistics.Call call = operationStats.newCall();
            TimingStatistics.Call outercall = totalStatistics.newCall();
            ClassifierResult classifiedLabel = classifier.classifyDocument(strings.toArray(new String[strings
                .size()]), params.get("defaultCat"));
            call.end();
            outercall.end();
            boolean correct = resultAnalyzer.addInstance(correctLabel, classifiedLabel);
            if (verbose) {
              // We have one document per line
              log.info("Line Number: {} Line(30): {} Expected Label: {} Classified Label: {} Correct: {}",
                new Object[] {lineNum, line.length() > 30 ? line.substring(0, 30) : line, correctLabel,
                              classifiedLabel.getLabel(), correct,});
            }
            // log.info("{} {}", correctLabel, classifiedLabel);
           
          }
          lineNum++;
View Full Code Here

 
  @Override
  public ClassifierResult classifyDocument(String[] document,
                                           Datastore datastore,
                                           String defaultCategory) throws InvalidDatastoreException {
    ClassifierResult result = new ClassifierResult(defaultCategory);
    double max = Double.MAX_VALUE;
    Collection<String> categories = datastore.getKeys("labelWeight");
   
    for (String category : categories) {
      double prob = documentWeight(datastore, category, document);
      if (prob < max) {
        max = prob;
        result.setLabel(category);
      }
    }
    result.setScore(max);
    return result;
  }
View Full Code Here

    PriorityQueue<ClassifierResult> pq = new PriorityQueue<ClassifierResult>(numResults,
        new ByScoreLabelResultComparator());
    for (String category : categories) {
      double prob = documentWeight(datastore, category, document);
      if (prob > 0.0) {
        pq.add(new ClassifierResult(category, prob));
        if (pq.size() > numResults) {
          pq.remove();
        }
      }
    }
   
    if (pq.isEmpty()) {
      return new ClassifierResult[] {new ClassifierResult(defaultCategory, 0.0)};
    } else {
      List<ClassifierResult> result = new ArrayList<ClassifierResult>(pq.size());
      while (pq.isEmpty() == false) {
        result.add(pq.remove());
      }
View Full Code Here

 
  @Override
  public ClassifierResult classifyDocument(String[] document,
                                           Datastore datastore,
                                           String defaultCategory) throws InvalidDatastoreException {
    ClassifierResult result = new ClassifierResult(defaultCategory);
    double max = Double.MIN_VALUE;
    Collection<String> categories = datastore.getKeys("labelWeight");
   
    for (String category : categories) {
      double prob = documentWeight(datastore, category, document);
      if (max < prob) {
        max = prob;
        result.setLabel(category);
      }
    }
    result.setScore(max);
    return result;
  }
View Full Code Here

    PriorityQueue<ClassifierResult> pq = new PriorityQueue<ClassifierResult>(numResults,
        new ByScoreLabelResultComparator());
    for (String category : categories) {
      double prob = documentWeight(datastore, category, document);
      if (prob > 0.0) {
        pq.add(new ClassifierResult(category, prob));
        if (pq.size() > numResults) {
          pq.remove();
        }
      }
    }
   
    if (pq.isEmpty()) {
      return new ClassifierResult[] {new ClassifierResult(defaultCategory, 0.0)};
    } else {
      List<ClassifierResult> result = new ArrayList<ClassifierResult>(pq.size());
      while (pq.isEmpty() == false) {
        result.add(pq.remove());
      }
View Full Code Here

  }
 
  public void test() throws InvalidDatastoreException {
    ClassifierContext classifier = new ClassifierContext(algorithm, store);
    String[] document = {"aa", "ff"};
    ClassifierResult result = classifier.classifyDocument(document, "unknown");
    assertNotNull("category is null and it shouldn't be", result);
    assertEquals(result + " is not equal to e", "e", result.getLabel());
   
    document = new String[] {"dd"};
    result = classifier.classifyDocument(document, "unknown");
    assertNotNull("category is null and it shouldn't be", result);
    assertEquals(result + " is not equal to d", "d", result.getLabel());
   
    document = new String[] {"cc"};
    result = classifier.classifyDocument(document, "unknown");
    assertNotNull("category is null and it shouldn't be", result);
    assertEquals(result + " is not equal to d", "d", result.getLabel());
  }
View Full Code Here

  }
 
  public void testResults() throws Exception {
    ClassifierContext classifier = new ClassifierContext(algorithm, store);
    String[] document = {"aa", "ff"};
    ClassifierResult result = classifier.classifyDocument(document, "unknown");
    assertNotNull("category is null and it shouldn't be", result);
    System.out.println("Result: " + result);
  }
View Full Code Here

TOP

Related Classes of org.apache.mahout.classifier.ClassifierResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.