def random_walk(embeddings, positive_seeds, negative_seeds, beta=0.9, **kwargs):
    """
    Learns polarity scores via random walks with teleporation to seed sets.
    Main method used in paper. 
    """

    def run_random_walk(M, teleport, beta, **kwargs):
        def update_seeds(r):
            r += (1 - beta) * teleport / np.sum(teleport)

        return run_iterative(
            M * beta, np.ones(M.shape[1]) / M.shape[1], update_seeds, **kwargs
        )

    if not type(positive_seeds) is dict:
        positive_seeds = {word: 1.0 for word in positive_seeds}
        negative_seeds = {word: 1.0 for word in negative_seeds}
    words = embeddings.iw
    M = transition_matrix(embeddings, **kwargs)
    rpos = run_random_walk(
        M, weighted_teleport_set(words, positive_seeds), beta, **kwargs
    )
    rneg = run_random_walk(
        M, weighted_teleport_set(words, negative_seeds), beta, **kwargs
    )
    return {w: rpos[i] / (rpos[i] + rneg[i]) for i, w in enumerate(words)}
Exemple #2
0
def label_propagate_probabilistic(embeddings,
                                  seeds_map,
                                  normalize=True,
                                  **kwargs):
    """
    Learns polarity scores via standard label propagation from seed sets.
    One walk per label. Scores normalized to probabilities. 
    """
    words = embeddings.iw
    M = transition_matrix(embeddings, **kwargs)
    teleport_set_map = {}
    for seed_key, seed_list in seeds_map.items():
        teleport_set_map[seed_key] = teleport_set(words, seed_list)

    def update_seeds(r):
        idm = np.eye(len(seeds_map))
        for seed_key, w_indices in teleport_set_map.items():
            r[w_indices] = idm[seed_key]
        r /= np.sum(r, axis=1)[:, np.newaxis]

    r = run_iterative(M, np.random.random((M.shape[0], len(seeds_map))),
                      update_seeds, **kwargs)
    polarities = {}
    for i, w in enumerate(words):
        polarities[w] = Counter()
        for seed_key in seeds_map:
            polarities[w][seed_key] = r[i][seed_key]
        if normalize:
            polarities[w] = normalize_counter(polarities[w])
    return polarities
Exemple #3
0
def random_walk(embeddings, seeds_map, beta=0.9, normalize=True, **kwargs):
    """
    Learns polarity scores via random walks with teleporation to seed sets.
    Main method used in paper. 
    """
    def run_random_walk(M, teleport, beta, **kwargs):
        def update_seeds(r):
            r += (1 - beta) * teleport / np.sum(teleport)

        return run_iterative(M * beta,
                             np.ones(M.shape[1]) / M.shape[1], update_seeds,
                             **kwargs)

    seeds_map_dict = {}
    for seed_key, seed_list in seeds_map.items():
        seeds_map_dict[seed_key] = {
            word: 1.0
            for i, word in enumerate(seed_list)
        }
    words = embeddings.iw
    M = transition_matrix(embeddings, **kwargs)
    scores_dict = {}
    for seed_key, seeds in seeds_map_dict.items():
        scores_dict[seed_key] = run_random_walk(
            M, weighted_teleport_set(words, seeds), beta, **kwargs)
    polarities = {}
    for i, w in enumerate(words):
        polarities[w] = Counter()
        for seed_key in scores_dict:
            polarities[w][seed_key] = scores_dict[seed_key][i]
        if normalize:
            polarities[w] = normalize_counter(polarities[w])
    return polarities
def label_propagate_continuous(embeddings, positive_seeds, negative_seeds, **kwargs):
    """
    Learns polarity scores via standard label propagation from seed sets.
    One walk for both labels, continuous non-normalized scores.
    """
    words = embeddings.iw
    M = transition_matrix(embeddings, **kwargs)
    pos, neg = teleport_set(words, positive_seeds), teleport_set(words, negative_seeds)
    def update_seeds(r):
        r[pos] = 1
        r[neg] = -1
    r = run_iterative(M, np.zeros(M.shape[0]), update_seeds, **kwargs)
    return {w: r[i] for i, w in enumerate(words)}
def label_propagate_probabilistic(embeddings, positive_seeds, negative_seeds, **kwargs):
    """
    Learns polarity scores via standard label propagation from seed sets.
    One walk per label. Scores normalized to probabilities. 
    """
    words = embeddings.iw
    M = transition_matrix(embeddings, **kwargs)
    pos, neg = teleport_set(words, positive_seeds), teleport_set(words, negative_seeds)
    def update_seeds(r):
        r[pos] = [1, 0]
        r[neg] = [0, 1]
        r /= np.sum(r, axis=1)[:, np.newaxis]
    r = run_iterative(M, np.random.random((M.shape[0], 2)), update_seeds, **kwargs)
    return {w: r[i][0] / (r[i][0] + r[i][1]) for i, w in enumerate(words)}
def random_walk(embeddings, positive_seeds, negative_seeds, beta=0.9, **kwargs):
    """
    Learns polarity scores via random walks with teleporation to seed sets.
    Main method used in paper. 
    """
    def run_random_walk(M, teleport, beta, **kwargs):
        def update_seeds(r):
            r += (1 - beta) * teleport / np.sum(teleport)
        return run_iterative(M * beta, np.ones(M.shape[1]) / M.shape[1], update_seeds, **kwargs)

    if not type(positive_seeds) is dict:
        positive_seeds = {word:1.0 for word in positive_seeds}
        negative_seeds = {word:1.0 for word in negative_seeds}
    words = embeddings.iw
    M = transition_matrix(embeddings, **kwargs)
    rpos = run_random_walk(M, weighted_teleport_set(words, positive_seeds), beta, **kwargs)
    rneg = run_random_walk(M, weighted_teleport_set(words, negative_seeds), beta, **kwargs)
    return {w: rpos[i] / (rpos[i] + rneg[i]) for i, w in enumerate(words)}