-
Notifications
You must be signed in to change notification settings - Fork 0
/
ana.py
89 lines (66 loc) · 1.71 KB
/
ana.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Wrapper for the Internet Anagram Server.
"""
from collections import defaultdict
import pathlib
import sys
import typing
from icecream import ic
import requests
def explode (
text: str,
) -> list:
"""
Explode a word into a list of characters
"""
return list(text.replace(" ", "").lower())
def request_words (
term: str,
) -> typing.List[ str ]:
"""
Make an API call to the anagram server.
"""
api_url = f"https://new.wordsmith.org/anagram/anagram.cgi?anagram={ term }&t=500&a=n"
response = requests.get(api_url)
pat_head = "Displaying all:"
pat_done = "<script>document.body"
ignore = True
words = set([])
for i, line in enumerate(response.text.split("\n")):
if pat_done in line:
ignore = True
if not ignore:
for word in line.strip().lstrip("</b><br>").rstrip("<br>").lower().split(" "):
words.add(word)
if ignore and pat_head in line:
ignore = False
return words
if __name__ == "__main__":
text: str = "bitwest the best weekend of my year"
ic(text)
targets: typing.List[ str ] = [
"monkey",
"tribe",
"fete",
"by",
"swathed",
"sweet",
]
ic(targets)
letters: typing.Dict[ str, int ] = defaultdict(int)
for char in explode(text):
letters[char] += 1
ic(letters)
for word in targets:
for char in explode(word):
letters[char] -= 1
residual = [
char * count
for char, count in sorted(letters.items())
]
term: str = "".join(residual)
ic(term)
words = request_words(term)
ic(len(words), words)