• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            sunrise

            每天不斷學習,才能不斷提升自己。

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              64 隨筆 :: 0 文章 :: 92 評論 :: 0 Trackbacks
             1 #!/usr/bin/env python
             2 # -*-coding: UTF-8-*-
             3 
             4 #構建節點
             5 class Node:
             6   def __init__(self):
             7     self.map = {}
             8   def contain(self,key):
             9     return self.map.__contains__(key)
            10   def __getitem__(self,key):
            11     return self.map[key]
            12   def __setitem__(self,key,value):
            13     self.map[key]=value
            14     
            15 class Item:
            16   def __init__(self,key):
            17     self.key=key
            18     self.subNum=0
            19     self.subNode=Node()
            20   def add(self,key,item):
            21     self.subNum += 1
            22     self.subNode[key] = item
            23 
            24 #建樹
            25 def build_tree(input):
            26   """build a tree"""
            27   sock = open(input,"r")
            28   buf = sock.read()
            29   buf = chinese(buf)
            30   tree = Item(" ")
            31   for word in buf:
            32     current = tree
            33     for x in word:
            34       if current.subNode.contain(x):
            35         current = current.subNode[x]
            36       else:
            37         item = Item(x)
            38         current.add(x,item)
            39         current = item
            40   print tree
            41   return tree
            42 
            43 def search(buf,thefile):
            44   "search."
            45   buf = chinese(buf)
            46   tree = build_tree(thefile)
            47   havefind = ""
            48   for word in buf:
            49     current = tree
            50     for x in word:
            51       if current.subNode.contain(x):
            52         current = current.subNode[x]
            53         if  current.subNum == 0:
            54           havefind += "".join(word)
            55       else:
            56         break
            57   return havefind
            58 
            59 #判斷讀入的是否為漢字
            60 def chinese(input):
            61   "remove the not chinese"
            62   input = unicode(input,"utf8")
            63   buf = []
            64   for word in input:
            65     if word >= u'\u4e00' and word <= u'\u9fa5':
            66       buf = buf+[word.encode('utf-8')]
            67   return buf
            68 
            69 
            70 def run():
            71   fileName = "test"
            72   textChar = ""
            73   buf = search(textChar,fileName)
            74   print buf
            75 
            76 if __name__ == "__main__":
            77   run()
            有關字典樹的理論和概念可以參見http://blog.csdn.net/v_july_v/article/details/6897097
            有關python的建樹過程http://bytes.com/topic/python/answers/827476-dictionary-tree-format-hopefully-simple

















            posted on 2012-04-05 11:04 SunRise_at 閱讀(3454) 評論(3)  編輯 收藏 引用 所屬分類: 可愛的python數據結構

            評論

            # re: 用python實現的字典樹 2012-04-05 14:27 Glueless full lace wig silk top
            這么強大,收藏是必須的  回復  更多評論
              

            # re: 用python實現的字典樹 2012-04-05 15:33 SunRise_at
            你是?  回復  更多評論
              

            # re: 用python實現的字典樹 2012-04-06 14:20 C小加
            和C++有很大差別啊。不知道C++怎么實現接收UTF8格式的字符。  回復  更多評論
              

            久久精品国产精品亚洲精品| 伊人久久综在合线亚洲2019| 久久男人中文字幕资源站| 72种姿势欧美久久久久大黄蕉| 国内精品久久久久久野外| 精品久久久久久无码人妻蜜桃| 久久久国产99久久国产一| 国产成人精品三上悠亚久久| 久久精品一区二区三区不卡| 国产成人香蕉久久久久| 精产国品久久一二三产区区别 | 久久精品国产亚洲AV麻豆网站| 久久婷婷五月综合色高清| 中文字幕成人精品久久不卡| 久久午夜夜伦鲁鲁片免费无码影视 | 久久福利青草精品资源站免费| 久久精品中文字幕第23页| 久久亚洲精品国产精品| 久久久久久一区国产精品| www.久久热| 亚洲av成人无码久久精品| 久久九九久精品国产| 99久久人妻无码精品系列| 久久无码专区国产精品发布| 久久国产乱子伦精品免费午夜| 成人妇女免费播放久久久| 99精品久久精品一区二区| 中文精品99久久国产 | 久久99精品久久久久久噜噜| 国产三级久久久精品麻豆三级| 久久精品视频一| 亚洲欧美国产日韩综合久久| 色综合久久久久网| 91精品国产高清久久久久久io| 久久WWW免费人成一看片| 国产精品乱码久久久久久软件| 久久久久亚洲AV成人网| 久久精品成人免费观看97| 久久精品无码一区二区三区免费| 激情五月综合综合久久69| 久久久久久久综合日本亚洲|