From 52008779b0281639e17a6570271dc7d5a3227b03 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 25 Aug 2023 18:02:57 +0100 Subject: [PATCH] Working on blog posts --- riddle_creator.md | 106 ++++++++++++++++++++++++++++++++++++++---- riddle_definitions.md | 27 ++++++++--- riddle_solver.md | 2 +- 3 files changed, 118 insertions(+), 17 deletions(-) diff --git a/riddle_creator.md b/riddle_creator.md index 36b939a..5484c03 100644 --- a/riddle_creator.md +++ b/riddle_creator.md @@ -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}" diff --git a/riddle_definitions.md b/riddle_definitions.md index 402fd94..a9242f3 100644 --- a/riddle_definitions.md +++ b/riddle_definitions.md @@ -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 @@ -28,8 +28,8 @@ import random ``` ```python -stop_words = set('my is in within lies and also always you will find the found'.split()) -negative_words = set('but not never neither nor'.split()) +stop_words = set('my is in within lies and also always you will find the found but'.split()) +negative_words = set('not never neither nor'.split()) ``` ```python @@ -111,20 +111,33 @@ def edit_distance(s: str, t: str) -> int: return len(t) if t == "": return len(s) - if s[-1] == t[-1]: + if s[0] == t[0]: cost = 0 else: cost = 1 res = min( - [ edit_distance(s[:-1], t)+1 - , edit_distance(s, t[:-1])+1 - , edit_distance(s[:-1], t[:-1]) + cost + [ edit_distance(s[1:], t) + 1 + , edit_distance(s, t[1:]) + 1 + , edit_distance(s[1:], t[1:]) + cost ]) return res ``` +```python +dictionary_neighbours = { + w: [o for o in dictionary + if edit_distance(w, o) <= 5 + if not set(w) <= set(o) + if not set(o) <= set(w)] + for w in dictionary} + +dictionary_neighbours = {w: ns + for w, ns in dictionary_neighbours.items() + if ns} +``` + ```python def collapse_riddle_clues(elems : Riddle) -> RiddleElems: """Combine the two parts of a riddle line into one element for solving. diff --git a/riddle_solver.md b/riddle_solver.md index 67b6298..54c060b 100644 --- a/riddle_solver.md +++ b/riddle_solver.md @@ -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 -- 2.34.1