コード例 #1
0
def getResponse(input):
    input = input.strip().split(" ")
    for i in range(len(input)):
        input[i] = input[i].lower()
    try:
        question = input[-1][-1] == "?"
    except:
        question = False
    scorer.processInput(input)

    bestResponse = ""
    bestScore = 0

    for i in range(chains):
        # noinspection PyRedeclaration
        currentWord = random.choice(["My", "The", "When", "I", "What"])
        response = [currentWord]
        while True:
            if len(response) > 1:
                newWord = nextWord(currentWord, response[-2])
            else:
                newWord = nextWord(currentWord)
            response.append(newWord)
            currentWord = newWord
            if newWord[-1] in "?!.":
                break
        score = scorer.getScore(input, question, response)
        if score >= bestScore:
            bestResponse = response
            bestScore = score

    print(bestScore)
    return bestResponse
コード例 #2
0
ファイル: generate.py プロジェクト: petevieira/grammar
def perplexity(s, t):
    N = len(s)

    l = zip(map(lambda w: (w,),s), xrange(N-1))
    l.sort()
    l = scorer.getScore(l, 1)
    l.sort()

    l = map(lambda (id, g, score):
        math.log(float(score)/float(t)) * 1.0/N , l)
    return math.exp(-sum(l))
コード例 #3
0
ファイル: spell.py プロジェクト: petevieira/grammar
def correct(text, between=False):
    sents = splitSentence(text)
    if between:
        sents = map(lambda s: list(intersperse(s, "")), sents)
    print len(sents),
    sys.stdout.flush()
    candSents = confusionSets(sents)
    print len(candSents),
    sys.stdout.flush()

    scoreEvaler = [-0.01,1,2,3,4,5]
    scoredgrams = []
    for n in xrange(1,6):
        print n,
        sys.stdout.flush()
        # create trigrams from candidate sentences
        grams = []
        cid = 0
        for (cs, sid) in candSents:
            cs = map(operator.itemgetter(1), cs)
            grams += gramify(n, cs, (sid, cid, n))
            cid += 1

        #run score function
        #scoredgrams = map(lambda (t0, t1, t2, ((sid, cid), wid)):
        #        (sid, cid, wid, t1, 1), trigrams)
        grams.sort()
        (sg, count, total) = scorer.getScore(grams, n)
        scoredgrams += sg
        scoreEvaler[n] = float(n*n*n*n)/total

    scoredgrams.sort()
    #print scoredgrams

    nsentences = []
    bsid = 0
    bcid = 0
    cand = []
    candS = []
    candidates = []
    for (((sid, cid, n), wid), t, score) in scoredgrams + [(((None, None, None), None), None, None)]:
        if (bcid != cid or bsid != sid) and cand != []:
            #print cand
            candidates.append( calcScore(cand, sents[bsid], scoreEvaler) )
            cand = []
        if bsid != sid:
            candidates.sort(reverse=True)
            map(printf,candidates)
            print
            nsentences.append(candidates[0][2])
            candidates = []
        cand.append( (t, score, wid, n) )
        #print sid,cid,wid, cand
        bsid = sid
        bcid = cid
    #print
    #print scoredgrams
    #print
    #print nsentences
    return " ".join(map(lambda s: " ".join(filter(lambda x: x!=""
                                                 ,s))
                       ,nsentences))