I decided to try out Python (3.x) two or so weeks ago, and this is my first real script using it. The program I've written below is slow, clunky, inefficient, inaccurate, and probably poorly coded! But it produces what I want most of the time, and it was really just a project to become accustomed to different Python techniques.
What it should do is, recieve a string, one that looks like a tag from a social media site (#socialmedia, #20000leaguesunder, etc.), and then based on a dictionary, split the string into logical words.
So "#volunteeringtoday" becomes "volunteering today" and so on.
I understand my algorithm is poor, so any help with the actual Python and formatting would be preferred.
import re
class Splitter():
input = ''
clean = ''
words = []
def __init__(self, string):
'''
Set the input string
'''
self.input = string
def split(self):
'''
Split the input string. Get the clean version then start the separation loop.
'''
self.clean = self.cleanString(self.input)
return self.separateString(self.clean)
def separateString(self, inp):
'''
Separate the string input. Break apart and look for matches in a dictionary.
'''
# Index each character in the input string
for ind in range(len(inp)):
# Build a segment
built = self.partition(inp, ind)
#print('"'+built+'"') # Add for details
# If only one letter remains, steal a letter from the previous match
if len(built) == 1:
built = self.words[-1][-1] + self.lastLarge
self.words[-1] = self.words[-1][0:-1]
return self.separateString(built)
# Check if segment ends with a digit. Separate it if it does.
if re.match('\d+$', built):
built = re.sub("([^\d])\d", "", built)
self.words.append(built)
return self.separateString(inp.replace(built, '', 1))
# It does not end with a digit
else:
# Iterate over each line in the dictionary
for word in open('word.txt', 'r'):
if self.cleanString(word) == built:
self.words.append(built)
# Check if list of separations joined together is equal to original
if ''.join(self.words) == self.clean:
return ' '.join(self.words)
else:
# Loop back through to separate more
self.lastLarge = inp.replace(built, '', 1)
return self.separateString(inp.replace(built, '', 1))
def partition(self, value, index):
'''
Break apart string. Length from right
'''
return value[0:len(value) - index]
def cleanString(self, string):
'''
Remove all non-alphabetic and non-digit characters.
'''
return re.sub("[^\d[a-z]]*", "", string.lower().strip())
And then you can sample it with something such as:
splitter = Splitter(input('Hashtag: '))
print('\nFinal:',splitter.split())
word.txt is a text file with roughly 60k words. One on each line. The list can be found here.
Any tips on making it more professional?