def sampleFromModel(prefix='', length=10): """Sample symbols from the model.""" startPos = seq.size() for c in prefix: seq.push_back(ord(c)) endPos = seq.size() out = [] for i in xrange(length): predictive = model.predictiveDistribution(startPos, endPos) sample = libplump.sample_unnormalized_pdf(predictive) out.append(chr(sample)) seq.push_back(sample) endPos = seq.size() return out
def completion(): """Simple command line word completion example.""" key = None startPos = seq.size() endPos = seq.size() while key != 27: key = ord(getch()) if key == 127: # backspace seq.pop_back() endPos -= 1 sys.stdout.write('\b') sys.stdout.write(' ') sys.stdout.write('\b') elif key == 13: #return sys.stdout.write('\n') startPos = seq.size() endPos = seq.size() elif key == 9: # tab pressed -> predict (map) sample = 0 while chr(int(sample)) != ' ': predictive = model.predictiveDistribution(startPos, endPos) sample = int(np.argmax(predictive)) sys.stdout.write(chr(sample)) seq.push_back(sample) endPos += 1 elif key == 49: # 1 pressed -> predict (sample) sample = 0 while chr(int(sample)) != ' ': predictive = model.predictiveDistribution(startPos, endPos) sample = libplump.sample_unnormalized_pdf(predictive) sys.stdout.write(chr(sample)) seq.push_back(sample) endPos += 1 else: sys.stdout.write(chr(key)) seq.push_back(key) endPos += 1