Working on blog posts
[riddle-generator.git] / riddle_creator.md
index 36b939ac7ec65a26c9628b47325467ccadf2a199..5484c03212afd2ab31a1b4bc48182a9a010369f3 100644 (file)
@@ -6,7 +6,7 @@ jupyter:
       extension: .md
       format_name: markdown
       format_version: '1.3'
-      jupytext_version: 1.14.5
+      jupytext_version: 1.15.0
   kernelspec:
     display_name: Python 3 (ipykernel)
     language: python
@@ -21,6 +21,45 @@ from enum import Enum, auto
 import random
 ```
 
+```python
+gencount = 0
+```
+
+```python
+len([w for w in dictionary if 's' in w])
+```
+
+```python
+len([w for w in dictionary if 's' not in w])
+```
+
+```python
+dset = set(frozenset(w) for w in dictionary)
+len(dset), len(dictionary)
+```
+
+```python
+len([w for w in dset if 's' in w])
+```
+
+```python
+len([w for w in dset if 's' not in w])
+```
+
+```python
+sw = random.choice([w for w in dictionary if 's' in w])
+sw
+```
+
+```python
+swrel = [w for w in dictionary if 's' not in w if edit_distance(w, sw) <=3]
+len(swrel)
+```
+
+```python
+swrel
+```
+
 ```python
 def include_exclude_clue(letter: str, limit: int = 3) -> (RiddleClue, RiddleClue):
   with_letter = [w for w in dictionary if letter in w]
@@ -28,6 +67,8 @@ def include_exclude_clue(letter: str, limit: int = 3) -> (RiddleClue, RiddleClue
   
   finished = False
   while not finished:
+    global gencount
+    gencount +=1
     a = random.choice(with_letter)
     b = random.choice(without_letter)
     finished = ((edit_distance(a, b) <= limit) and
@@ -46,6 +87,8 @@ def include_include_clue(letter: str, limit: int = 3) -> (RiddleClue, RiddleClue
   
   finished = False
   while not finished:
+    global gencount
+    gencount +=1
     a = random.choice(with_letter)
     b = random.choice(with_letter)
     finished = ((a != b) and 
@@ -65,6 +108,8 @@ def exclude_exclude_clue(letter: str, limit: int = 3) -> (RiddleClue, RiddleClue
   
   finished = False
   while not finished:
+    global gencount
+    gencount +=1
     a = random.choice(without_letter)
     b = random.choice(without_letter)
     finished = ((a != b) and 
@@ -83,33 +128,45 @@ def random_clue( letter: str
                , ie_limit: int = 3
                , ii_limit: int = 2
                , ee_limit: int = 2) -> (RiddleClue, RiddleClue):
-  r = random.random()
-  if r <= 0.7:
+  clue_type = random.choices(['include_exclude', 'include_include', 'exclude_exclude'],
+                             weights=[7, 2, 1],
+                             k=1)[0]
+  if clue_type == 'include_exclude':
     return include_exclude_clue(letter, limit=ie_limit)
-  elif r <= 0.9:
+  elif clue_type =='include_include':
     return include_include_clue(letter, limit=ii_limit)
   else:
     return exclude_exclude_clue(letter, limit=ee_limit)
 ```
 
 ```python
-def random_riddle(word: str, limit: int = 3) -> Riddle:
-  return {i+1 : random_clue(l, ie_limit=limit)
+def random_riddle( word: str
+                 , ie_limit: int = 3
+                 , ii_limit: int = 2
+                 , ee_limit: int = 2
+                 ) -> Riddle:
+  return {i+1 : 
+    random_clue(l, 
+                ie_limit=ie_limit, ii_limit=ii_limit, ee_limit=ee_limit)
           for i, l in enumerate(word)}  
 ```
 
 ```python
-sample_riddle = random_riddle('sonnet')
+sample_riddle = random_riddle('teacup')
 sample_riddle
 ```
 
 ```python
+solve_riddle(collapse_riddle_clues(sample_riddle))
+```
 
+```python
+# write_riddle(sample_riddle)
 ```
 
 ```python
-sample_riddle = random_riddle('sonnet', limit=4)
-sample_riddle
+sample_riddle = random_riddle('sonnet', limit=4)
+sample_riddle
 ```
 
 ```python
@@ -134,6 +191,37 @@ def valid_random_riddle(word: str) -> Riddle:
   return riddle
 ```
 
+```python
+# import time
+# w_times = []
+# c_times = []
+# for _ in range(1000):
+#   w1, c1 = time.perf_counter(), time.process_time()
+#   valid_random_riddle(random.choice(dictionary))
+#   w2, c2 = time.perf_counter(), time.process_time()
+#   w_times.append(w2 - w1)
+#   c_times.append(c2 - c1)
+
+# with open('cpu_times.txt', 'w') as f:
+#   f.writelines(f'{t}\n' for t in c_times)
+# with open('wall_times.txt', 'w') as f:
+#   f.writelines(f'{t}\n' for t in w_times)
+
+```
+
+```python
+glcounts = []
+for _ in range(1000):
+  gencount = 0
+  r = valid_random_riddle(random.choice(dictionary))
+  linecount = len(r)
+  glcounts.append((gencount, linecount))
+
+with open('linecounts.txt', 'w') as f:
+  f.write('"Lines generated","Lines in riddle"\n')
+  f.writelines(f'{g},{l}\n' for g, l in glcounts)
+```
+
 ```python
 def write_include_exclude_line(clue_a: RiddleClue, clue_b: RiddleClue) -> str:
   line = f"is in {clue_a.word} but not in {clue_b.word}"