def buildCollection(dbh, p, collectionName): ''' Build the collection specified''' # Create the collection try: if p.dropCollection==True: if p.verbose==True: print "---- Dropping Collection." print "---- Creating Collection." dbh.drop_collection(collectionName) dbh.create_collection(collectionName) except Exception, e: handleErrors(p, e)
def mongoUpdate(p, collection, word, slang): """ Inserts the word and its phonetic representation into a new mongo document. """ # The query condition for the update q = {"word": word, "wordlen": len(word)} # Just in case the word doesn't already exist, create a new dictionary item with it, so that it maps to the emoticon/slang try: exists = collection.find(q).count() if exists == 0: collection.insert(q) except Exception, e: handleErrors(p, e)
def main(configFile): ''' Holds it all together ''' # Get the config information into a single object. p also gets passed into the listener p = getConfigParameters(configFile) # Handle the mongo connection and authentication c, dbh, collection, emoCollection = getMongoHandles(p) # Open the english dictionary file and loop it try: f = open(os.path.join(p.sourcePath, p.enPlainFile), 'r') except Exception, e: handleErrors(p, e)
def buildIndexes(p, collection, collHandle): ''' Build the indexes specified''' # Create indexes try: if p.verbose==True: print "---- Create Plain Indexes." for index in collection['plain']: collHandle.create_index([(index, ASCENDING)]) if p.verbose==True: print "---- Index Created On: %s." %index except Exception, e: handleErrors(p, e)
def mongoInserter(p, collection, word, pho): ''' Inserts the word and its phonetic representation into a new mongo document. ''' record = {'word' : word, 'pho' : pho, 'wordlen' : len(word), 'pholen' : len(pho)} try: if collection.find(record).count() > 0: res = 0 else: res = collection.insert(record) except Exception, e: handleErrors(p, e) res = None
def main(configFile, file="norm"): """ Holds it all together """ # Get the config information into a single object. p also gets passed into the listener p = getConfigParameters(configFile) # Handle the mongo connection and authentication c, dbh, collection, emoCollection = getMongoHandles(p) if file == "norm": fileToProcess = p.enNormalisedFile elif file == "slang": fileToProcess = p.slangFile # Open the english dictionary file and loop it try: f = open(os.path.join(p.sourcePath, fileToProcess), "r") except Exception, e: print e handleErrors(p, e)
print "---- Index Created On: %s." %index except Exception, e: handleErrors(p, e) # Create compound indexes try: if p.verbose==True: print "---- Create Compound Indexes." for index in collection['compound']: if p.verbose==True: print "---- Index Created On: %s." %index except Exception, e: handleErrors(p, e) #------------------------------------------------------------------------ def buildCollection(dbh, p, collectionName): ''' Build the collection specified''' # Create the collection try: if p.dropCollection==True: if p.verbose==True: print "---- Dropping Collection." print "---- Creating Collection." dbh.drop_collection(collectionName) dbh.create_collection(collectionName) except Exception, e:
exists = collection.find(q).count() if exists == 0: collection.insert(q) except Exception, e: handleErrors(p, e) # Adds to the list if slang isn't already present up = {'$addToSet':{'emo':token}} try: response = collection.update(q, up) except Exception, e: response = None handleErrors(p, e) return response #---------------------------------------------------------------------------------------- def getDespaced(slang): ''' Removes spaces and returns slang(s) as a list''' # Add the existing slang to a list slangs = [slang] # Remove all spaces from the slang term and add that to the list too while slang.find(' ') >= 0: slang = slang.replace(' ', '')