Example #1
0
    def get_next_neighbour(self, p_node_name_now : str, p_mpdj_data : MPDJData,
                         p_play_data : PlayData,
                         p_mpd_connection : MPDConnection) -> str:
        """Returns the next neighbour, according to a random choice
            weighted by songs in collected divided by 1 plays
            of songs in this node."""
        random.seed()
        neighbour_node_names = p_mpdj_data.get_neighbours_for_node_name(p_node_name_now)
        # If node does not have any neighbours, we will select a random one from the set
        # of all neighbours.
        if len(neighbour_node_names) < 1:
            print('No neighbours for {} considering all nodes as next'.format(p_node_name_now))
            neighbour_node_names = p_mpdj_data.get_song_selection_names()
        if len(neighbour_node_names) > 1 and p_play_data.previous_node in neighbour_node_names:
            neighbour_node_names.remove(p_play_data.previous_node)

        nodes_with_song_count_not_zero = self.get_possible_next_neighbours(p_node_name_now,
                                                                           p_mpdj_data,
                                                                           p_play_data,
                                                                           p_mpd_connection)
        node_weights = calculate_node_weight_with_song_play_count(
            p_play_data,
            list(nodes_with_song_count_not_zero.values()))
        choice = random.choices(population=[*nodes_with_song_count_not_zero],
                                weights=node_weights,k=1)
        return choice[0]
Example #2
0
 def get_possible_next_neighbours(self, p_node_name_now : str, p_mpdj_data : MPDJData,
                                  p_play_data : PlayData, p_mpd_connection : MPDConnection):
     """Returns those neighbors who could be selected as next neighbors. Dependent of
         p_node_name_now, p_mpdj_data, p_play_data and p_mpd_connection."""
     neighbours_in_graph = p_mpdj_data.get_neighbours_for_node_name(p_node_name_now)
     if len(neighbours_in_graph) < 1:
         print('No neighbours for {} considering all nodes as next'.format(p_node_name_now))
         neighbours_in_graph = p_mpdj_data.get_song_selection_names()
     if len(neighbours_in_graph) > 1 and p_play_data.previous_node in neighbours_in_graph:
         neighbours_in_graph.remove(p_play_data.previous_node)
     neighbours_with_song_count_not_zero = dict()
     for node in neighbours_in_graph:
         songs_in_node = p_mpdj_data.get_song_selection_by_name(node).get_songs(p_mpd_connection)
         if len(songs_in_node) == 0:
             sys.stderr.write('Node {} has now songs, ignoring it.'.format(node))
             sys.stderr.flush()
             continue
         neighbours_with_song_count_not_zero[node] = songs_in_node
     return neighbours_with_song_count_not_zero