def correlateRatingsTags(Ratings, Group='FullDirTrees'): import numpy.oldnumeric.mlab as MLab DirectionGivers = '(EDA|EMWC|KLS|KXP|TJS|WLH)' Routes = '.*' Envs = '.*' Suffix = 'Dirs_\d.txt$' Directions = DirectionCorpusReader('_'.join( [DirectionGivers, Envs, Routes, Suffix])) CFDist = LearnCondDist(Directions, list(Directions.items(Group)), Start='DIRECTIONS', verbose=1) TagOrder = [ tag.symbol() for tag in CFDist.conditions() if not (tag.symbol().endswith('_P') or tag.symbol().endswith('_N') or tag.symbol().endswith('_V')) ] TagOrder.sort() results = {} for item in Directions.items(Group): dirID = item.split('-')[1] if not Ratings.has_key(dirID): print 'Skipping', dirID continue TagCounts = {} DirModel = LearnCondDist(Directions, [item], Start='DIRECTIONS') for nonterm in DirModel.conditions(): tag = nonterm.symbol() #print nonterm, [DirModel[nonterm].count(s) for s in DirModel[nonterm].samples()] if tag in TagOrder: TagCounts[tag] = MLab.sum([ DirModel[nonterm].count(s) for s in DirModel[nonterm].samples() ]) TraitList = [ Ratings[dirID][2], Ratings[dirID][3], Ratings[dirID][4], Ratings[dirID][5], Ratings[dirID][6] + Ratings[dirID][7] ] for TagName in TagOrder: if TagCounts.has_key(TagName): TraitList.append(TagCounts[TagName]) else: TraitList.append(0) results[dirID] = TraitList #print dirID, TraitList for k in Ratings.keys(): if not results.has_key(k): results[k] = [0] * (len(TagOrder) + 5) # Number of ratings used return results, TagOrder
def correlateRatingsTags(Ratings,Group='FullDirTrees'): import numpy.oldnumeric.mlab as MLab DirectionGivers = '(EDA|EMWC|KLS|KXP|TJS|WLH)' Routes = '.*' Envs = '.*' Suffix = 'Dirs_\d.txt$' Directions = DirectionCorpusReader('_'.join([DirectionGivers,Envs,Routes,Suffix])) CFDist = LearnCondDist(Directions, list(Directions.items(Group)), Start='DIRECTIONS',verbose=1) TagOrder = [tag.symbol() for tag in CFDist.conditions() if not (tag.symbol().endswith('_P') or tag.symbol().endswith('_N') or tag.symbol().endswith('_V'))] TagOrder.sort() results = {} for item in Directions.items(Group): dirID = item.split('-')[1] if not Ratings.has_key(dirID): print 'Skipping', dirID; continue TagCounts = {} DirModel = LearnCondDist(Directions, [item], Start='DIRECTIONS') for nonterm in DirModel.conditions(): tag = nonterm.symbol() #print nonterm, [DirModel[nonterm].count(s) for s in DirModel[nonterm].samples()] if tag in TagOrder: TagCounts[tag] = MLab.sum([DirModel[nonterm].count(s) for s in DirModel[nonterm].samples()]) TraitList = [Ratings[dirID][2], Ratings[dirID][3], Ratings[dirID][4], Ratings[dirID][5], Ratings[dirID][6]+Ratings[dirID][7]] for TagName in TagOrder: if TagCounts.has_key(TagName): TraitList.append(TagCounts[TagName]) else: TraitList.append(0) results[dirID] = TraitList #print dirID, TraitList for k in Ratings.keys(): if not results.has_key(k): results[k] = [0] * (len(TagOrder)+5) # Number of ratings used return results, TagOrder
def rosen(x): # The Rosenbrock function return MLab.sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)