Пример #1
0
  def testFeedTermReturnsTerm(self):
    model = Model()
    term = Term().createFromString("test")

    result = model.feedTerm(term)

    self.assertIsInstance(result, Term, "Result is not a Term")
Пример #2
0
def getModel(uid):
  if uid in modelCache:
    return modelCache[uid]

  modelDir = _getModelDir(uid)

  if not os.path.exists(modelDir):
    os.makedirs(modelDir)

  model = Model(checkpointDir=modelDir)

  if model.hasCheckpoint():
    model.load()

  modelCache[uid] = model
  return model
Пример #3
0
  def test_checkpoint(self):
    model1 = Model(checkpointDir=MODEL_CHECKPOINT_DIR)
    term = Term().createFromString("fox")

    for _ in range(5):
      prediction = model1.feedTerm(term)

    self.assertTrue(len(prediction.bitmap))
    model1.save()

    model2 = Model(checkpointDir=MODEL_CHECKPOINT_DIR)
    model2.load()

    prediction = model2.feedTerm(term)

    self.assertTrue(len(prediction.bitmap))
Пример #4
0
  def test_checkpoint(self):
    model1 = Model(checkpointDir=MODEL_CHECKPOINT_DIR)
    term = Term().createFromString("fox")

    for _ in range(5):
      prediction = model1.feedTerm(term)

    self.assertTrue(len(prediction.bitmap))
    model1.save()

    model2 = Model(checkpointDir=MODEL_CHECKPOINT_DIR)
    model2.load()

    prediction = model2.feedTerm(term)

    self.assertTrue(len(prediction.bitmap))
Пример #5
0
  parser.add_option(
      "-f",
      "--format",
      dest="format",
      help="Format to output (ie: csv)",
      metavar="FORMAT")
  parser.add_option(
      "-r",
      "--reset-sequences",
      dest="resetSequences",
      action="store_true",
      default=False,
      help="Reset the model sequence after every line")

  (options, args) = parser.parse_args()

  if not len(args):
    parser.print_help()
    print
    raise(Exception("file required"))

  model = Model(checkpointDir=options.checkpoint)

  try:
    readFile(args[0], model,
             resetSequences=options.resetSequences, format=options.format)
  except KeyboardInterrupt:
    if model.canCheckpoint():
      print("Saving model before exiting...")
      model.save()
Пример #6
0
  def testSaveWithoutCheckpointDirectory(self):
    model = Model()

    with self.assertRaises(Exception) as e:
      model.save()
    self.assertIn("No checkpoint directory specified", e.exception)
Пример #7
0
  def testLoadWithoutCheckpointFile(self):
    model = Model(checkpointDir=MODEL_CHECKPOINT_DIR)

    with self.assertRaises(Exception) as e:
      model.load()
    self.assertIn("Could not find checkpoint file", e.exception)
Пример #8
0
  def test_training(self):
    term0 = Term().createFromString("the")
    term1 = Term().createFromString("fox")
    term2 = Term().createFromString("eats")
    term3 = Term().createFromString("rodent")

    model = Model()

    prediction = model.feedTerm(term0)
    self.assertFalse(len(prediction.bitmap))

    for _ in range(5):
      model.feedTerm(term1)
      model.feedTerm(term2)
      model.feedTerm(term3)
      model.resetSequence()

    model.feedTerm(term1)
    prediction = model.feedTerm(term2)

    self.assertEqual(prediction.closestString(), "rodent")
Пример #9
0
  def test_training(self):
    term0 = Term().createFromString("the")
    term1 = Term().createFromString("fox")
    term2 = Term().createFromString("eats")
    term3 = Term().createFromString("rodent")

    model = Model()

    prediction = model.feedTerm(term0)
    self.assertFalse(len(prediction.bitmap))

    for _ in range(5):
      model.feedTerm(term1)
      model.feedTerm(term2)
      model.feedTerm(term3)
      model.resetSequence()

    model.feedTerm(term1)
    prediction = model.feedTerm(term2)

    self.assertEqual(prediction.closestString(), "rodent")
Пример #10
0
            if resetSequences:
                model.resetSequence()

            s += 1
            t = 1


if __name__ == '__main__':
    parser = OptionParser("%prog file [options]")
    parser.add_option("--checkpoint",
                      dest="checkpoint",
                      help="Directory to save model to and load model from")
    parser.add_option("-r",
                      "--reset-sequences",
                      dest="resetSequences",
                      action="store_true",
                      default=False,
                      help="Reset the model sequence after every line")

    (options, args) = parser.parse_args()

    if not len(args):
        parser.print_help()
        print
        raise (Exception("file required"))

    model = Model(checkpointDir=options.checkpoint)

    readFile(args[0], model, resetSequences=options.resetSequences)