In [64]:
import sys
sys.path.append('../vectorsearch/')
import nltk_helper
#reload(nltk_helper)

nltk_helper.clean_nltk('Are there any-bars around town?!?!?!')


Out[64]:
['any-bars', 'around', 'town']

In [93]:
import sys
sys.path.append('../vectorsearch/')
import nltk_helper
def parse_query(query):
    '''
    Parse the search string which must follow a dict specification format, 
    *Must* use semicolons to separate entries.
    e.g. query = "dive bar:5 ; music : [1,4] ; expensive : -1 "
    
    query : str
        Search string
    '''
    try: 
        terms = query.split(';')
        query_dict = {}
        for term in terms: 
            key, val = term.split(':')
            key = nltk_helper.clean_nltk(key)[0]

            if "[" in val and "]" in val: 
                val = val.replace('[','').replace(']','').split(",")
                try:
                    val = [float(weight) for weight in val]
                    query_dict[key] = val
                except: 
                    print 'Warning, value for key %s could not be parsed'%key
            # If just a floating point number..
            else: 
                try: 
                    val = float(val)
                    query_dict[key] = val
                except: 
                    print 'Warning, value for key %s could not be parsed'%key
    except: 
        print 'Sorry... invalid query try again.  example: "dive bar:5 ; music : [1,4] ; expensive : -1 "'
    return query_dict



if __name__=='__main__':
    print parse_query('dive bar : [1,10] ; music:1 ')


{'music': 1.0, 'dive-bar': [1.0, 10.0]}

In [68]:



Out[68]:
['1', '2']

In [ ]:


In [ ]:


In [ ]: