def populate_topics_from_phantom_forms(cls): all_forms = phantom_on_the_capitol.retrieve_form_elements([x.bioguide_id for x in Legislator.query.all()]) all_topics = {} for legislator, req in all_forms.iteritems(): for key, val in req.iteritems(): for step in val: if step['value'] == '$TOPIC': if type(step['options_hash']) is dict: keys = step['options_hash'].keys() else: keys = step['options_hash'] for k in keys: k = k.strip() if all_topics.has_key(k): all_topics[k] += 1 else: all_topics[k] = 1 failed_topics = [] for topic, count in all_topics.iteritems(): result = select_solver.choose('test', [topic.lower()]) if result is None: failed_topics.append(topic.lower()) elif result: db_first_or_create(Topic, name=topic.lower()) all_topics = Topic.query.filter_by(wikipedia_parent=None) for f_topic in failed_topics: try: lowest = (None, None) for topic in all_topics: print topic.name, f_topic d = jellyfish.damerau_levenshtein_distance(unicode(str(topic.name)), unicode(str(f_topic))) if lowest[0] is None or lowest[1] > d: lowest = (topic, d) print 'Adding ' + f_topic + ' with parent ' + lowest[0].name db_first_or_create(Topic, name=f_topic, wikipedia_parent=lowest[0].id) except: continue
def send(self): """ Method that actually passes information to phantom of the capitol. @return: self @rtype: models.MessageLegislator """ if not self.is_sent(): for bioguide_id, ra in phantom_on_the_capitol.retrieve_form_elements([self.legislator.bioguide_id]).iteritems(): json_dict = self.map_to_contact_congress() for step in ra['required_actions']: field = step.get('value') options = step.get('options_hash') if options is not None: # convert first to dictionary for convenience if type(options) is not dict: options = {k: k for k in options} if field == '$TOPIC': # need lower case strings for select-solver options = {k.lower(): v for k, v in options.items()} try: # try to determine best topic based off content of text choice = Topic.topic_for_message(options.keys(), self.message) json_dict['fields'][field] = choice self.topic = Topic.query.filter_by(name=choice).first() except: # if failed, choose a random topic pass if field not in json_dict['fields'] or json_dict['fields'][field] not in options.values(): json_dict['fields'][field] = random.choice(options.values()) if field not in json_dict['fields'].keys(): print 'What the heck is ' + step.get('value') + ' in ' + bioguide_id + '?' result = phantom_on_the_capitol.fill_out_form(json_dict) self.sent = result['status'] == 'success' self.send_status = json.dumps(result) db.session.commit() return self