human
I am working on the file "30. 딥러닝/05. 확률론적 언어 모형.ipynb".
The first task is:
이제 이 입력으로부터 확률값을 추정한다.
Can you write Python program for it?
assistant
#Here is the Python program you requested.

import collections, math
from math import log
from collections import Counter
from konlpy.utils import pprint

def stringify_context(context):
    return(" ".join(context))

boundaryToken = "</s>"
def ngrams(n, sentences, boundaryToken=boundaryToken, verbose=False):
    c = {}
    q = []
    for i in range(n-1):
        q.append(boundaryToken)
    for sentence in sentences:
        for w in sentence + [boundaryToken]:
            context_gram = stringify_context(q)
            if verbose:
                print(q)
                print(context_gram)
                print(w)
            if not context_gram in c:
                c[context_gram] = Counter()
            c[context_gram][w] += 1
            q.pop(0)
            q.append(w)
    return(c)

ngrams(2, sentences[:1000])["we"]

class BigramModel:
    
    def __init__(self, training_sentences, smoothing='none'):
        train = ngrams(2, training_sentences)
        self.probs = {}
        if smoothing == 'none':
            for context_gram in train.keys():
                N = sum(train[context_gram].values())
                self.probs[context_gram] = Counter({k:v/N for k,v in train[context_gram].items()})

    def prob(self, word, context):
        """takes a word string and a context which is a list of word strings, and returns the probability of the word"""
        c = stringify_context(context)
        return(self.probs[c][word])

    def scoreSentence(self, sentence, verbose=False):
        context = [boundaryToken]
        result = 0
        for w in sentence + [boundaryToken]:
            lp = log(self.prob(w, context))
            result = result + lp
            if verbose:
                pprint([context, w, lp])
            context = [w]
        return result

    def generateSentence(self, verbose=False, goryDetails=False):
        context = [boundaryToken]
        result = []
        w = None
        while not w == boundaryToken:
            r = random.random() # returns a random float between 0 and 1
            x = 0
            c =...