#!/usr/bin/env python
# encoding: utf-8
"""
@version: v 0.1
@author: muyeby
@contact: bxf_hit@163.com
@site: http://muyeby.github.io
@software: PyCharm
@file: __init__.py
@time: 16-7-4 下午7:24
"""
from hmmlearn import hmm
import numpy as np
import os
import re
MAX_LENGTH = 10
idict = []


class Graph(object):
    def __init__(self):
        self.seq = []
class Gnode(object):
    def __init__(self,strs):
        self.value = strs
DATA_DIR = os.getcwd()+'/../../data/'
DICT_DIR = DATA_DIR + 'icwb2-data/gold/pku_training_words.utf8'

def load_dict(FILE):
    '''生成汉字字典'''
    global idict
    idict = []
    for line in open(FILE, 'rb'):
        if not (type(line) is unicode):
            try:
                line = line.decode('utf-8')
            except:
                line = line.decode('gbk', 'ignore')
        line = line.strip()
        if not line or line[0] == '#':
            continue
        re_han = re.compile(ur"([\u4E00-\u9FA5]+)")
        m = re_han.match(line)
        if m:
            idict.append(m.group(0))
            # print ' '.join(idict)
def createGraph(sentence):
    #生成有向图
    gra = Graph()
    for i in range(len(sentence)):
        gra.seq.append({})
    for i in range(len(sentence)):
        for j in range(MAX_LENGTH):
            if i + j + 1 > len(sentence):
                break
            tmp = ''.join(sentence[i:i+j+1])
            if tmp in idict:
                tmpvalue = Gnode(tmp)
                gra.seq[i+j][tmp] = tmpvalue
    end = Gnode('#')
    gra.seq.append({'#': end})
    return gra
res = ''
allres = []
def dfs(n,graph,sentence,path):
    if n<0:
        print path
        allres.append(path)
    elif not graph.seq[n]:
        path = sentence[n] + ' ' + path
        dfs(n-1,graph,sentence,path)
    else:
        for key,value in graph.seq[n].items():
            path = key + ' ' + path
            dfs(n-len(key),graph,sentence,path)
load_dict(DICT_DIR)
sentence = u'今天天气真好'
tmp = createGraph(sentence)
dfs(len(sentence)-1,tmp,sentence,'')
print allres

@@ -1561,16 +1561,17 @@
 es = %5B%5D%0A
+%0A
 def dfs(
@@ -1822,24 +1822,27 @@
             
+tmp
 path = key +
@@ -1899,53 +1899,356 @@
 nce,
+tmp
 path)%0A
-load_dict(DICT_DIR)%0Asentence = u'%E4%BB%8A%E5%A4%A9%E5%A4%A9%E6%B0%94%E7%9C%9F%E5%A5%BD
+def select():%0A    best = %5B%5D%0A    best.append(allres%5B0%5D)%0A    for item in allres:%0A        if len(item) %3C len(best%5B0%5D):%0A            best = %5B%5D%0A            best.append(item)%0A        elif len(item) == len(best%5B0%5D):%0A            best.append(item)%0A    print '%5Cn'.join(best)%0A%0Aload_dict(DICT_DIR)%0Asentence = u'%E6%B1%89%E5%AD%97%E7%AC%94%E9%A1%BA%E6%A0%87%E5%87%86%E7%94%B1%E5%9B%BD%E5%AE%B6%E8%AF%AD%E8%A8%80%E6%96%87%E5%AD%97%E5%B7%A5%E4%BD%9C%E5%A7%94%E5%91%98%E4%BC%9A%E6%A0%87%E5%87%86%E5%8C%96%E5%B7%A5%E4%BD%9C%E5%A7%94%E5%91%98%E4%BC%9A%E5%88%B6%E5%AE%9A%E5%8F%AB%E5%81%9A%E7%8E%B0%E4%BB%A3%E6%B1%89%E8%AF%AD%E9%80%9A%E7%94%A8%E5%AD%97%E7%AC%94%E9%A1%BA%E8%A7%84%E8%8C%83
 '%0Atm
@@ -2314,17 +2314,12 @@
 '')%0A
-print allres%0A
+select()

fix bugs