Package nltk :: Package misc :: Module nemo
[hide private]
[frames] | no frames]

Source Code for Module nltk.misc.nemo

  1  #!/usr/bin/env python 
  2   
  3  # Finding (and Replacing) Nemo, Version 1.1, Aristide Grange 2006/06/06  
  4  # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496783 
  5   
  6  """ 
  7  Finding (and Replacing) Nemo 
  8   
  9  Instant Regular Expressions 
 10  Created by Aristide Grange 
 11  """ 
 12  import Tkinter as tk 
 13  import sre 
 14  import itertools 
 15   
 16  windowTitle = "Finding (and Replacing) Nemo" 
 17  initialFind = r"n(.*?)e(.*?)m(.*?)o" 
 18  initialRepl = r"M\1A\2K\3I" 
 19  initialText = """\ 
 20  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
 21  Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
 22  Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
 23  Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 
 24  """ 
 25  images = { 
 26      "FIND":"R0lGODlhMAAiAPcAMf/////37//35//n1v97Off///f/9/f37/fexvfOvfeEQvd7QvdrQvdrKfdaKfdSMfdSIe/v9+/v7+/v5+/n3u/e1u/Wxu/Gre+1lO+tnO+thO+Ua+97Y+97Oe97Me9rOe9rMe9jOe9jMe9jIe9aMefe5+fe3ufezuece+eEWudzQudaIedSIedKMedKIedCKedCId7e1t7Wzt7Oxt7Gvd69vd69rd61pd6ljN6UjN6Ue96EY95zY95rUt5rQt5jMd5SId5KIdbn59be3tbGztbGvda1rdaEa9Z7a9Z7WtZzQtZzOdZzMdZjMdZaQtZSOdZSMdZKMdZCKdZCGNY5Ic7W1s7Oxs7Gtc69xs69tc69rc6tpc6llM6clM6cjM6Ue86EY85zWs5rSs5SKc5KKc5KGMa1tcatrcalvcalnMaUpcZ7c8ZzMcZrUsZr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TgEoCafTySYIhYMxgLBjEQgCULvCw0QdAZdoVhUIJUFChISEAxYeQM1N1OMTAp+UwZ5eA4TEhFbDWYFdC4ECVMJjwl5BwsQa0umEhUVlhESDgqlBp0rAn5nVpBMDxeZDRQbHBgWFBSWDgtLBnFjKwRYCI9VqQsPs0YKEcMXFq0UEalFDWx4BAO2IwPjppAKDkrTWKYUGd7fEJJFEZpM00cOzCgh4EE8SaoWxKNixQooBRMyZMBwAYIRBhUgLDGS4MoBJeoANMhAgQsaCRZm/5lqaCUJhA4cNHjDoKEDBlJUHqkBlYBTiQUZNGjYMMxDhY3VWk6R4MEDBoMUak5AqoYBqANIBo4wcGGDUKIeLlzVZmWJggsVIkwAZaQSA3kdZzlKkIiEAAlDvW5oOkEBs488JTw44oeUIwdvVTFTUK7uiAAPgubt8GFDhQepqETAQCFU1UMGzlqAgFhUsAcCS0AO6lUDhw8xNRSbENGDhgWSHjWUe6ACbKITizmopZoBa6KvOwj9uuHDhwxyj3xekgDDhw5EvWKo0IB4iQLCOCC/njc7ZQ8UeGvza+ABZZgcxJNc4FO1gc0cOsCUrHevc8tdIMTIAhc4F198G2Qwwd8CBIQUAwEINABBBJUwR9R5wElgVRLwWODBBx4cGB8GEzDQIAo33CGJA8gh+JoH/clUgQU0YvDhdfmJdwEFC6Sjgg8yEPAABsPkh2F22cl2AQbn6QdTghTQ5eAJAQyQAAQV0MSBB9gRVZ4GE1mw5JZOAmiAVi1UWcAZDrDyZXYTeaOhA/bIVuIBPtKQ4h7ViYekUPdcEAEbzTzCRp5CADmAAwj+ORGPBcgwAAHo9ABGCYtm0ChwFHShlRiXhmHlkAcCiOeUodqQw5W0oXLAiamy4MOkjOyAaqxUymApDCEAADs=", 
 30  } 
 31  colors = ["#FF7B39","#80F121"] 
 32  emphColors = ["#DAFC33","#F42548"] 
 33  fieldParams = { 
 34      "height":3, 
 35      "width":70, 
 36      "font":("monaco",14), 
 37      "highlightthickness":0, 
 38      "borderwidth":0, 
 39      "background":"white", 
 40  } 
 41  textParams = { 
 42      "bg":"#F7E0D4", 
 43      "fg":"#2321F1", 
 44      "highlightthickness":0, 
 45      "width":1, 
 46      "height":10, 
 47      "font":("verdana",16), 
 48      "wrap":"word", 
 49  } 
 50   
 51   
