Ed L authored on 23/06/2011 19:01:37
Showing 1 changed files
Showing 1 changed files
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,93 @@ |
1 |
+def prototype(name,bases,dict): |
|
2 |
+ cls = type(name, bases,dict) |
|
3 |
+ cls.prototype = cls |
|
4 |
+ return cls |
|
5 |
+ |
|
6 |
+__metaclass__ = prototype |
|
7 |
+ |
|
8 |
+from copy import deepcopy |
|
9 |
+ |
|
10 |
+class prototyped_object: |
|
11 |
+ def clone(self): |
|
12 |
+ c = self.prototype() |
|
13 |
+ c.__dict__ = deepcopy(self.__dict__) |
|
14 |
+ return c |
|
15 |
+ |
|
16 |
+ |
|
17 |
+class Char: |
|
18 |
+ toplevels = set() |
|
19 |
+ |
|
20 |
+ def __init__(self, val): |
|
21 |
+ self.char = val |
|
22 |
+ self.successors = set() |
|
23 |
+ if self not in self.toplevels: |
|
24 |
+ self.toplevels.add(self) |
|
25 |
+ else: |
|
26 |
+ self.__dict__ = (set([self]) & self.toplevels).pop().__dict__ |
|
27 |
+ |
|
28 |
+ def set_toplevel(self): |
|
29 |
+ item = (self.toplevels | set([self])).pop() |
|
30 |
+ item.merge(self) |
|
31 |
+ # print item |
|
32 |
+ self.toplevels.add(item) |
|
33 |
+ |
|
34 |
+ def merge(this, other): |
|
35 |
+ for x,y in zip(this.successors, other.successors): |
|
36 |
+ x.merge(y) |
|
37 |
+ x.successors = x.successors | y.successors |
|
38 |
+ |
|
39 |
+ def clone(self, val): |
|
40 |
+ c = self.prototype(val) |
|
41 |
+ return c |
|
42 |
+ def __hash__(self): |
|
43 |
+ return hash(self.char) |
|
44 |
+ def __cmp__(self, other): |
|
45 |
+ try: |
|
46 |
+ return cmp(self.char, other.char) |
|
47 |
+ except AttributeError: |
|
48 |
+ return cmp(self.char, other) |
|
49 |
+ def process_word(self, word): |
|
50 |
+ if self.char == word[0]: |
|
51 |
+ try: |
|
52 |
+ new = self.clone(word[1]) |
|
53 |
+ if word[1] in self.successors: |
|
54 |
+ new = (set([word[1]]) | this.successors).pop() |
|
55 |
+ new.process_word(word[1:]) |
|
56 |
+ self.successors.add(new) |
|
57 |
+ except IndexError: |
|
58 |
+ pass |
|
59 |
+ |
|
60 |
+ def print_(self, depth=0): |
|
61 |
+ print '-'*depth+self.char |
|
62 |
+ for i in self.successors: |
|
63 |
+ i.print_(depth+1) |
|
64 |
+ |
|
65 |
+ |
|
66 |
+ |
|
67 |
+def process_text(text): |
|
68 |
+ text = ( x.strip() for x in text.replace('\n', ' ').expandtabs().split(' ') if x.strip().isalpha() and len(x.strip()) > 1 ) |
|
69 |
+ for i in text: |
|
70 |
+ x = Char(i[0]) |
|
71 |
+ x.process_word(i) |
|
72 |
+# x.set_toplevel() |
|
73 |
+ |
|
74 |
+import random |
|
75 |
+def randword(): |
|
76 |
+ string = [] |
|
77 |
+ x = random.choice(tuple(Char.toplevels)) |
|
78 |
+ string.append(x.char) |
|
79 |
+ |
|
80 |
+ while x.successors: |
|
81 |
+ x = random.choice(tuple(x.successors)) |
|
82 |
+ string.append(x.char) |
|
83 |
+ return ''.join(string) |
|
84 |
+ |
|
85 |
+import sys |
|
86 |
+string = file(sys.argv[1]).read() |
|
87 |
+ |
|
88 |
+process_text(string) |
|
89 |
+#[ q.print_() for q in Char.toplevels ] |
|
90 |
+ |
|
91 |
+#for x in range(10000): |
|
92 |
+while raw_input() != 'q': |
|
93 |
+ print randword() |