Elias Ponvert avatar Elias Ponvert committed 48f9049

initial check-in

Comments (0)

Files changed (1)

+'''
+produces random (uniform) binary parses of strings
+'''
+
+from sys import stdin, stderr, exit
+from random import randint
+
+PUNC=';'
+
+def randparse(s):
+  if len(s) <= 2:
+    return s
+  new_parse = [None,None]
+  split = randint(1,len(s)-1)
+  new_parse[0] = randparse(s[:split])
+  new_parse[1] = randparse(s[split:])
+  return new_parse
+
+def pieces(s):
+  if type(s) == str:
+    yield s
+  elif type(s) == list:
+    if len(s) == 1:
+      yield s[0]
+
+    else:
+      yield '('
+      for _s in s:
+        for __s in pieces(_s):
+          yield __s
+      yield ')'
+
+  else:
+    print >>stderr, 'wtf', s
+    exit(1)
+
+def pprint(s):
+  s = ' '.join(pieces(s))
+  print s.replace('( ','(').replace(' )',')')
+
+if __name__ == '__main__':
+  for s in stdin:
+    s = s.split()
+    if PUNC not in s:
+      pprint(randparse(s))
+      continue
+
+    start = 0
+    new_s = []
+    while PUNC in s:
+      end = s.index(PUNC)
+      if start != end:
+        new_s.append(randparse(s[start:end]))
+      del s[end]
+      start = end
+
+    pprint(new_s)
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.