Tokenize String in Python with Optional components -
i'm building simple webapp in flask , wanted able tokenize strings fit format , variables. example understand this
if input_string = 'play me song eminem' artist = eminem action = play if input_string = 'play me sad song eminem' artist = eminem action = play emotion = sad if input_string = 'play me sad song' emotion = sad action = play
i'm not looking @ natural language processing, rather picking tokens if exist. i'm looking @ format string [play/show/do] {[emotion]} [song/image/video] { [artist]} {[topic]}
where in {} optional, , should none/null if not present in string
i'm unsure how proceed though. regex way go?
this 1 (straight forward) possibility of 1000s, may give direction dig. notice there no error check:
class command(object): def __init__(self, input_string): words = input_string.split() self.artist = self.get_artist(words) self.action = self.get_action(words) self.emotion = self.get_emotion(words) def get_artist(self, words): try: index = words.index('by') + 1 except valueerror: return else: return words[index] def get_action(self, words): return words[0] def get_emotion(self, words): index = words.index('song') - 1 emotion = words[index] return emotion if emotion not 'a' else none >>> tokenise import command >>> command = command('play me song eminem') >>> command.artist 'eminem' >>> command.action 'play' >>> command.emotion >>> command = command('play me sad song eminem') >>> command.artist 'eminem' >>> command.action 'play' >>> command.emotion 'sad' >>> command = command('play me sad song') >>> command.artist >>> command.action 'play' >>> command.emotion 'sad'
Comments
Post a Comment