Пример #1
0
static void
build_graph(struct lattice_info* info, int from, int to)
{
  int i;
  struct lattice_node* node;
  struct lattice_node* left_node;

  /* 始点となるノードを追加 */
  node = alloc_lattice_node(info, NULL, NULL, from);
  push_node(info, node, from);

  /* info->lattice_node_list[index]にはindexまでの遷移が入っているのであって、
   * indexからの遷移が入っているのではない
   */

  /* 全ての遷移を左から試す */
  for (i = from; i < to; ++i) {
    for (left_node = info->lattice_node_list[i].head; left_node;
	 left_node = left_node->next) {
      struct meta_word *mw;
      /* i文字目に到達するlattice_nodeのループ */

      for (mw = info->sc->word_split_info->cnode[i].mw; mw; mw = mw->next) {
	int position;
	struct lattice_node* new_node;
	/* i文字目からのmeta_wordのループ */

	if (mw->can_use != ok) {
	  continue; /* 決められた文節の区切りをまたぐmetawordは使わない */
	}
	position = i + mw->len;
	new_node = alloc_lattice_node(info, left_node, mw, i);
	push_node(info, new_node, position);

	/* 解の候補が多すぎたら、確率の低い方から削る */
	if (info->lattice_node_list[position].nr_nodes >= NODE_MAX_SIZE) {
	  remove_min_node(info, &info->lattice_node_list[position]);
	}
      }
    }
  }

  /* 文末補正 */
  for (node = info->lattice_node_list[to].head; node; node = node->next) {
    struct feature_list features;
    anthy_feature_list_init(&features);
    build_feature_list(NULL, &features);
    node->adjusted_probability = node->adjusted_probability *
      calc_probability(SEG_TAIL, &features);
    anthy_feature_list_free(&features);
  }
}
Пример #2
0
static double
get_transition_probability(struct lattice_node *node)
{
  struct feature_list features;
  double probability;

  /**/
  anthy_feature_list_init(&features);
  build_feature_list(node, &features);
  probability = calc_probability(node->seg_class, &features);
  anthy_feature_list_free(&features);

  /* 文節の形に対する評価 */
  probability *= get_form_bias(node->mw);
  return probability;
}
Пример #3
0
std::shared_ptr<AntColonyResult> AntColony::process()
{
	//initialize random number generator
	std::srand(static_cast<unsigned>(std::time(0)));

	const unsigned cityCount = get_count_city();
	cities_t shortestPath;

	//initiate pheramones to value from params
	Pheramone pheramone(cityCount, get_initial_pheromone());

	for (unsigned iteration = 0; iteration < get_count_iterations(); ++iteration)
	{
		std::vector<ant_t> v_ants(get_count_ants_in_iteration());

		// randomly place ants in cities
		place_ants_randomly(v_ants);

		// Ants should go throw all cities
		for (unsigned step = 0; step < cityCount; ++step)
		{
			for (auto & ant : v_ants)
			{
				auto prob = calc_probability(ant, pheramone);

				if (prob.size() == 0) continue;

				int next_city = realize_probability(ant, prob);

				// refresh an ant state
				ant.curState = next_city;
				ant.visited.push_back(ant.curState);
			}
		}

		// update pheramones
		update_pheramones(pheramone, v_ants);

		// find shortest path
		update_shortes_path(shortestPath, v_ants);
	}

	result->shortestPath = shortestPath;
	result->pathLen = calc_path_length(result->shortestPath);

	return	result;
}
Пример #4
0
static void
mw_eval(struct seg_ent *prev_seg, struct seg_ent *seg,
	struct meta_word *mw)
{
  int pc;
  struct feature_list fl;
  double prob;
  (void)seg;
  anthy_feature_list_init(&fl);
  /**/
  anthy_feature_list_set_cur_class(&fl, mw->seg_class);
  anthy_feature_list_set_dep_word(&fl, mw->dep_word_hash);
  anthy_feature_list_set_dep_class(&fl, mw->dep_class);
  anthy_feature_list_set_mw_features(&fl, mw->mw_features);
  /* 前の文節の素性 */
  if (prev_seg) {
    pc = prev_seg->best_seg_class;
  } else {
    pc = SEG_HEAD;
  }
  anthy_feature_list_set_class_trans(&fl, pc, mw->seg_class);
  anthy_feature_list_sort(&fl);
  /* 計算する */
  prob = 0.1 + calc_probability(&fl);
  if (prob < 0) {
    prob = (double)1 / (double)1000;
  }
  anthy_feature_list_free(&fl);
  mw->struct_score = RATIO_BASE * RATIO_BASE;
  mw->struct_score *= prob;
  /*
  anthy_feature_list_print(&fl);
  printf(" prob=%f, struct_score=%d\n", prob, mw->struct_score);
  */

  /**/
  if (mw->mw_features & MW_FEATURE_SUFFIX) {
    mw->struct_score /= 2;
  }
  if (mw->mw_features & MW_FEATURE_WEAK_CONN) {
    mw->struct_score /= 10;
  }
}