<commit_before>/*
 * SenseRank.cc
 *
 * Implements the PageRank graph centrality algorithm for word-senses.
 *
 * Copyright (c) 2008 <NAME> <<EMAIL>>
 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "FollowLink.h"
#include "ForeachWord.h"
#include "SenseRank.h"
#include "Node.h"
#include "SimpleTruthValue.h"
#include "TruthValue.h"

using namespace opencog;

// #define DEBUG

SenseRank::SenseRank(void)
{
	// The page-rank damping factor. Normally taken to be quite large.
	damping_factor = 0.85;

	// The convergence damping factor, used to determine when the page
	// rank has converged. This is used to create an exponentially 
	// decaying average of the last N page-rank adjustments, where 
	// N = 1/convergence_damper.  Basically, N should be choosen so
	// that N == total number of word-senses in graph. For now, this
	// is assumed to be 20 (i.e. a single-sentence-worth of senses.)
	// For multi-sentence use, this should probably be pumped up.
	double N = 50;
	convergence_damper = 1.0/N;
}

SenseRank::~SenseRank()
{
}

/**
 * For each parse of the sentence, perform the ranking algo.
 */
void SenseRank::iterate(Handle h)
{
	foreach_parse(h, &SenseRank::rank_parse, this);
}

/**
 * For each parse, find some place to start. Since (at this point),
 * noun sense ranking and verb sense ranking are disjoint, start 
 * off on every word. That is, as of right now, the graph consists
 * of multplie connected components.
 */
bool SenseRank::rank_parse(Handle h)
{
	foreach_word_instance(h, &SenseRank::start_word, this);
	return false;
}

/**
 * Pick some random word sense to start at.
 */
bool SenseRank::start_word(Handle h)
{
	// Only noun-senses and verb-senses get ranked.
	std::string pos = get_pos_of_word_instance(h);
	if (pos.compare("#noun") && pos.compare("#verb")) return false;

	foreach_word_sense_of_inst(h, &SenseRank::start_sense, this);
	return false;
}

bool SenseRank::start_sense(Handle word_sense_h,
                            Handle sense_link_h)
{
	rand_walk(sense_link_h);
	return true;
}

/**
 * Compute the page rank for the indicated (word-inst,word-sense) pair.
 * The handle argument points at a (word-inst,word-sense) pair.
 * The page rank is defined as
 *
 * P(a) = (1-d) + d* sum_b w_ba / (sum_c w_bc) P(b)<commit_msg>bug fix<commit_after>/*
 * SenseRank.cc
 *
 * Implements the PageRank graph centrality algorithm for word-senses.
 *
 * Copyright (c) 2008 <NAME> <<EMAIL>>
 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "FollowLink.h"
#include "ForeachWord.h"
#include "SenseRank.h"
#include "Node.h"
#include "SimpleTruthValue.h"
#include "TruthValue.h"

using namespace opencog;

// #define DEBUG

SenseRank::SenseRank(void)
{
	// The page-rank damping factor. Normally taken to be quite large.
	damping_factor = 0.85;

	// The convergence damping factor, used to determine when the page
	// rank has converged. This is used to create an exponentially 
	// decaying average of the last N page-rank adjustments, where 
	// N = 1/convergence_damper.  Basically, N should be choosen so
	// that N == total number of word-senses in graph. For now, this
	// is assumed to be 20 (i.e. a single-sentence-worth of senses.)
	// For multi-sentence use, this should probably be pumped up.
	double N = 50;
	convergence_damper = 1.0/N;
}

SenseRank::~SenseRank()
{
}

/**
 * For each parse of the sentence, perform the ranking algo.
 */
void SenseRank::iterate(Handle h)
{
	foreach_parse(h, &SenseRank::rank_parse, this);
}

/**
 * For each parse, find some place to start. Since (at this point),
 * noun sense ranking and verb sense ranking are disjoint, start 
 * off on every word. That is, as of right now, the graph consists
 * of multplie connected components.
 */
bool SenseRank::rank_parse(Handle h)
{
	foreach_word_instance(h, &SenseRank::start_word, this);
	return false;
}

/**
 * Pick some random word sense to start at.
 */
bool SenseRank::start_word(Handle h)
{
	// Only noun-senses and verb-senses get ranked.
	std::string pos = get_pos_of_word_instance(h);
	if (pos.compare("#noun") && pos.compare("#verb")) return false;

	foreach_word_sense_of_inst(h, &SenseRank::start_sense, this);
	return false;
}

bool SenseRank::start_sense(Handle word_sense_h,
                            Handle sense_link_h)
{
	// Rule out things that aren't actually senses
	Node *n = dynamic_cast<Node *>(TLB::getAtom(word_sense_h));
	if ((n == NULL) || (n->getType() != WORD_SENSE_NODE)) return false;

	rand_walk(sense_link_h);
	return true;
}

/**
 * Compute the page rank for the indicated (word-inst,word-sense) pair.
 * The handle argument points at a (word-inst,word-sense) pair.
 * The page rank is defined as
 *
 * P(a) = (1-d) + d* sum_b w_ba / (sum_c w_bc) P(b)
{'fraction_non_alphanumeric': 0.12329055333473596, 'fraction_numerical': 0.006311803071744161, 'mean_word_length': 3.8510204081632655, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '780265', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1588, 'n_words': 530}