Maximum probability participle

Source: Internet
Author: User

 

Here is a method of word segmentation-the maximum probability participle, also called 1-gram participle, because it does not consider the context, only consider the current word probability.

We need to have a dictionary that records the frequency of each word, such as:

Based on this dictionary, we can express a sentence with a directed acyclic graph (DAG), such as

In this figure, each node is a word, and the edge is the probability of a two-point constituent word. The problem with the word breaker is to find out the probability and the largest one in the DAG that overwrites all the words from start to finish. For example, spicy-"dish, five-" meat, lid-"rice is a path, spicy-" spicy, white-"dish, five-" flower, meat-"meat, lid-" Rice is also a path, how to find the largest one?

Set \ (\alpha_i\) is the probability of the optimal path to the portion of the DAG that follows the I node, and J is the adjacency point of I, so easy to go out \ (\alpha_i = \SUM_JP (<i,j>) \alpha_j\),p<i,j> is the probability of a word.

This is a dynamic programming problem, starting with the last word, and gradually moving forward until the first point, and then getting the optimal path from the previous one.

The Python code is as follows:

defBuild_dag (sentence): DAG= {}#Dict,key is a word that each word's index,value is able to form with this word listN =Len (sentence) forKinchxrange (N): TMP=[] I=k piece=Sentence[k] whileI < N andPieceinchDict. FREQ:ifDict. Freq[piece]: tmp.append (i) I+ = 1piece= sentence[k:i + 1]       if  notTmp:tmp.append (k) dag[k]=tmpreturnDag
def Calc_route (sentence, DAG, route):         = Len (sentence)        = (0, 0)        = log (total_freq)        for in xrange ( N-1,-1,-1):            or 1)-                              for in Dag[idx])

def __cut_dag (self, sentence):     = Build_dag (sentence)    = {}    calc_route (DAG, route)    = 0    = Len ( Sentence)    = []    while x < N:        = route[x][1] + 1         = Sentence[x,y]        segs.append (word)        = y

Maximum probability participle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.