52 -class Zone:
53 - def __init__(self, image, initialField, initialText):
54 frm = tk.Frame(root) 55 frm.config(background="white") 56 self.image = tk.PhotoImage(format='gif',data=images[image.upper()]) 57 self.imageDimmed = tk.PhotoImage(format='gif',data=images[image]) 58 self.img = tk.Label(frm) 59 self.img.config(borderwidth=0) 60 self.img.pack(side = "left") 61 self.fld = tk.Text(frm, **fieldParams) 62 self.initScrollText(frm,self.fld,initialField) 63 frm = tk.Frame(root) 64 self.txt = tk.Text(frm, **textParams) 65 self.initScrollText(frm,self.txt,initialText) 66 for i in range(2): 67 self.txt.tag_config(colors[i], background = colors[i]) 68 self.txt.tag_config("emph"+colors[i], foreground = emphColors[i])
69 - def initScrollText(self,frm,txt,contents):
70 scl = tk.Scrollbar(frm) 71 scl.config(command = txt.yview) 72 scl.pack(side="right",fill="y") 73 txt.pack(side = "left", expand=True, fill="x") 74 txt.config(yscrollcommand = scl.set) 75 txt.insert("1.0",contents) 76 frm.pack(fill = "x") 77 tk.Frame(height=2, bd=1, relief="ridge").pack(fill="x")
78 - def refresh(self):
79 self.colorCycle = itertools.cycle(colors) 80 try: 81 self.substitute() 82 self.img.config(image = self.image) 83 except sre.error: 84 self.img.config(image = self.imageDimmed)
85 86
87 -class FindZone(Zone):
88 - def addTags(self,m):
89 color = self.colorCycle.next() 90 self.txt.tag_add(color,"1.0+%sc"%m.start(),"1.0+%sc"%m.end()) 91 try: 92 self.txt.tag_add("emph"+color,"1.0+%sc"%m.start("emph"), 93 "1.0+%sc"%m.end("emph")) 94 except: 95 pass
96 - def substitute(self,*args):
97 for color in colors: 98 self.txt.tag_remove(color,"1.0","end") 99 self.txt.tag_remove("emph"+color,"1.0","end") 100 self.rex = sre.compile("") # default value in case of misformed regexp 101 self.rex = sre.compile(self.fld.get("1.0","end")[:-1],sre.MULTILINE) 102 try: 103 sre.compile("(?P<emph>%s)" % self.fld.get(tk.SEL_FIRST, 104 tk.SEL_LAST)) 105 self.rexSel = sre.compile("%s(?P<emph>%s)%s" % ( 106 self.fld.get("1.0",tk.SEL_FIRST), 107 self.fld.get(tk.SEL_FIRST,tk.SEL_LAST), 108 self.fld.get(tk.SEL_LAST,"end")[:-1], 109 ),sre.MULTILINE) 110 except: 111 self.rexSel = self.rex 112 self.rexSel.sub(self.addTags,self.txt.get("1.0","end"))
113 114
115 -class ReplaceZone(Zone):
116 - def addTags(self,m):
117 s = sz.rex.sub(self.repl,m.group()) 118 self.txt.delete("1.0+%sc"%(m.start()+self.diff), 119 "1.0+%sc"%(m.end()+self.diff)) 120 self.txt.insert("1.0+%sc"%(m.start()+self.diff),s, 121 self.colorCycle.next()) 122 self.diff += len(s) - (m.end() - m.start())
123 - def substitute(self):
124 self.txt.delete("1.0","end") 125 self.txt.insert("1.0",sz.txt.get("1.0","end")[:-1]) 126 self.diff = 0 127 self.repl = rex0.sub(r"\\g<\1>",self.fld.get("1.0","end")[:-1]) 128 sz.rex.sub(self.addTags,sz.txt.get("1.0","end")[:-1])
129 130
131 -def launchRefresh(_):
132 sz.fld.after_idle(sz.refresh) 133 rz.fld.after_idle(rz.refresh)
134 135 136 if __name__ == '__main__': 137 root = tk.Tk() 138 root.resizable(height=False,width=True) 139 root.title(windowTitle) 140 root.minsize(width=250,height=0) 141 sz = FindZone("find",initialFind,initialText) 142 sz.fld.bind("<Button-1>",launchRefresh) 143 sz.fld.bind("<ButtonRelease-1>",launchRefresh) 144 sz.fld.bind("<B1-Motion>",launchRefresh) 145 sz.rexSel = sre.compile("") 146 rz = ReplaceZone("repl",initialRepl,"") 147 rex0 = sre.compile(r"(?<!\\)\\([0-9]+)") 148 root.bind_all("<Key>",launchRefresh) 149 launchRefresh(None) 150 root.mainloop() 151