X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=riddle_creator.md;h=8373658efdad67d1cdca50d26cb851737fbb487a;hb=refs%2Fheads%2Fmain;hp=5484c03212afd2ab31a1b4bc48182a9a010369f3;hpb=52008779b0281639e17a6570271dc7d5a3227b03;p=riddle-generator.git diff --git a/riddle_creator.md b/riddle_creator.md index 5484c03..8373658 100644 --- a/riddle_creator.md +++ b/riddle_creator.md @@ -1,12 +1,12 @@ --- jupyter: jupytext: - formats: ipynb,md,py:percent + formats: ipynb,md text_representation: extension: .md format_name: markdown format_version: '1.3' - jupytext_version: 1.15.0 + jupytext_version: 1.14.5 kernelspec: display_name: Python 3 (ipykernel) language: python @@ -21,45 +21,6 @@ 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] @@ -67,8 +28,6 @@ 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 @@ -87,8 +46,6 @@ 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 @@ -108,8 +65,6 @@ 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 @@ -160,27 +115,6 @@ sample_riddle solve_riddle(collapse_riddle_clues(sample_riddle)) ``` -```python -# write_riddle(sample_riddle) -``` - -```python -# sample_riddle = random_riddle('sonnet', limit=4) -# sample_riddle -``` - -```python -sample_riddle -``` - -```python -collapse_riddle_clues(sample_riddle) -``` - -```python -solve_riddle(collapse_riddle_clues(sample_riddle)) -``` - ```python def valid_random_riddle(word: str) -> Riddle: finished = False @@ -192,34 +126,27 @@ def valid_random_riddle(word: str) -> 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 = [] +import time +import csv +reports = [] for _ in range(1000): - gencount = 0 + w1, c1 = time.perf_counter(), time.process_time() r = valid_random_riddle(random.choice(dictionary)) + w2, c2 = time.perf_counter(), time.process_time() linecount = len(r) - glcounts.append((gencount, linecount)) + reports.append({'wall_time': w2 - w1, + 'cpu_time': c2 - c1, + 'riddle_lines': linecount}) + w_times.append(w2 - w1) + c_times.append(c2 - c1) + +with open('metrics_original.csv', 'w', newline='') as csvfile: + fieldnames = list(reports[0].keys()) + writer = csv.DictWriter(csvfile, fieldnames=fieldnames) -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) + writer.writeheader() + for r in reports: + writer.writerow(r) ``` ```python @@ -230,7 +157,10 @@ def write_include_exclude_line(clue_a: RiddleClue, clue_b: RiddleClue) -> str: ```python def write_include_include_line(clue_a: RiddleClue, clue_b: RiddleClue) -> str: - line = f"is in {clue_a.word} and also in {clue_b.word}" + if random.randrange(2) == 0: + line = f"is in {clue_a.word} and also in {clue_b.word}" + else: + line = f"is in both {clue_a.word} and {clue_b.word}" return line ``` @@ -270,7 +200,7 @@ def write_riddle(riddle: Riddle) -> List[str]: ``` ```python -sample_riddle = valid_random_riddle("elephant") +sample_riddle = valid_random_riddle("riddle") sample_riddle ```