{"id":8935,"date":"2024-05-02T15:01:01","date_gmt":"2024-05-02T07:01:01","guid":{"rendered":""},"modified":"2024-05-02T15:01:01","modified_gmt":"2024-05-02T07:01:01","slug":"\u6e38\u79bb\u6001GLZ\u7684NLP\u4efb\u52a11\uff1a\u62fc\u5199\u7ea0\u9519","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8935.html","title":{"rendered":"\u6e38\u79bb\u6001GLZ\u7684NLP\u4efb\u52a11\uff1a\u62fc\u5199\u7ea0\u9519"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u5f53\u6211\u4eec\u4f7f\u7528\u641c\u7d22\u5f15\u64ce\u7684\u65f6\u5019\uff0c\u7ecf\u5e38\u4f1a\u53d1\u73b0\u6211\u4eec\u6253\u9519\u4e86\u6211\u4eec\u60f3\u8981\u68c0\u7d22\u7684\u4e1c\u897f\uff0c\u4f46\u662f\u641c\u7d22\u5f15\u64ce\u4ecd\u65e7\u7ed9\u4e86\u6211\u4eec\u6b63\u786e\u7684\u7b54\u6848\u3002\u6bd4\u5982\u6211\u4eec\u628a\"python\"\u6253\u6210\u4e86\"pathon\"\uff0c\u767e\u5ea6\u6210\u529f\u8bc6\u522b\u4e86\u51fa\u6765\u6211\u4eec\u771f\u6b63\u60f3\u8981\u7684\u3002<\/p>\n

\"\u6e38\u79bb\u6001GLZ\u7684NLP\u4efb\u52a11\uff1a\u62fc\u5199\u7ea0\u9519<\/p>\n

1.\u5229\u7528\u7f16\u8f91\u8ddd\u79bb\u751f\u6210\u5019\u9009\u96c6\u5408<\/h2>\n

\u5b9e\u73b0\u62fc\u5199\u7ea0\u9519\u65f6\u9996\u5148\u9700\u8981\u751f\u6210\u53ef\u80fd\u6b63\u786e\u8bcd\u7684\u5019\u9009\u96c6\u5408\u3002\u5176\u6838\u5fc3\u5728\u4e8e\u7f16\u8f91\u8ddd\u79bb\u8fd9\u4e00NLP\u4efb\u52a1\u7684\u5e38\u7528\u57fa\u7840\u7b97\u6cd5\u3002\u7f16\u8f91\u8ddd\u79bb\u7b49\u4e8e\u628a\u4e00\u4e2a\u5b57\u7b26\u4e32\u901a\u8fc7\u5220\u9664\u3001\u4fee\u6539\u3001\u63d2\u5165\u4e09\u79cd\u64cd\u4f5c\u6539\u4e3a\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u6700\u77ed\u8ddd\u79bb\uff08\u5f3a\u70c8\u5efa\u8bae\u5237\u4e00\u4e0b\u8fd9\u9053DP\u9898\uff09\u3002<\/p>\n

\u751f\u6210\u53ef\u80fd\u6b63\u786e\u8bcd\u7684\u5019\u9009\u96c6\u5408\u65f6\uff0c\u6211\u4eec\u9700\u8981\u9884\u5148\u51c6\u5907\u597d\u4e00\u4e2a\u8bcd\u5178\u5e93\uff0c\u4ee3\u8868\u5e38\u89c1\u7684\u8bcd\u6c47\uff08\u4e00\u822c\u8ba4\u4e3a\u8fd9\u4e9b\u662f\u6b63\u786e\u7684\uff09\u3002\u5f53\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u53ef\u80fd\u62fc\u5199\u9519\u8bef\u7684\u8bcd\u65f6\uff0c\u6211\u4eec\u751f\u6210\u7f16\u8f91\u8ddd\u79bb\u4e00\u5b9a\u7684\u5019\u9009\u8bcd\uff0c\u628a\u8fd9\u4e9b\u5019\u9009\u8bcd\u548c\u8bcd\u5e93\u4e2d\u7684\u8bcd\u5bf9\u6bd4\uff0c\u5982\u679c\u4e00\u6837\u5219\u8ba4\u4e3a\u53ef\u80fd\u662f\u7528\u6237\u62fc\u9519\u7684\u8f93\u5165\u771f\u6b63\u60f3\u8981\u7684\u8bcd\u3002<\/p>\n

\u4e3a\u4e86\u5b9e\u73b0\u65b9\u4fbf\u9009\u62e9\u7f16\u8f91\u8ddd\u79bb\u4e3a1\uff1a<\/p>\n

#\u52a0\u8f7d\u8bcd\u5178\u5e93\uff0c\u6ce8\u610f\u7528set\uff0c\u68c0\u7d22\u590d\u6742\u5ea6\u4e3a0\uff081\uff09<\/span>\nvocab =<\/span> set<\/span>(<\/span>line.<\/span>rstrip(<\/span>)<\/span> for<\/span> line in<\/span> open<\/span>(<\/span>\"vocab.txt\"<\/span>)<\/span>)<\/span>\n\n#\u9009\u51fa\u6240\u6709\u4e0e\u7528\u6237\u8f93\u5165\u7f16\u8f91\u8ddd\u79bb\u4e3a1\u7684\u8bcd\u4f5c\u4e3a\u5019\u9009<\/span>\ndef<\/span> generate_candidates<\/span>(<\/span>input_word)<\/span>:<\/span>\n    ''' input_word:\u7528\u6237\u8f93\u5165\uff08\u53ef\u80fd\u6709\u9519\uff09 return:\u4e0e\u7528\u6237\u8f93\u5165\u7f16\u8f91\u8ddd\u79bb\u4e3a1\u7684\u5019\u9009\u8bcd '''<\/span>\n    \n    # \u6240\u6709\u8f93\u5165\u90fd\u5df2\u7ecf\u5f52\u4e00\u5316\u4e3a\u5c0f\u5199<\/span>\n    letters =<\/span> 'abcdefghijklmnopqrstuvwxyz'<\/span>\n    \n    #\u904d\u5386\u6240\u6709\u8f93\u5165\u62c6\u5f00\u7684\u60c5\u51b5\uff08\u62c6\u5f00\u6210\u4e3a\u539f\u59cb\u8f93\u5165\u548c\u7a7a\u4e5f\u7b97\uff09<\/span>\n    splits =<\/span> [<\/span>(<\/span>input_word[<\/span>:<\/span>i]<\/span>,<\/span>input_word[<\/span>i:<\/span>]<\/span>)<\/span> for<\/span> i in<\/span> range<\/span>(<\/span>len<\/span>(<\/span>input_word)<\/span>+<\/span>1<\/span>)<\/span>]<\/span>\n    #\u63d2\u5165\u60c5\u51b5<\/span>\n    inserts =<\/span> [<\/span>L +<\/span> c +<\/span> R for<\/span> L,<\/span>R in<\/span> splits for<\/span> c in<\/span> letters]<\/span>\n    #\u5220\u9664\u60c5\u51b5<\/span>\n    deletes =<\/span> [<\/span>L +<\/span> R[<\/span>1<\/span>:<\/span>]<\/span> for<\/span> L,<\/span>R in<\/span> splits]<\/span>\n    #\u4fee\u6539\u60c5\u51b5<\/span>\n    replaces =<\/span> [<\/span>L +<\/span> c +<\/span> R[<\/span>1<\/span>:<\/span>]<\/span> for<\/span> L,<\/span>R in<\/span> splits for<\/span> c in<\/span> letters]<\/span>\n    \n    candidates =<\/span> set<\/span>(<\/span>inserts +<\/span> deletes +<\/span> replaces)<\/span>\n    candidates =<\/span> [<\/span>word for<\/span> word in<\/span> candidates if<\/span> word in<\/span> vocab]<\/span>\n    \n    return<\/span> candidates\n<\/code><\/pre>\n

\u6d4b\u8bd5\u6548\u679c\uff1a
\"\u6e38\u79bb\u6001GLZ\u7684NLP\u4efb\u52a11\uff1a\u62fc\u5199\u7ea0\u9519
\u6ce8\u610f\u5b9e\u73b0\u7684\u65f6\u5019\u8bcd\u5178\u5e93\u4f7f\u7528set\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u6837\u68c0\u7d22\u7684\u4ee3\u4ef7\u662fO(1)\u3002\u6574\u4f53\u7b97\u6cd5\u65f6\u95f4\u590d\u6742\u5ea6\uff1a
1.\u62c6\u5206\u8f93\u5165\uff1aO(m)(m\u4e3a\u7528\u6237\u8f93\u5165\u957f\u5ea6)
2.\u5220\u9664\u3001\u63d2\u5165\u3001\u4fee\u6539\u65b9\u6cd5\u7684\u5019\u9009\u8bcd\u751f\u6210:O(lm)(l\u4e3a\u53ef\u80fd\u7684\u5b57\u7b26\u6570\u91cf)
3.\u7b5b\u9009\u8bcd\uff1aO(n)(n\u4e3a\u751f\u6210\u7684\u5019\u9009\u8bcd\u6570\u91cf\uff0c\u67e5\u627e\u8bcd\u5178\u5e93O(1))
\u603b\u4f53T = O(l<\/em>m + n)\uff0c\u4ee3\u4ef7\u5e76\u4e0d\u5927\u3002<\/p>\n

2.\u5229\u7528\u62fc\u9519\u7684\u5386\u53f2\u8bb0\u5f55\u7edf\u8ba1\u62fc\u9519\u6982\u7387<\/h2>\n

\u6211\u4eec\u8bfb\u53d6\u51c6\u5907\u597d\u7684\u62fc\u5199\u9519\u8bef\u5386\u53f2\u8bb0\u5f55\uff0c\u5f97\u5230\u628a\u67d0\u4e2a\u5355\u8bcd\u62fc\u5199\u9519\u8bef\u7684\u5b57\u5178\uff0c\u5e76\u4e14\u8ba4\u4e3a\u62fc\u5199\u9519\u8bef\u6210\u8fd9\u4e9b\u5355\u8bcd\u662f\u7b49\u6982\u7387\u7684\u3002<\/p>\n

def<\/span> channel_probability<\/span>(<\/span>)<\/span>:<\/span>\n    channel_prob =<\/span> { \n   <\/span>}<\/span>\n    \n    for<\/span> line in<\/span> open<\/span>(<\/span>\"spell-errors.txt\"<\/span>)<\/span>:<\/span>\n        line =<\/span> line.<\/span>split(<\/span>\":\"<\/span>)<\/span>\n        correct =<\/span> line[<\/span>0<\/span>]<\/span>.<\/span>strip(<\/span>)<\/span>\n        mistack =<\/span> [<\/span>word.<\/span>strip(<\/span>)<\/span> for<\/span> word in<\/span> line[<\/span>1<\/span>]<\/span>.<\/span>strip(<\/span>'\\n'<\/span>)<\/span>.<\/span>split(<\/span>','<\/span>)<\/span>]<\/span>\n        channel_prob[<\/span>correct]<\/span> =<\/span> { \n   <\/span>}<\/span>\n        for<\/span> mis in<\/span> mistack:<\/span>\n            channel_prob[<\/span>correct]<\/span>[<\/span>mis]<\/span> =<\/span> 1<\/span>\/<\/span>len<\/span>(<\/span>mistack)<\/span>\n    return<\/span> channel_prob\n<\/code><\/pre>\n

3.bigram\u8bed\u8a00\u6a21\u578b<\/h2>\n

\u5229\u7528nltk\u7684\u8def\u900f\u793e\u8bed\u6599\u5e93\u7edf\u8ba1\u5355\u8bcd\u4ee5\u53ca\u4e24\u4e2a\u5355\u8bcd\u76f8\u8fde\u51fa\u73b0\u7684\u6982\u7387\u3002\u53e5\u9996\u589e\u52a0\u8d77\u59cb\u7b26 < s > <s> <\/span><\/span><<\/span><\/span><\/span><\/span>s<\/span><\/span>><\/span><\/span><\/span><\/span><\/span>\u7528\u6765\u4f53\u73b0\u53e5\u9996\u51fa\u73b0\u67d0\u6b21\u7684\u6982\u7387\u3002<\/p>\n

# \u6839\u636e\u8def\u900f\u793e\u8bed\u6599\u5e93\u7edf\u8ba1\u6bcf\u4e2a\u8bcd\u51fa\u73b0\u7684\u9891\u7387\u4ee5\u53ca\u5176bigram\u7684\u9891\u7387,\u7528\u4e8e\u8ba1\u7b97p\uff08w2|w1\uff09<\/span>\ndef<\/span> language_model<\/span>(<\/span>)<\/span>:<\/span>\n    word_count=<\/span>{ \n   <\/span>}<\/span>\n    bigram_count=<\/span>{ \n   <\/span>}<\/span>\n    for<\/span> doc in<\/span> corpus:<\/span>\n        #\u589e\u52a0\u53e5\u5b50\u8d77\u59cb\u7b26\uff0c\u53ef\u4ee5\u8003\u8651\u5230\u8d77\u70b9\u8bcd\u7684\u6982\u7387<\/span>\n        doc =<\/span> [<\/span>'<s>'<\/span>]<\/span> +<\/span> doc\n        for<\/span> i in<\/span> range<\/span>(<\/span>len<\/span>(<\/span>doc)<\/span>-<\/span>1<\/span>)<\/span>:<\/span>\n            #bigram=[w_i,w_i+1]<\/span>\n            word =<\/span> doc[<\/span>i]<\/span>\n            bigram =<\/span> doc[<\/span>i:<\/span>i+<\/span>2<\/span>]<\/span>\n            bigram =<\/span> ' '<\/span>.<\/span>join(<\/span>bigram)<\/span>\n            if<\/span> word not<\/span> in<\/span> word_count:<\/span>\n                word_count[<\/span>word]<\/span> =<\/span> 1<\/span>\n            else<\/span>:<\/span>\n                word_count[<\/span>word]<\/span> +=<\/span> 1<\/span>\n            if<\/span> bigram not<\/span> in<\/span> bigram_count:<\/span>\n                bigram_count[<\/span>bigram]<\/span> =<\/span> 1<\/span>\n            else<\/span>:<\/span>\n                bigram_count[<\/span>bigram]<\/span> +=<\/span> 1<\/span>\n    return<\/span> [<\/span>word_count,<\/span>bigram_count]<\/span>\n<\/code><\/pre>\n

4.\u62fc\u5199\u7ea0\u9519\u7684\u5b9e\u73b0<\/h2>\n

\u7ed9\u5b9a\u67d0\u4e2a\u9519\u8bef\u62fc\u5199mistake\uff0c\u5176\u771f\u6b63\u5e94\u8be5\u662f\u67d0\u5355\u8bcdcorrect\uff0c\u8fd9\u4e2a\u6982\u7387\u8868\u793a\u4e3a\uff1a
p ( c o r r e c t \u2223 m i s t a c k ) = p ( m i s t a c k \u2223 c o r r e c t ) p ( c o r r e c t ) p ( m i s t a c k ) p(correct|mistack) = \\frac{p(mistack|correct)p(correct)}{p(mistack)} <\/span><\/span>p<\/span>(<\/span>c<\/span>o<\/span>r<\/span>r<\/span>e<\/span>c<\/span>t<\/span>\u2223<\/span>m<\/span>i<\/span>s<\/span>t<\/span>a<\/span>c<\/span>k<\/span>)<\/span><\/span>=<\/span><\/span><\/span><\/span><\/span><\/span>p<\/span>(<\/span>m<\/span>i<\/span>s<\/span>t<\/span>a<\/span>c<\/span>k<\/span>)<\/span><\/span><\/span><\/span><\/span><\/span><\/span>p<\/span>(<\/span>m<\/span>i<\/span>s<\/span>t<\/span>a<\/span>c<\/span>k<\/span>\u2223<\/span>c<\/span>o<\/span>r<\/span>r<\/span>e<\/span>c<\/span>t<\/span>)<\/span>p<\/span>(<\/span>c<\/span>o<\/span>r<\/span>r<\/span>e<\/span>c<\/span>t<\/span>)<\/span><\/span><\/span><\/span>\u200b<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/p>\n

\u56e0\u4e3a\u9519\u8bef\u5355\u8bcd\u662f\u7ed9\u5b9a\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u8ba4\u4e3a p ( m i s t a c k ) p(mistack) <\/span><\/span>p<\/span>(<\/span>m<\/span>i<\/span>s<\/span>t<\/span>a<\/span>c<\/span>k<\/span>)<\/span><\/span><\/span><\/span><\/span>\u4e3a\u5e38\u6570\uff0c\u56e0\u6b64\uff1a<\/p>\n

p ( c o r r e c t \u2223 m i s t a c k ) = p ( m i s t a c k \u2223 c o r r e c t ) p ( c o r r e c t ) p(correct|mistack) = p(mistack|correct)p(correct) <\/span><\/span>p<\/span>(<\/span>c<\/span>o<\/span>r<\/span>r<\/span>e<\/span>c<\/span>t<\/span>\u2223<\/span>m<\/span>i<\/span>s<\/span>t<\/span>a<\/span>c<\/span>k<\/span>)<\/span><\/span>=<\/span><\/span><\/span><\/span>p<\/span>(<\/span>m<\/span>i<\/span>s<\/span>t<\/span>a<\/span>c<\/span>k<\/span>\u2223<\/span>c<\/span>o<\/span>r<\/span>r<\/span>e<\/span>c<\/span>t<\/span>)<\/span>p<\/span>(<\/span>c<\/span>o<\/span>r<\/span>r<\/span>e<\/span>c<\/span>t<\/span>)<\/span><\/span><\/span><\/span><\/span><\/span><\/p>\n

p ( m i s t a c k \u2223 c o r r e c t ) p(mistack|correct) <\/span><\/span>p<\/span>(<\/span>m<\/span>i<\/span>s<\/span>t<\/span>a<\/span>c<\/span>k<\/span>\u2223<\/span>c<\/span>o<\/span>r<\/span>r<\/span>e<\/span>c<\/span>t<\/span>)<\/span><\/span><\/span><\/span><\/span>\u53ef\u75282\u4e2d\u62fc\u9519\u6982\u7387\u8868\u793a\uff0c p ( c o r r e c t ) p(correct) <\/span><\/span>p<\/span>(<\/span>c<\/span>o<\/span>r<\/span>r<\/span>e<\/span>c<\/span>t<\/span>)<\/span><\/span><\/span><\/span><\/span>\u53ef\u75283\u4e2d\u8bed\u8a00\u6a21\u578b\u8868\u793a\u3002<\/p>\n

#\u4fee\u6539\u6587\u672c\u4e2d\u7684\u9519\u8bef<\/span>\ndef<\/span> main<\/span>(<\/span>)<\/span>:<\/span>\n    vocab =<\/span> set<\/span>(<\/span>[<\/span>line.<\/span>rstrip(<\/span>)<\/span> for<\/span> line in<\/span> open<\/span>(<\/span>'vocab.txt'<\/span>)<\/span>]<\/span>)<\/span>\n    word_count,<\/span>bigram_count =<\/span> language_model(<\/span>)<\/span>\n    #\u8bed\u6599\u5e93\u4e2d\u7684\u8bcd\u8bed\u603b\u7c7b\u522b\u6570<\/span>\n    V =<\/span> len<\/span>(<\/span>word_count.<\/span>keys(<\/span>)<\/span>)<\/span>\n    channel_prob =<\/span> channel_probability(<\/span>)<\/span>\n    with<\/span> open<\/span>(<\/span>\"testdata.txt\"<\/span>)<\/span> as<\/span> f:<\/span>\n        for<\/span> line in<\/span> f:<\/span>\n            #\u628a\u53e5\u5b50\u62c6\u6210\u82f1\u6587\u5355\u8bcd\u5217\u8868<\/span>\n            words =<\/span> line.<\/span>rstrip(<\/span>'.'<\/span>)<\/span>.<\/span>split(<\/span>'\\t'<\/span>)<\/span>\n            words =<\/span> words[<\/span>2<\/span>]<\/span>.<\/span>split(<\/span>)<\/span>\n            for<\/span> word in<\/span> words:<\/span>\n                if<\/span> word[<\/span>-<\/span>1<\/span>]<\/span> ==<\/span> ','<\/span>:<\/span>\n                    word =<\/span> word[<\/span>:<\/span>-<\/span>2<\/span>]<\/span>\n            if<\/span> words[<\/span>-<\/span>1<\/span>]<\/span>[<\/span>-<\/span>1<\/span>]<\/span> ==<\/span> '.'<\/span>:<\/span>\n                words[<\/span>-<\/span>1<\/span>]<\/span> =<\/span> words[<\/span>-<\/span>1<\/span>]<\/span>[<\/span>:<\/span>-<\/span>2<\/span>]<\/span>\n            #print(words)<\/span>\n            \n            #\u5bf9\u6bcf\u4e2a\u5355\u8bcd\u751f\u6210\u5019\u9009\u5355\u8bcd<\/span>\n            for<\/span> i,<\/span>word in<\/span> enumerate<\/span>(<\/span>words)<\/span>:<\/span>\n                if<\/span> word in<\/span> vocab:<\/span>\n                    continue<\/span>\n                \n                print<\/span>(<\/span>word)<\/span>\n                candidates =<\/span> generate_candidates(<\/span>word)<\/span>\n                \n                # \u4e00\u79cd\u65b9\u5f0f\uff1a if candidate = [], \u591a\u751f\u6210\u51e0\u4e2acandidates, \u6bd4\u5982\u751f\u6210\u7f16\u8f91\u8ddd\u79bb\u4e0d\u5927\u4e8e2\u7684<\/span>\n                # TODO \uff1a \u6839\u636e\u6761\u4ef6\u751f\u6210\u66f4\u591a\u7684\u5019\u9009\u96c6\u5408<\/span>\n                #\u7b80\u5355\u5b9e\u73b0\u53ea\u8003\u8651\u7f16\u8f91\u8ddd\u79bb\u4e3a1\u7684<\/span>\n                if<\/span> len<\/span>(<\/span>candidates)<\/span> <<\/span> 1<\/span>:<\/span>\n                    continue<\/span>\n                \n                #\u5bfb\u627e\u53ef\u80fd\u6027\u6700\u9ad8\u7684candidates\uff0c\u6839\u636e\u8d1d\u53f6\u65af\u516c\u5f0f\uff0c<\/span>\n                # p\uff08correct|mistack\uff09= p(mistack|correct)p(correct)\/p(mistack)(\u5206\u6bcd\u4e3a\u5e38\u6570)<\/span>\n                # \u7b49\u540c\u4e8e log\uff08p(mistack|correct)\uff09+ log(p(correct))<\/span>\n\n                probs =<\/span> [<\/span>]<\/span>\n                for<\/span> candidate in<\/span> candidates:<\/span>\n                    prob =<\/span> 0<\/span>\n                    # log\uff08p(mistack|correct)\uff09\u90e8\u5206\u8ba1\u7b97channel_probability\uff0c\u5373\u6b63\u786e\u5355\u8bcd\u662f\u8fd9\u4e2a\u7684\u65f6\u5019\u62fc\u9519\u6210word\u7684\u6982\u7387<\/span>\n                    if<\/span> candidate in<\/span> channel_prob and<\/span> word in<\/span> channel_prob[<\/span>candidate]<\/span>:<\/span>\n                        prob +=<\/span> np.<\/span>log(<\/span>channel_prob[<\/span>candidate]<\/span>[<\/span>word]<\/span>)<\/span>\n                        #print(prob)<\/span>\n                    else<\/span>:<\/span>\n                        prob +=<\/span> np.<\/span>log(<\/span>0.0001<\/span>)<\/span> #\u8ba4\u4e3a\u662f\u4e00\u4e2a\u6781\u5c0f\u7684\u6982\u7387\uff0c\u9632\u6b62\u53d80<\/span>\n                    \n                    # log(p(correct))\u90e8\u5206\u8ba1\u7b97\u8bed\u8a00\u6a21\u578b\uff0c\u770b\u770b\u8fd9\u4e2a\u8bcd\u662f\u5426\u5408\u9002\u51fa\u73b0\u5728\u8fd9<\/span>\n                    if<\/span> i ==<\/span> 0<\/span>:<\/span>\n                        biword =<\/span> '<s> '<\/span>+<\/span> word\n                    else<\/span>:<\/span>\n                        biword =<\/span> words[<\/span>i-<\/span>1<\/span>]<\/span> +<\/span> ' '<\/span> +<\/span> word\n                    \n                    if<\/span> biword in<\/span> bigram_count:<\/span>\n                        prob +=<\/span> np.<\/span>log(<\/span>(<\/span>bigram_count[<\/span>biword]<\/span>+<\/span>1<\/span>)<\/span>\/<\/span>(<\/span>word_count[<\/span>word]<\/span>+<\/span>V)<\/span>)<\/span>\n                        print<\/span>(<\/span>prob)<\/span>\n                    else<\/span>:<\/span>\n                        prob +=<\/span> np.<\/span>log(<\/span>1.0<\/span> \/<\/span> V)<\/span>\n                    \n                    probs.<\/span>append(<\/span>prob)<\/span>\n                \n                max_idx =<\/span> probs.<\/span>index(<\/span>max<\/span>(<\/span>probs)<\/span>)<\/span>\n                #print(probs)<\/span>\n                #print(max_idx)<\/span>\n                print<\/span>(<\/span>word,<\/span> candidates[<\/span>max_idx]<\/span>)<\/span>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u6e38\u79bb\u6001GLZ\u7684NLP\u4efb\u52a11\uff1a\u62fc\u5199\u7ea0\u9519\u5f53\u6211\u4eec\u4f7f\u7528\u641c\u7d22\u5f15\u64ce\u7684\u65f6\u5019\uff0c\u7ecf\u5e38\u4f1a\u53d1\u73b0\u6211\u4eec\u6253\u9519\u4e86\u6211\u4eec\u60f3\u8981\u68c0\u7d22\u7684\u4e1c\u897f\uff0c\u4f46\u662f\u641c\u7d22\u5f15\u64ce\u4ecd\u65e7\u7ed9\u4e86\u6211...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8935"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8935"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8935\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}