Newer
Older
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Graphing a kind of \"Hamming Similarity\" of strings\n",
"\n",
"This notebook explores a slightly weird similarity measure for strings.\n",
"\n",
"## Equal characters in strings\n",
"\n",
"Given two strings, the idea is to consider the positions where their characters match:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"v = \"Wiesbaden\"\n",
"w = \"Potsdam\"\n",
"# s a – the matching characters of the two strings "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can extract those characters with a loop:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = [] # resulting equal characters\n",
"for i in range(min(map(len, [v, w]))): # loop over the shortest word's length\n",
" if v[i] == w[i]: # check character equality \n",
" m.append(v[i]) # add character\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's create a function that, given two strings, returns their equal characters:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def equal_chars(w, v):\n",
" m = [] # resulting equal characters\n",
" for i in range(min(map(len, [v, w]))): # loop over the shortest word's length\n",
" if v[i] == w[i]: # check character equality \n",
" m.append(v[i]) # add character\n",
" return m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By the way: thanks to Python's [list comprehensions](https://docs.python.org/3/howto/functional.html#generator-expressions-and-list-comprehensions) we can write the body of the function in one line:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def equal_chars(w, v):\n",
" return [v[i] for i in range(min(map(len, [v, w]))) if v[i] == w[i]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Similarity \n",
"\n",
"Now the number of equal characters between two strings defines a similarity measure. For example, the similarity of our two strings is:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(equal_chars(v, w))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Graph\n",
"\n",
"Now given a set of strings, for example, the 16 capitals of all German states:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"capitals_de = [\"Berlin\", \"Bremen\", \"Dresden\", \"Düsseldorf\", \"Erfurt\",\n",
" \"Hamburg\", \"Hannover\", \"Kiel\", \"Magdeburg\", \"Mainz\", \"München\",\n",
" \"Potsdam\", \"Saarbrücken\", \"Schwerin\", \"Stuttgart\", \"Wiesbaden\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we can create a graph with the strings as nodes by connecting strings whose similarity is larger than zero, that is, they have at least one position with equal characters:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import networkx as nx\n",
"\n",
"def sim_graph(words):\n",
" G = nx.Graph() # resulting graph\n",
"\n",
" for k, v in enumerate(words): # first node\n",
" for l, w in enumerate(words): # second node\n",
" if k > l: # avoid reverse duplicates\n",
" ec = equal_chars(v, w) # equal characters\n",
" sim = len(ec) # similarity\n",
" if sim > 0: # ignore dissimilar words\n",
" G.add_edge(v, w, label=\"\".join(ec), weight=sim) # add edge\n",
" return G"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's compute the graph for our set of capitals:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"g = sim_graph(capitals_de)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A good way to understand a graph is to visualise it:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from networkx.drawing.nx_agraph import graphviz_layout\n",
"import matplotlib.pyplot as plt\n",
" \n",
"pos = graphviz_layout(g, prog='dot')\n",
"nx.draw(g, pos, with_labels=True, arrows=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This layout is not the best but we can try to use graphviz directly:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from networkx.drawing.nx_pydot import write_dot\n",
"import pydot\n",
"from IPython.display import HTML, display\n",
"import random\n",
"\n",
"write_dot(g, \"graph.dot\")\n",
"graph = pydot.graph_from_dot_file(\"graph.dot\")\n",
"graph[0].write_svg(\"graph.svg\")\n",
" \n",
"display(HTML('<img src=\"graph.svg?{0}\">'.format(random.randint(0,2e9)))) "
]
}
],
"metadata": {
"language_info": {
"name": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}