snipt

Ctrl+h for KB shortcuts

Python

Flickr-style tag normalisation

1
2
3
4
5
6
import string

tag_chars = string.lowercase + string.digits
valid_char = lambda c: c in tag_chars

normalize = lambda tag: filter(valid_char, str(tag).lower())
https://snipt.net/embed/42f5eada9a8e3e83fbcb088f2540447a/
https://snipt.net/raw/42f5eada9a8e3e83fbcb088f2540447a/
42f5eada9a8e3e83fbcb088f2540447a
python
Python
6
2016-09-20T09:45:08
True
False
False
/api/public/snipt/2626/
flickr-style-tag-normalisation
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="kn">import</span> <span class="nn">string</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="n">tag_chars</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">lowercase</span> <span class="o">+</span> <span class="n">string</span><span class="o">.</span><span class="n">digits</span> </span><span id="L-4"><a name="L-4"></a><span class="n">valid_char</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">c</span><span class="p">:</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">tag_chars</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="n">normalize</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">tag</span><span class="p">:</span> <span class="nb">filter</span><span class="p">(</span><span class="n">valid_char</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">tag</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span> </span></pre></div> </td></tr></table>
flickr, python
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.