Sentence clustering
<p>I have a huge number of names from different sources.</p>

<ol>
<li>I need to extract all the groups (part of the names), which repeat from one to another.
In the example below program should locate: Post, Office, Post Office.</li>
<li>I need to get popularity count.</li>
</ol>

<p>So I want to extract a sorted by popularity list of phrases.</p>

<p>Here is an example of names:</p>

<pre><code>Post Office - High Littleton
Post Office Pilton Outreach Services
Town Street Post Office
post office St Thomas
</code></pre>

<p>Basically need to find out some algorithm or better library, to get such results:</p>

<pre><code>Post Office: 16999
Post: 17934
Office: 16999
Tesco: 7300
...
</code></pre>

<p>Here is the full <a href="https://gist.github.com/maZahaca/a54046a4cc7ab27f9d06751b89aa7446" rel="nofollow">example of names</a>.</p>

<p>I wrote a code which is fine for single words, but not for sentences:</p>

<pre><code>from textblob import TextBlob
import operator

title_file = open("names.txt", 'r')
blob = TextBlob(title_file.read())
list = sorted(blob.word_counts.items(), key=operator.itemgetter(1))
print list
</code></pre>

<p>Are you looking for something like this?</p>

<pre><code>workspace={}

with open('names.txt','r') as f:
  for name in f:
    if len(name): # makes sure line isnt empty
      if name in workspace:
        workspace[name]+=1
      else:
        workspace[name]=1

for name in workspace:
  print "{}: {}".format(name,workspace[name])
</code></pre>

python machine-learning cluster-analysis