Python: I want to check for the count of the words in the string
<p>I managed to do that but the case I'm struggling with is when I have to consider 'color' equal to 'colour' for all such words and return count accordingly. To do this, I wrote a dictionary of common words with spelling changes in American and GB English for this, but pretty sure this isn't the right approach.</p>

<pre><code> ukus=dict()      ukus={'COLOUR':'COLOR','CHEQUE':'CHECK',
'PROGRAMME':'PROGRAM','GREY':'GRAY',
'JEWELLERY':'JEWELERY','ALUMINIUM':'ALUMINUM',
'THEATER':'THEATRE','LICENSE':'LICENCE','ARMOUR':'ARMOR',
'ARTEFACT':'ARTIFACT','CENTRE':'CENTER',
'CYPHER':'CIPHER','DISC':'DISK','FIBRE':'FIBER',
'FULFILL':'FULFIL','METRE':'METER',
'SAVOURY':'SAVORY','TONNE':'TON','TYRE':'TIRE',
'COLOR':'COLOUR','CHECK':'CHEQUE',
'PROGRAM':'PROGRAMME','GRAY':'GREY',
'JEWELERY':'JEWELLERY','ALUMINUM':'ALUMINIUM',
'THEATRE':'THEATER','LICENCE':'LICENSE','ARMOR':'ARMOUR',
'ARTIFACT':'ARTEFACT','CENTER':'CENTRE',
'CIPHER':'CYPHER','DISK':'DISC','FIBER':'FIBRE',
'FULFIL':'FULFILL','METER':'METRE','SAVORY':'SAVOURY',
'TON':'TONNNE','TIRE':'TYRE'}
</code></pre>

<p>This is the dictionary I wrote to check the values. As you can see this is degrading the performance. Pyenchant isn't available for 64bit python. Someone please help me out. Thank you in advance.</p>

<p><strong>Step 1:</strong>
Create a temporary string and then replace all the words with <code>values</code> of your dict with it's corresponding keys as:</p>

<pre><code>&gt;&gt;&gt; temp_string = str(my_string)
&gt;&gt;&gt; for k, v in ukus.items():
...     temp_string = temp_string.replace(" {} ".format(v), " {} ".format(k))  # &lt;--surround by space " " to replace only words
</code></pre>

<p><strong>Step 2:</strong>
Now, in order to find words in the string, firstly split it into <code>list</code> of words and then use <a href="https://pymotw.com/2/collections/counter.html" rel="nofollow"><code>itertools.Counter()</code></a> to get count of each element in the <code>list</code>. Below is the sample code:</p>

<pre><code>&gt;&gt;&gt; from collections import Counter
&gt;&gt;&gt; my_string = 'Hello World! Hello again. I am saying Hello one more time'
&gt;&gt;&gt; count_dict = Counter(my_string.split())
# Value of count_dict:
# Counter({'Hello': 3, 'saying': 1, 'again.': 1, 'I': 1, 'am': 1, 'one': 1, 'World!': 1, 'time': 1, 'more': 1})
&gt;&gt;&gt; count_dict['Hello']
3
</code></pre>

<p><strong>Step 3:</strong> 
Now, since you want the count of both "colour" and "color" in your dict, re-iterate the <code>dict</code> to add those values, and the missing values as "0"</p>

<pre><code>for k, v in ukus.items():
    if k in count_dict:
        count_dict[v] = count_dict[k]
    else:
        count_dict[v] = count_dict[k] = 0   
</code></pre>

python count