def generate_recommendation_data():
 global recommendation_data
 if recommendation_data:
  return recommendation_data
 tree = get_topic_nodes_with_children(parent='root')
 topic_index = 0
 subtopic_index = 0
 for topic in tree:
  subtopic_index = 0
  for subtopic_id in topic['children']:
   neighbors_dist_1 = get_neighbors_at_dist_1(topic_index, subtopic_index, topic)
   recommendation_data[subtopic_id] = {'related_subtopics': ([(subtopic_id + ' 0')] + neighbors_dist_1)}
   subtopic_index += 1
  topic_index += 1
 for subtopic in recommendation_data:
  related = recommendation_data[subtopic]['related_subtopics']
  other_neighbors = get_subsequent_neighbors(related, recommendation_data, subtopic)
  recommendation_data[subtopic]['related_subtopics'] += other_neighbors
 for subtopic in recommendation_data:
  at_dist_4 = []
  at_dist_lt_4 = []
  for recc in recommendation_data[subtopic]['related_subtopics']:
   if (recc.split(' ')[1] == '4'):
    at_dist_4.append(recc.split(' ')[0])
   else:
    at_dist_lt_4.append(recc.split(' ')[0])
  sorted_related = (at_dist_lt_4 + at_dist_4)
  recommendation_data[subtopic]['related_subtopics'] = sorted_related
 return recommendation_data

def generate_recommendation_data ( ) : global recommendation_data if recommendation_data : return recommendation_data tree = get_topic_nodes_with_children ( parent = 'root' ) topic_index = 0 subtopic_index = 0 for topic in tree : subtopic_index = 0 for subtopic_id in topic [ 'children' ] : neighbors_dist_1 = get_neighbors_at_dist_1 ( topic_index , subtopic_index , topic ) recommendation_data [ subtopic_id ] = { 'related_subtopics' : ( [ ( subtopic_id + ' 0' ) ] + neighbors_dist_1 ) } subtopic_index += 1 topic_index += 1 for subtopic in recommendation_data : related = recommendation_data [ subtopic ] [ 'related_subtopics' ] other_neighbors = get_subsequent_neighbors ( related , recommendation_data , subtopic ) recommendation_data [ subtopic ] [ 'related_subtopics' ] += other_neighbors for subtopic in recommendation_data : at_dist_4 = [ ] at_dist_lt_4 = [ ] for recc in recommendation_data [ subtopic ] [ 'related_subtopics' ] : if ( recc . split ( ' ' ) [ 1 ] == '4' ) : at_dist_4 . append ( recc . split ( ' ' ) [ 0 ] ) else : at_dist_lt_4 . append ( recc . split ( ' ' ) [ 0 ] ) sorted_related = ( at_dist_lt_4 + at_dist_4 ) recommendation_data [ subtopic ] [ 'related_subtopics' ] = sorted_related return recommendation_data
traverses topic tree to generate a dictionary with related subtopics per subtopic .