Ejemplo n.º 1
0
  def predict(self, X):
    """
      Uses trained support vector classifier to predict label.
    """
    f_name = os.path.join(abs_path("../model"), self.name + '.predict')

    first = True
    with open(f_name, 'w') as f:
      for x in X:
        out = ("|BT| " + str(x.getEnrichedTree(self.mode)) + " |ET|"
               + hotty(x.getConcepts()[0].label, x.getConcepts()[1].label)
               + " |EV|\n")
        f.write(out)
    for label in _LABELS.values():
      m_name = os.path.join(abs_path("../model"), self.name + '.' + label + '.svm')
      r_name = os.path.join(abs_path("../results"), self.name + '.' + label + '.predict')
      subprocess.call([abs_path('../bin/svm-light-TK-1.2.1/svm_classify'), 
                       f_name, 
                       m_name,
                       r_name])
      r_label = list()
      with open(r_name, 'r') as f:
        for line in f:
          r_label.append(float(line))
      if first:
        results = np.array(r_label).reshape(len(r_label),1)
        first = False
      else:
        results = np.concatenate((results,np.array(r_label).reshape(len(r_label),1)),axis=1)
      os.remove(r_name)    
    os.remove(f_name)
    
    return [_LABELS[r] for r in results.argmax(axis=1)]
Ejemplo n.º 2
0
  def train(self, X, Y):
    """
      Trains a classifier using a custom kernel.
      Note: X is a list of entries Y is a list of labels 
    """
    for label in _LABELS.values():
      f_name = os.path.join(abs_path('../model'), self.name + '.' + label + '.tmp')
      with open(f_name, 'w') as f:
        for y, x in zip(Y,X):
          if y == label:
            y = '1'
          else:
            y = '-1'
         
          out = (str(y) + " |BT| " + str(x.getEnrichedTree(self.mode)) + " |ET|"
                 + hotty(x.getConcepts()[0].label, x.getConcepts()[1].label)
                 + " |EV|\n")
          f.write(out)

      subprocess.call([abs_path('../bin/svm-light-TK-1.2.1/svm_learn'), 
                       '-t', '5',
                       '-S', '1',
                       '-D', '1',
                       '-r', '1',
                       '-c', '0.5',
                       '-d', '2',
                       '-T', '3.35',
                       '-N', '3',
                       '-W', 'S',
                       '-v', '0',
                       f_name,
                       f_name.split('.tmp')[0] + '.svm',
                      ])
Ejemplo n.º 3
0
  # Create Model
  if (v):
    sys.stdout.write("\tCreating model...\n")
  
  if model_type == 'svm-spt':
    svm.MODE = 'spt'
    ml = svm
  elif model_type == 'svm-insert':
    svm.MODE = 'insert'
    ml = svm
  elif model_type == 'svm-suffix':
    svm.MODE = 'suffix'
    ml = svm
  else:
    sys.stderr.write("ERROR: Invalid model type.\n")
    sys.exit(1)
  
  model = ml.Model()
  model.train(entries, labels)

  # Save model for later use
  if (v):
    sys.stdout.write("\tPickling model to %s...\n" % model_path)
  model.save(model_path)

if __name__ == "__main__":
  main(abs_path('./i2b2_examples/'), abs_path('../model/example-spt.mod'), 'svm-spt', True)
  main(abs_path('./i2b2_examples/'), abs_path('../model/example-insert.mod'), 'svm-insert', True)
  main(abs_path('./i2b2_examples/'), abs_path('../model/example-suffix.mod'), 'svm-suffix', True)
Ejemplo n.º 4
0
    total_p_labels += p_labels
    total_g_labels += g_labels

  # Calculate Precision and Recall Using true positive, false positive, false
  # negative for relation detection
  precision = float(total_TP) / (total_TP + total_FP)
  recall    = float(total_TP) / (total_TP + total_FN)
  F1        = 2 * (precision * recall) / (precision + recall)

  with open(results_file, "w") as f:
    print >>f, "-" * 80
    print >>f, "Relation Detection Statistics:"
    print >>f, ""
    print >>f, "\tTrue positives: ",  total_TP
    print >>f, "\tFalse positives:",  total_FP
    print >>f, "\tFalse negatives:",  total_FN
    print >>f, ""
    print >>f, "\tPrecision: %.4f \t Recall: %.4f \t F1: %.4f" % (precision, recall, F1)
    print >>f, ""
    print >>f, "-" * 80
    print >>f, "Classification Statistics:"
    print >>f, ""
    print >>f, classification_report(total_g_labels, total_p_labels)
    print >>f, "-" * 80
    print >>f, ""

if __name__ == "__main__":
  main(abs_path("../results/"), 
       abs_path("./i2b2_examples/rel/"),
       abs_path("../results/i2b2_results"), True)