/* bigram-decoder.c generated by valac 0.56.19, the Vala compiler
 * generated from bigram-decoder.vala, do not modify */

/*
 * Copyright (C) 2012-2014 Daiki Ueno <ueno@gnu.org>
 * Copyright (C) 2012-2014 Red Hat, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib-object.h>
#include "libkkc/libkkc.h"
#include <glib.h>
#include <float.h>
#include <math.h>
#include <gobject/gvaluecollector.h>
#include <stdlib.h>
#include <string.h>
#include <gee.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define KKC_TYPE_NBEST_NODE (kkc_nbest_node_get_type ())
#define KKC_NBEST_NODE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), KKC_TYPE_NBEST_NODE, KkcNbestNode))
#define KKC_NBEST_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), KKC_TYPE_NBEST_NODE, KkcNbestNodeClass))
#define KKC_IS_NBEST_NODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), KKC_TYPE_NBEST_NODE))
#define KKC_IS_NBEST_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), KKC_TYPE_NBEST_NODE))
#define KKC_NBEST_NODE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), KKC_TYPE_NBEST_NODE, KkcNbestNodeClass))

typedef struct _KkcNbestNode KkcNbestNode;
typedef struct _KkcNbestNodeClass KkcNbestNodeClass;
typedef struct _KkcNbestNodePrivate KkcNbestNodePrivate;
#define _kkc_trellis_node_unref0(var) ((var == NULL) ? NULL : (var = (kkc_trellis_node_unref (var), NULL)))
#define _kkc_nbest_node_unref0(var) ((var == NULL) ? NULL : (var = (kkc_nbest_node_unref (var), NULL)))
typedef struct _KkcParamSpecNbestNode KkcParamSpecNbestNode;
enum  {
	KKC_BIGRAM_DECODER_0_PROPERTY,
	KKC_BIGRAM_DECODER_MODEL_PROPERTY,
	KKC_BIGRAM_DECODER_NUM_PROPERTIES
};
static GParamSpec* kkc_bigram_decoder_properties[KKC_BIGRAM_DECODER_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _kkc_language_model_entry_free0(var) ((var == NULL) ? NULL : (var = (kkc_language_model_entry_free (var), NULL)))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))
#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }
#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);

struct _KkcNbestNode {
	GTypeInstance parent_instance;
	volatile int ref_count;
	KkcNbestNodePrivate * priv;
	KkcTrellisNode* node;
	gdouble gn;
	gdouble fn;
	KkcNbestNode* next;
};

struct _KkcNbestNodeClass {
	GTypeClass parent_class;
	void (*finalize) (KkcNbestNode *self);
};

struct _KkcParamSpecNbestNode {
	GParamSpec parent_instance;
};

struct _KkcBigramDecoderPrivate {
	KkcBigramLanguageModel* _model;
};

static gpointer kkc_nbest_node_parent_class = NULL;
static gint KkcBigramDecoder_private_offset;
static gpointer kkc_bigram_decoder_parent_class = NULL;

VALA_EXTERN gpointer kkc_nbest_node_ref (gpointer instance);
VALA_EXTERN void kkc_nbest_node_unref (gpointer instance);
VALA_EXTERN GParamSpec* kkc_param_spec_nbest_node (const gchar* name,
                                       const gchar* nick,
                                       const gchar* blurb,
                                       GType object_type,
                                       GParamFlags flags);
VALA_EXTERN void kkc_value_set_nbest_node (GValue* value,
                               gpointer v_object);
VALA_EXTERN void kkc_value_take_nbest_node (GValue* value,
                                gpointer v_object);
VALA_EXTERN gpointer kkc_value_get_nbest_node (const GValue* value);
VALA_EXTERN GType kkc_nbest_node_get_type (void) G_GNUC_CONST ;
VALA_EXTERN KkcNbestNode* kkc_nbest_node_new (KkcTrellisNode* node);
VALA_EXTERN KkcNbestNode* kkc_nbest_node_construct (GType object_type,
                                        KkcTrellisNode* node);
static void kkc_nbest_node_finalize (KkcNbestNode * obj);
static GType kkc_nbest_node_get_type_once (void);
static KkcSegment** kkc_bigram_decoder_real_decode (KkcDecoder* base,
                                             const gchar* input,
                                             gint nbest,
                                             gint* constraint,
                                             gint constraint_length1,
                                             gint* result_length1);
static KkcSegment** kkc_bigram_decoder_real_decode_with_costs (KkcDecoder* base,
                                                        const gchar* input,
                                                        gint nbest,
                                                        gint* constraint,
                                                        gint constraint_length1,
                                                        gdouble max_distance,
                                                        gdouble min_path_cost,
                                                        gint* result_length1);
static gboolean kkc_bigram_decoder_check_overlaps (KkcBigramDecoder* self,
                                            gint* constraint,
                                            gint constraint_length1,
                                            gint i,
                                            gint j);
static gboolean kkc_bigram_decoder_check_constraint (KkcBigramDecoder* self,
                                              gint* constraint,
                                              gint constraint_length1,
                                              gint i,
                                              gint j);
static gdouble kkc_bigram_decoder_real_path_cost (KkcBigramDecoder* self,
                                           KkcTrellisNode* pnode,
                                           KkcTrellisNode* node,
                                           gint endpos);
static KkcSegment* kkc_bigram_decoder_nbest_node_to_segment (KkcBigramDecoder* self,
                                                      KkcNbestNode* nbest_node);
static gboolean _kkc_language_model_entry_equal (const KkcLanguageModelEntry * s1,
                                          const KkcLanguageModelEntry * s2);
static gint kkc_bigram_decoder_compare_nbest_node (KkcNbestNode* a,
                                            KkcNbestNode* b);
static gint _kkc_bigram_decoder_compare_nbest_node_gcompare_data_func (gconstpointer a,
                                                                gconstpointer b,
                                                                gpointer self);
static gchar* kkc_bigram_decoder_concat_nbest_node_outputs (KkcBigramDecoder* self,
                                                     KkcNbestNode* nbest_node);
static void kkc_bigram_decoder_finalize (GObject * obj);
static GType kkc_bigram_decoder_get_type_once (void);
static void _vala_kkc_bigram_decoder_get_property (GObject * object,
                                            guint property_id,
                                            GValue * value,
                                            GParamSpec * pspec);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);

static gpointer
_kkc_trellis_node_ref0 (gpointer self)
{
	return self ? kkc_trellis_node_ref (self) : NULL;
}

KkcNbestNode*
kkc_nbest_node_construct (GType object_type,
                          KkcTrellisNode* node)
{
	KkcNbestNode* self = NULL;
	KkcTrellisNode* _tmp0_;
	g_return_val_if_fail (node != NULL, NULL);
	self = (KkcNbestNode*) g_type_create_instance (object_type);
	_tmp0_ = _kkc_trellis_node_ref0 (node);
	_kkc_trellis_node_unref0 (self->node);
	self->node = _tmp0_;
	return self;
}

KkcNbestNode*
kkc_nbest_node_new (KkcTrellisNode* node)
{
	return kkc_nbest_node_construct (KKC_TYPE_NBEST_NODE, node);
}

static void
kkc_value_nbest_node_init (GValue* value)
{
	value->data[0].v_pointer = NULL;
}

static void
kkc_value_nbest_node_free_value (GValue* value)
{
	if (value->data[0].v_pointer) {
		kkc_nbest_node_unref (value->data[0].v_pointer);
	}
}

static void
kkc_value_nbest_node_copy_value (const GValue* src_value,
                                 GValue* dest_value)
{
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = kkc_nbest_node_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}

static gpointer
kkc_value_nbest_node_peek_pointer (const GValue* value)
{
	return value->data[0].v_pointer;
}

static gchar*
kkc_value_nbest_node_collect_value (GValue* value,
                                    guint n_collect_values,
                                    GTypeCValue* collect_values,
                                    guint collect_flags)
{
	if (collect_values[0].v_pointer) {
		KkcNbestNode * object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = kkc_nbest_node_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}

static gchar*
kkc_value_nbest_node_lcopy_value (const GValue* value,
                                  guint n_collect_values,
                                  GTypeCValue* collect_values,
                                  guint collect_flags)
{
	KkcNbestNode ** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = kkc_nbest_node_ref (value->data[0].v_pointer);
	}
	return NULL;
}

GParamSpec*
kkc_param_spec_nbest_node (const gchar* name,
                           const gchar* nick,
                           const gchar* blurb,
                           GType object_type,
                           GParamFlags flags)
{
	KkcParamSpecNbestNode* spec;
	g_return_val_if_fail (g_type_is_a (object_type, KKC_TYPE_NBEST_NODE), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}

gpointer
kkc_value_get_nbest_node (const GValue* value)
{
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, KKC_TYPE_NBEST_NODE), NULL);
	return value->data[0].v_pointer;
}

void
kkc_value_set_nbest_node (GValue* value,
                          gpointer v_object)
{
	KkcNbestNode * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, KKC_TYPE_NBEST_NODE));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, KKC_TYPE_NBEST_NODE));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		kkc_nbest_node_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		kkc_nbest_node_unref (old);
	}
}

void
kkc_value_take_nbest_node (GValue* value,
                           gpointer v_object)
{
	KkcNbestNode * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, KKC_TYPE_NBEST_NODE));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, KKC_TYPE_NBEST_NODE));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		kkc_nbest_node_unref (old);
	}
}

static void
kkc_nbest_node_class_init (KkcNbestNodeClass * klass,
                           gpointer klass_data)
{
	kkc_nbest_node_parent_class = g_type_class_peek_parent (klass);
	((KkcNbestNodeClass *) klass)->finalize = kkc_nbest_node_finalize;
}

static void
kkc_nbest_node_instance_init (KkcNbestNode * self,
                              gpointer klass)
{
	self->gn = 0.0;
	self->fn = DBL_MAX;
	self->ref_count = 1;
}

static void
kkc_nbest_node_finalize (KkcNbestNode * obj)
{
	KkcNbestNode * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, KKC_TYPE_NBEST_NODE, KkcNbestNode);
	g_signal_handlers_destroy (self);
	_kkc_trellis_node_unref0 (self->node);
	_kkc_nbest_node_unref0 (self->next);
}

static GType
kkc_nbest_node_get_type_once (void)
{
	static const GTypeValueTable g_define_type_value_table = { kkc_value_nbest_node_init, kkc_value_nbest_node_free_value, kkc_value_nbest_node_copy_value, kkc_value_nbest_node_peek_pointer, "p", kkc_value_nbest_node_collect_value, "p", kkc_value_nbest_node_lcopy_value };
	static const GTypeInfo g_define_type_info = { sizeof (KkcNbestNodeClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) kkc_nbest_node_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (KkcNbestNode), 0, (GInstanceInitFunc) kkc_nbest_node_instance_init, &g_define_type_value_table };
	static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
	GType kkc_nbest_node_type_id;
	kkc_nbest_node_type_id = g_type_register_fundamental (g_type_fundamental_next (), "KkcNbestNode", &g_define_type_info, &g_define_type_fundamental_info, 0);
	return kkc_nbest_node_type_id;
}

GType
kkc_nbest_node_get_type (void)
{
	static volatile gsize kkc_nbest_node_type_id__once = 0;
	if (g_once_init_enter (&kkc_nbest_node_type_id__once)) {
		GType kkc_nbest_node_type_id;
		kkc_nbest_node_type_id = kkc_nbest_node_get_type_once ();
		g_once_init_leave (&kkc_nbest_node_type_id__once, kkc_nbest_node_type_id);
	}
	return kkc_nbest_node_type_id__once;
}

gpointer
kkc_nbest_node_ref (gpointer instance)
{
	KkcNbestNode * self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}

void
kkc_nbest_node_unref (gpointer instance)
{
	KkcNbestNode * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		KKC_NBEST_NODE_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

static inline gpointer
kkc_bigram_decoder_get_instance_private (KkcBigramDecoder* self)
{
	return G_STRUCT_MEMBER_P (self, KkcBigramDecoder_private_offset);
}

static KkcSegment**
kkc_bigram_decoder_real_decode (KkcDecoder* base,
                                const gchar* input,
                                gint nbest,
                                gint* constraint,
                                gint constraint_length1,
                                gint* result_length1)
{
	KkcBigramDecoder * self;
	gint _tmp0_ = 0;
	KkcSegment** _tmp1_;
	KkcSegment** _tmp2_;
	gint _tmp2__length1;
	KkcSegment** result;
	self = (KkcBigramDecoder*) base;
	g_return_val_if_fail (input != NULL, NULL);
	_tmp1_ = kkc_decoder_decode_with_costs ((KkcDecoder*) self, input, nbest, constraint, (gint) constraint_length1, DBL_MAX, -DBL_MAX, &_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp2__length1 = _tmp0_;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	return result;
}

static KkcSegment**
kkc_bigram_decoder_real_decode_with_costs (KkcDecoder* base,
                                           const gchar* input,
                                           gint nbest,
                                           gint* constraint,
                                           gint constraint_length1,
                                           gdouble max_distance,
                                           gdouble min_path_cost,
                                           gint* result_length1)
{
	KkcBigramDecoder * self;
	GeeArrayList** trellis = NULL;
	gint _tmp0_ = 0;
	GeeArrayList** _tmp1_;
	gint trellis_length1;
	gint _trellis_size_;
	GeeArrayList** _tmp2_;
	gint _tmp2__length1;
	GeeArrayList** _tmp3_;
	gint _tmp3__length1;
	KkcSegment** segments = NULL;
	GeeArrayList** _tmp4_;
	gint _tmp4__length1;
	gint _tmp5_ = 0;
	KkcSegment** _tmp6_;
	gint segments_length1;
	gint _segments_size_;
	KkcSegment** _tmp12_;
	gint _tmp12__length1;
	KkcSegment** result;
	self = (KkcBigramDecoder*) base;
	g_return_val_if_fail (input != NULL, NULL);
	_tmp1_ = kkc_bigram_decoder_build_trellis (self, input, constraint, (gint) constraint_length1, &_tmp0_);
	trellis = _tmp1_;
	trellis_length1 = _tmp0_;
	_trellis_size_ = trellis_length1;
	_tmp2_ = trellis;
	_tmp2__length1 = trellis_length1;
	kkc_bigram_decoder_add_unknown_nodes (self, _tmp2_, (gint) _tmp2__length1, input, constraint, (gint) constraint_length1);
	_tmp3_ = trellis;
	_tmp3__length1 = trellis_length1;
	kkc_bigram_decoder_forward_search (self, _tmp3_, (gint) _tmp3__length1, input);
	_tmp4_ = trellis;
	_tmp4__length1 = trellis_length1;
	_tmp6_ = kkc_bigram_decoder_backward_search (self, _tmp4_, (gint) _tmp4__length1, nbest, max_distance, min_path_cost, &_tmp5_);
	segments = _tmp6_;
	segments_length1 = _tmp5_;
	_segments_size_ = segments_length1;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp7_ = FALSE;
			_tmp7_ = TRUE;
			while (TRUE) {
				GeeArrayList** _tmp9_;
				gint _tmp9__length1;
				GeeArrayList** _tmp10_;
				gint _tmp10__length1;
				GeeArrayList* _tmp11_;
				if (!_tmp7_) {
					gint _tmp8_;
					_tmp8_ = i;
					i = _tmp8_ + 1;
				}
				_tmp7_ = FALSE;
				_tmp9_ = trellis;
				_tmp9__length1 = trellis_length1;
				if (!(i < _tmp9__length1)) {
					break;
				}
				_tmp10_ = trellis;
				_tmp10__length1 = trellis_length1;
				_tmp11_ = _tmp10_[i];
				gee_abstract_collection_clear ((GeeAbstractCollection*) _tmp11_);
			}
		}
	}
	_tmp12_ = segments;
	_tmp12__length1 = segments_length1;
	if (result_length1) {
		*result_length1 = _tmp12__length1;
	}
	result = _tmp12_;
	trellis = (_vala_array_free (trellis, trellis_length1, (GDestroyNotify) g_object_unref), NULL);
	return result;
}

static gint
string_index_of_nth_char (const gchar* self,
                          glong c)
{
	gchar* _tmp0_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	_tmp0_ = g_utf8_offset_to_pointer (self, c);
	result = (gint) (_tmp0_ - ((gchar*) self));
	return result;
}

static glong
string_strnlen (gchar* str,
                glong maxlen)
{
	gchar* end = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	glong result;
	_tmp0_ = memchr (str, 0, (gsize) maxlen);
	end = _tmp0_;
	_tmp1_ = end;
	if (_tmp1_ == NULL) {
		result = maxlen;
		return result;
	} else {
		gchar* _tmp2_;
		_tmp2_ = end;
		result = (glong) (_tmp2_ - str);
		return result;
	}
}

static gchar*
string_substring (const gchar* self,
                  glong offset,
                  glong len)
{
	glong string_length = 0L;
	gboolean _tmp0_ = FALSE;
	gchar* _tmp3_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	if (offset >= ((glong) 0)) {
		_tmp0_ = len >= ((glong) 0);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		string_length = string_strnlen ((gchar*) self, offset + len);
	} else {
		gint _tmp1_;
		gint _tmp2_;
		_tmp1_ = strlen (self);
		_tmp2_ = _tmp1_;
		string_length = (glong) _tmp2_;
	}
	if (offset < ((glong) 0)) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= ((glong) 0), NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < ((glong) 0)) {
		len = string_length - offset;
	}
	g_return_val_if_fail ((offset + len) <= string_length, NULL);
	_tmp3_ = g_strndup (((gchar*) self) + offset, (gsize) len);
	result = _tmp3_;
	return result;
}

void
kkc_bigram_decoder_add_unknown_nodes (KkcBigramDecoder* self,
                                      GeeArrayList** trellis,
                                      gint trellis_length1,
                                      const gchar* input,
                                      gint* constraint,
                                      gint constraint_length1)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (input != NULL);
	{
		gint i = 0;
		i = 1;
		{
			gboolean _tmp0_ = FALSE;
			_tmp0_ = TRUE;
			while (TRUE) {
				if (!_tmp0_) {
					gint _tmp1_;
					_tmp1_ = i;
					i = _tmp1_ + 1;
				}
				_tmp0_ = FALSE;
				if (!(i < trellis_length1)) {
					break;
				}
				{
					gint j = 0;
					j = i;
					{
						gboolean _tmp2_ = FALSE;
						_tmp2_ = TRUE;
						while (TRUE) {
							gboolean _tmp4_ = FALSE;
							glong offset = 0L;
							glong length = 0L;
							gchar* _input = NULL;
							gchar* _tmp8_;
							KkcLanguageModelEntry entry = {0};
							const gchar* _tmp9_;
							gchar* _tmp10_;
							const gchar* _tmp11_;
							gchar* _tmp12_;
							KkcLanguageModelEntry _tmp13_ = {0};
							KkcUnigramTrellisNode* node = NULL;
							KkcLanguageModelEntry _tmp14_;
							KkcUnigramTrellisNode* _tmp15_;
							GeeArrayList* _tmp16_;
							KkcUnigramTrellisNode* _tmp17_;
							if (!_tmp2_) {
								gint _tmp3_;
								_tmp3_ = j;
								j = _tmp3_ + 1;
							}
							_tmp2_ = FALSE;
							if (j < trellis_length1) {
								GeeArrayList* _tmp5_;
								gboolean _tmp6_;
								gboolean _tmp7_;
								_tmp5_ = trellis[j];
								_tmp6_ = gee_collection_get_is_empty ((GeeCollection*) _tmp5_);
								_tmp7_ = _tmp6_;
								_tmp4_ = _tmp7_;
							} else {
								_tmp4_ = FALSE;
							}
							if (!_tmp4_) {
								break;
							}
							if (!kkc_bigram_decoder_check_overlaps (self, constraint, (gint) constraint_length1, i, j)) {
								continue;
							}
							offset = (glong) string_index_of_nth_char (input, (glong) (i - 1));
							length = string_index_of_nth_char (input, (glong) j) - offset;
							_tmp8_ = string_substring (input, offset, length);
							_input = _tmp8_;
							_tmp9_ = _input;
							_tmp10_ = g_strdup (_tmp9_);
							_tmp11_ = _input;
							_tmp12_ = g_strdup (_tmp11_);
							_g_free0 (_tmp13_.input);
							_tmp13_.input = _tmp10_;
							_g_free0 (_tmp13_.output);
							_tmp13_.output = _tmp12_;
							_tmp13_.id = (guint) 2;
							entry = _tmp13_;
							_tmp14_ = entry;
							_tmp15_ = kkc_unigram_trellis_node_new (&_tmp14_, (guint) j);
							node = _tmp15_;
							_tmp16_ = trellis[j];
							_tmp17_ = node;
							gee_abstract_collection_add ((GeeAbstractCollection*) _tmp16_, (KkcTrellisNode*) _tmp17_);
							_kkc_trellis_node_unref0 (node);
							kkc_language_model_entry_destroy (&entry);
							_g_free0 (_input);
						}
					}
				}
			}
		}
	}
}

GeeArrayList**
kkc_bigram_decoder_build_trellis (KkcBigramDecoder* self,
                                  const gchar* input,
                                  gint* constraint,
                                  gint constraint_length1,
                                  gint* result_length1)
{
	gint length = 0;
	GeeArrayList** trellis = NULL;
	GeeArrayList** _tmp0_;
	gint trellis_length1;
	gint _trellis_size_;
	KkcUnigramTrellisNode* bos_node = NULL;
	KkcBigramLanguageModel* _tmp6_;
	KkcBigramLanguageModel* _tmp7_;
	KkcLanguageModelEntry _tmp8_ = {0};
	KkcLanguageModelEntry _tmp9_;
	KkcUnigramTrellisNode* _tmp10_;
	GeeArrayList** _tmp11_;
	gint _tmp11__length1;
	GeeArrayList* _tmp12_;
	KkcUnigramTrellisNode* _tmp13_;
	KkcUnigramTrellisNode* eos_node = NULL;
	KkcBigramLanguageModel* _tmp14_;
	KkcBigramLanguageModel* _tmp15_;
	KkcLanguageModelEntry _tmp16_ = {0};
	KkcLanguageModelEntry _tmp17_;
	KkcUnigramTrellisNode* _tmp18_;
	GeeArrayList** _tmp19_;
	gint _tmp19__length1;
	GeeArrayList* _tmp20_;
	KkcUnigramTrellisNode* _tmp21_;
	GeeArrayList** _tmp42_;
	gint _tmp42__length1;
	GeeArrayList** result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (input != NULL, NULL);
	length = g_utf8_strlen (input, (gssize) -1);
	_tmp0_ = g_new0 (GeeArrayList*, (length + 2) + 1);
	trellis = _tmp0_;
	trellis_length1 = length + 2;
	_trellis_size_ = trellis_length1;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp1_ = FALSE;
			_tmp1_ = TRUE;
			while (TRUE) {
				GeeArrayList** _tmp3_;
				gint _tmp3__length1;
				GeeArrayList** _tmp4_;
				gint _tmp4__length1;
				GeeArrayList* _tmp5_;
				if (!_tmp1_) {
					gint _tmp2_;
					_tmp2_ = i;
					i = _tmp2_ + 1;
				}
				_tmp1_ = FALSE;
				_tmp3_ = trellis;
				_tmp3__length1 = trellis_length1;
				if (!(i < _tmp3__length1)) {
					break;
				}
				_tmp4_ = trellis;
				_tmp4__length1 = trellis_length1;
				_tmp5_ = gee_array_list_new (KKC_TYPE_TRELLIS_NODE, (GBoxedCopyFunc) kkc_trellis_node_ref, (GDestroyNotify) kkc_trellis_node_unref, NULL, NULL, NULL);
				_g_object_unref0 (_tmp4_[i]);
				_tmp4_[i] = _tmp5_;
			}
		}
	}
	_tmp6_ = kkc_bigram_decoder_get_model (self);
	_tmp7_ = _tmp6_;
	kkc_language_model_get_bos ((KkcLanguageModel*) _tmp7_, &_tmp8_);
	_tmp9_ = _tmp8_;
	_tmp10_ = kkc_unigram_trellis_node_new (&_tmp9_, (guint) 1);
	bos_node = _tmp10_;
	_tmp11_ = trellis;
	_tmp11__length1 = trellis_length1;
	_tmp12_ = _tmp11_[0];
	_tmp13_ = bos_node;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp12_, (KkcTrellisNode*) _tmp13_);
	_tmp14_ = kkc_bigram_decoder_get_model (self);
	_tmp15_ = _tmp14_;
	kkc_language_model_get_eos ((KkcLanguageModel*) _tmp15_, &_tmp16_);
	_tmp17_ = _tmp16_;
	_tmp18_ = kkc_unigram_trellis_node_new (&_tmp17_, (guint) (length + 1));
	eos_node = _tmp18_;
	_tmp19_ = trellis;
	_tmp19__length1 = trellis_length1;
	_tmp20_ = _tmp19_[length + 1];
	_tmp21_ = eos_node;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp20_, (KkcTrellisNode*) _tmp21_);
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp22_ = FALSE;
			_tmp22_ = TRUE;
			while (TRUE) {
				glong byte_offset = 0L;
				gchar* _input = NULL;
				gchar* _tmp24_;
				GeeCollection* entries = NULL;
				KkcBigramLanguageModel* _tmp25_;
				KkcBigramLanguageModel* _tmp26_;
				const gchar* _tmp27_;
				GeeCollection* _tmp28_;
				if (!_tmp22_) {
					gint _tmp23_;
					_tmp23_ = i;
					i = _tmp23_ + 1;
				}
				_tmp22_ = FALSE;
				if (!(i < length)) {
					break;
				}
				byte_offset = (glong) string_index_of_nth_char (input, (glong) i);
				_tmp24_ = string_substring (input, byte_offset, (glong) -1);
				_input = _tmp24_;
				_tmp25_ = kkc_bigram_decoder_get_model (self);
				_tmp26_ = _tmp25_;
				_tmp27_ = _input;
				_tmp28_ = kkc_language_model_entries ((KkcLanguageModel*) _tmp26_, _tmp27_);
				entries = _tmp28_;
				{
					GeeIterator* _entry_it = NULL;
					GeeCollection* _tmp29_;
					GeeIterator* _tmp30_;
					_tmp29_ = entries;
					_tmp30_ = gee_iterable_iterator ((GeeIterable*) _tmp29_);
					_entry_it = _tmp30_;
					while (TRUE) {
						GeeIterator* _tmp31_;
						KkcLanguageModelEntry* entry = NULL;
						GeeIterator* _tmp32_;
						gpointer _tmp33_;
						gint j = 0;
						KkcLanguageModelEntry* _tmp34_;
						const gchar* _tmp35_;
						KkcUnigramTrellisNode* node = NULL;
						KkcLanguageModelEntry* _tmp36_;
						KkcLanguageModelEntry _tmp37_;
						KkcUnigramTrellisNode* _tmp38_;
						GeeArrayList** _tmp39_;
						gint _tmp39__length1;
						GeeArrayList* _tmp40_;
						KkcUnigramTrellisNode* _tmp41_;
						_tmp31_ = _entry_it;
						if (!gee_iterator_next (_tmp31_)) {
							break;
						}
						_tmp32_ = _entry_it;
						_tmp33_ = gee_iterator_get (_tmp32_);
						entry = (KkcLanguageModelEntry*) _tmp33_;
						_tmp34_ = entry;
						_tmp35_ = (*_tmp34_).input;
						j = i + g_utf8_strlen (_tmp35_, (gssize) -1);
						if (!kkc_bigram_decoder_check_constraint (self, constraint, (gint) constraint_length1, i, j)) {
							_kkc_language_model_entry_free0 (entry);
							continue;
						}
						_tmp36_ = entry;
						_tmp37_ = *_tmp36_;
						_tmp38_ = kkc_unigram_trellis_node_new (&_tmp37_, (guint) j);
						node = _tmp38_;
						_tmp39_ = trellis;
						_tmp39__length1 = trellis_length1;
						_tmp40_ = _tmp39_[j];
						_tmp41_ = node;
						gee_abstract_collection_add ((GeeAbstractCollection*) _tmp40_, (KkcTrellisNode*) _tmp41_);
						_kkc_trellis_node_unref0 (node);
						_kkc_language_model_entry_free0 (entry);
					}
					_g_object_unref0 (_entry_it);
				}
				_g_object_unref0 (entries);
				_g_free0 (_input);
			}
		}
	}
	_tmp42_ = trellis;
	_tmp42__length1 = trellis_length1;
	if (result_length1) {
		*result_length1 = _tmp42__length1;
	}
	result = _tmp42_;
	_kkc_trellis_node_unref0 (eos_node);
	_kkc_trellis_node_unref0 (bos_node);
	return result;
}

static gboolean
kkc_bigram_decoder_check_constraint (KkcBigramDecoder* self,
                                     gint* constraint,
                                     gint constraint_length1,
                                     gint i,
                                     gint j)
{
	gint last_c = 0;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	last_c = 0;
	{
		gint* c_collection = NULL;
		gint c_collection_length1 = 0;
		gint _c_collection_size_ = 0;
		gint c_it = 0;
		c_collection = constraint;
		c_collection_length1 = constraint_length1;
		for (c_it = 0; c_it < c_collection_length1; c_it = c_it + 1) {
			gint c = 0;
			c = c_collection[c_it];
			{
				gboolean _tmp0_ = FALSE;
				if (i == last_c) {
					_tmp0_ = j == c;
				} else {
					_tmp0_ = FALSE;
				}
				if (_tmp0_) {
					result = TRUE;
					return result;
				}
				last_c = c;
			}
		}
	}
	result = i >= last_c;
	return result;
}

static gboolean
kkc_bigram_decoder_check_overlaps (KkcBigramDecoder* self,
                                   gint* constraint,
                                   gint constraint_length1,
                                   gint i,
                                   gint j)
{
	gint last_c = 0;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	last_c = 0;
	{
		gint* c_collection = NULL;
		gint c_collection_length1 = 0;
		gint _c_collection_size_ = 0;
		gint c_it = 0;
		c_collection = constraint;
		c_collection_length1 = constraint_length1;
		for (c_it = 0; c_it < c_collection_length1; c_it = c_it + 1) {
			gint c = 0;
			c = c_collection[c_it];
			{
				gboolean _tmp0_ = FALSE;
				gboolean _tmp1_ = FALSE;
				if (last_c <= i) {
					_tmp1_ = i <= c;
				} else {
					_tmp1_ = FALSE;
				}
				if (_tmp1_) {
					gboolean _tmp2_ = FALSE;
					if (last_c <= j) {
						_tmp2_ = j <= c;
					} else {
						_tmp2_ = FALSE;
					}
					_tmp0_ = _tmp2_;
				} else {
					_tmp0_ = FALSE;
				}
				if (_tmp0_) {
					result = TRUE;
					return result;
				}
				last_c = c;
			}
		}
	}
	result = i >= last_c;
	return result;
}

void
kkc_bigram_decoder_forward_search (KkcBigramDecoder* self,
                                   GeeArrayList** trellis,
                                   gint trellis_length1,
                                   const gchar* input)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (input != NULL);
	{
		gint i = 0;
		i = 1;
		{
			gboolean _tmp0_ = FALSE;
			_tmp0_ = TRUE;
			while (TRUE) {
				if (!_tmp0_) {
					gint _tmp1_;
					_tmp1_ = i;
					i = _tmp1_ + 1;
				}
				_tmp0_ = FALSE;
				if (!(i < trellis_length1)) {
					break;
				}
				{
					GeeArrayList* _node_list = NULL;
					GeeArrayList* _tmp2_;
					gint _node_size = 0;
					GeeArrayList* _tmp3_;
					gint _tmp4_;
					gint _tmp5_;
					gint _node_index = 0;
					_tmp2_ = trellis[i];
					_node_list = _tmp2_;
					_tmp3_ = _node_list;
					_tmp4_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp3_);
					_tmp5_ = _tmp4_;
					_node_size = _tmp5_;
					_node_index = -1;
					while (TRUE) {
						gint _tmp6_;
						gint _tmp7_;
						KkcTrellisNode* node = NULL;
						GeeArrayList* _tmp8_;
						gpointer _tmp9_;
						gint j = 0;
						KkcTrellisNode* _tmp10_;
						guint _tmp11_;
						guint _tmp12_;
						gdouble max_cost = 0.0;
						KkcTrellisNode* max_pnode = NULL;
						KkcTrellisNode* _tmp26_;
						KkcTrellisNode* _tmp29_;
						KkcTrellisNode* _tmp30_;
						KkcTrellisNode* _tmp31_;
						KkcTrellisNode* _tmp32_;
						_node_index = _node_index + 1;
						_tmp6_ = _node_index;
						_tmp7_ = _node_size;
						if (!(_tmp6_ < _tmp7_)) {
							break;
						}
						_tmp8_ = _node_list;
						_tmp9_ = gee_abstract_list_get ((GeeAbstractList*) _tmp8_, _node_index);
						node = (KkcTrellisNode*) _tmp9_;
						_tmp10_ = node;
						_tmp11_ = kkc_trellis_node_get_length (_tmp10_);
						_tmp12_ = _tmp11_;
						j = i - ((gint) _tmp12_);
						if (j < 0) {
							_kkc_trellis_node_unref0 (node);
							continue;
						}
						max_cost = -DBL_MAX;
						max_pnode = NULL;
						{
							GeeArrayList* _pnode_list = NULL;
							GeeArrayList* _tmp13_;
							gint _pnode_size = 0;
							GeeArrayList* _tmp14_;
							gint _tmp15_;
							gint _tmp16_;
							gint _pnode_index = 0;
							_tmp13_ = trellis[j];
							_pnode_list = _tmp13_;
							_tmp14_ = _pnode_list;
							_tmp15_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp14_);
							_tmp16_ = _tmp15_;
							_pnode_size = _tmp16_;
							_pnode_index = -1;
							while (TRUE) {
								gint _tmp17_;
								gint _tmp18_;
								KkcTrellisNode* pnode = NULL;
								GeeArrayList* _tmp19_;
								gpointer _tmp20_;
								gdouble cost = 0.0;
								KkcTrellisNode* _tmp21_;
								KkcTrellisNode* _tmp22_;
								KkcTrellisNode* _tmp23_;
								_pnode_index = _pnode_index + 1;
								_tmp17_ = _pnode_index;
								_tmp18_ = _pnode_size;
								if (!(_tmp17_ < _tmp18_)) {
									break;
								}
								_tmp19_ = _pnode_list;
								_tmp20_ = gee_abstract_list_get ((GeeAbstractList*) _tmp19_, _pnode_index);
								pnode = (KkcTrellisNode*) _tmp20_;
								_tmp21_ = pnode;
								_tmp22_ = pnode;
								_tmp23_ = node;
								cost = _tmp21_->cumulative_cost + kkc_bigram_decoder_path_cost (self, _tmp22_, _tmp23_, j);
								if (cost > max_cost) {
									KkcTrellisNode* _tmp24_;
									KkcTrellisNode* _tmp25_;
									_tmp24_ = pnode;
									_tmp25_ = _kkc_trellis_node_ref0 (_tmp24_);
									_kkc_trellis_node_unref0 (max_pnode);
									max_pnode = _tmp25_;
									max_cost = cost;
								}
								_kkc_trellis_node_unref0 (pnode);
							}
						}
						_tmp26_ = max_pnode;
						if (_tmp26_ == NULL) {
							GeeArrayList* _tmp27_;
							gpointer _tmp28_;
							_tmp27_ = trellis[i];
							_tmp28_ = gee_abstract_list_get ((GeeAbstractList*) _tmp27_, 0);
							_kkc_trellis_node_unref0 (max_pnode);
							max_pnode = (KkcTrellisNode*) _tmp28_;
						}
						_tmp29_ = node;
						_tmp29_->cumulative_cost = max_cost;
						_tmp30_ = node;
						_tmp31_ = max_pnode;
						_tmp32_ = _kkc_trellis_node_ref0 (_tmp31_);
						_kkc_trellis_node_unref0 (_tmp30_->previous);
						_tmp30_->previous = _tmp32_;
						_kkc_trellis_node_unref0 (max_pnode);
						_kkc_trellis_node_unref0 (node);
					}
				}
			}
		}
	}
}

static gdouble
kkc_bigram_decoder_real_path_cost (KkcBigramDecoder* self,
                                   KkcTrellisNode* pnode,
                                   KkcTrellisNode* node,
                                   gint endpos)
{
	KkcUnigramTrellisNode* upnode = NULL;
	KkcUnigramTrellisNode* _tmp0_;
	KkcUnigramTrellisNode* unode = NULL;
	KkcUnigramTrellisNode* _tmp1_;
	gboolean _tmp2_ = FALSE;
	KkcUnigramTrellisNode* _tmp3_;
	KkcBigramLanguageModel* _tmp5_;
	KkcBigramLanguageModel* _tmp6_;
	KkcUnigramTrellisNode* _tmp7_;
	KkcLanguageModelEntry _tmp8_ = {0};
	KkcLanguageModelEntry _tmp9_;
	KkcUnigramTrellisNode* _tmp10_;
	KkcLanguageModelEntry _tmp11_ = {0};
	KkcLanguageModelEntry _tmp12_;
	gdouble result;
	g_return_val_if_fail (pnode != NULL, -1.0);
	g_return_val_if_fail (node != NULL, -1.0);
	_tmp0_ = _kkc_trellis_node_ref0 (KKC_IS_UNIGRAM_TRELLIS_NODE (pnode) ? ((KkcUnigramTrellisNode*) pnode) : NULL);
	upnode = _tmp0_;
	_tmp1_ = _kkc_trellis_node_ref0 (KKC_IS_UNIGRAM_TRELLIS_NODE (node) ? ((KkcUnigramTrellisNode*) node) : NULL);
	unode = _tmp1_;
	_tmp3_ = upnode;
	if (_tmp3_ != NULL) {
		KkcUnigramTrellisNode* _tmp4_;
		_tmp4_ = unode;
		_tmp2_ = _tmp4_ != NULL;
	} else {
		_tmp2_ = FALSE;
	}
	_vala_assert (_tmp2_, "upnode != null && unode != null");
	_tmp5_ = kkc_bigram_decoder_get_model (self);
	_tmp6_ = _tmp5_;
	_tmp7_ = upnode;
	kkc_unigram_trellis_node_get_entry (_tmp7_, &_tmp8_);
	_tmp9_ = _tmp8_;
	_tmp10_ = unode;
	kkc_unigram_trellis_node_get_entry (_tmp10_, &_tmp11_);
	_tmp12_ = _tmp11_;
	result = kkc_bigram_language_model_bigram_backoff_cost (_tmp6_, &_tmp9_, &_tmp12_);
	_kkc_trellis_node_unref0 (unode);
	_kkc_trellis_node_unref0 (upnode);
	return result;
}

gdouble
kkc_bigram_decoder_path_cost (KkcBigramDecoder* self,
                              KkcTrellisNode* pnode,
                              KkcTrellisNode* node,
                              gint endpos)
{
	KkcBigramDecoderClass* _klass_;
	g_return_val_if_fail (self != NULL, -1.0);
	_klass_ = KKC_BIGRAM_DECODER_GET_CLASS (self);
	if (_klass_->path_cost) {
		return _klass_->path_cost (self, pnode, node, endpos);
	}
	return -1.0;
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static KkcSegment*
kkc_bigram_decoder_nbest_node_to_segment (KkcBigramDecoder* self,
                                          KkcNbestNode* nbest_node)
{
	KkcSegment* start_segment = NULL;
	KkcSegment* previous_segment = NULL;
	KkcSegment* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (nbest_node != NULL, NULL);
	start_segment = NULL;
	previous_segment = NULL;
	{
		gboolean _tmp0_ = FALSE;
		_tmp0_ = TRUE;
		while (TRUE) {
			KkcNbestNode* _tmp2_;
			if (!_tmp0_) {
				KkcNbestNode* _tmp1_;
				_tmp1_ = nbest_node->next;
				nbest_node = _tmp1_;
			}
			_tmp0_ = FALSE;
			_tmp2_ = nbest_node->next;
			if (!(_tmp2_ != NULL)) {
				break;
			}
			{
				gint i = 0;
				i = 0;
				{
					gboolean _tmp3_ = FALSE;
					_tmp3_ = TRUE;
					while (TRUE) {
						KkcTrellisNode* _tmp5_;
						KkcLanguageModelEntry* _tmp6_;
						gint _tmp6__length1;
						gint _tmp7_ = 0;
						KkcLanguageModelEntry* _tmp8_;
						gint _tmp8__length1;
						KkcLanguageModelEntry entry = {0};
						KkcTrellisNode* _tmp9_;
						KkcLanguageModelEntry* _tmp10_;
						gint _tmp10__length1;
						gint _tmp11_ = 0;
						KkcLanguageModelEntry* _tmp12_;
						gint _tmp12__length1;
						KkcLanguageModelEntry _tmp13_;
						KkcLanguageModelEntry _tmp14_;
						KkcLanguageModelEntry _tmp15_ = {0};
						KkcSegment* segment = NULL;
						KkcLanguageModelEntry _tmp16_;
						const gchar* _tmp17_;
						KkcLanguageModelEntry _tmp18_;
						const gchar* _tmp19_;
						KkcSegment* _tmp20_;
						KkcSegment* _tmp21_;
						KkcSegment* _tmp24_;
						KkcSegment* _tmp28_;
						KkcSegment* _tmp29_;
						if (!_tmp3_) {
							gint _tmp4_;
							_tmp4_ = i;
							i = _tmp4_ + 1;
						}
						_tmp3_ = FALSE;
						_tmp5_ = nbest_node->node;
						_tmp6_ = kkc_trellis_node_get_entries (_tmp5_, &_tmp7_);
						_tmp6__length1 = _tmp7_;
						_tmp8_ = _tmp6_;
						_tmp8__length1 = _tmp6__length1;
						if (!(i < _tmp8__length1)) {
							break;
						}
						_tmp9_ = nbest_node->node;
						_tmp10_ = kkc_trellis_node_get_entries (_tmp9_, &_tmp11_);
						_tmp10__length1 = _tmp11_;
						_tmp12_ = _tmp10_;
						_tmp12__length1 = _tmp10__length1;
						_tmp13_ = _tmp12_[i];
						_tmp14_ = _tmp13_;
						kkc_language_model_entry_copy (&_tmp14_, &_tmp15_);
						entry = _tmp15_;
						_tmp16_ = entry;
						_tmp17_ = _tmp16_.input;
						_tmp18_ = entry;
						_tmp19_ = _tmp18_.output;
						_tmp20_ = kkc_segment_new (_tmp17_, _tmp19_);
						segment = _tmp20_;
						_tmp21_ = start_segment;
						if (_tmp21_ == NULL) {
							KkcSegment* _tmp22_;
							KkcSegment* _tmp23_;
							_tmp22_ = segment;
							_tmp23_ = _g_object_ref0 (_tmp22_);
							_g_object_unref0 (start_segment);
							start_segment = _tmp23_;
						}
						_tmp24_ = previous_segment;
						if (_tmp24_ != NULL) {
							KkcSegment* _tmp25_;
							KkcSegment* _tmp26_;
							KkcSegment* _tmp27_;
							_tmp25_ = previous_segment;
							_tmp26_ = segment;
							_tmp27_ = _g_object_ref0 (_tmp26_);
							_g_object_unref0 (_tmp25_->next);
							_tmp25_->next = _tmp27_;
						}
						_tmp28_ = segment;
						_tmp29_ = _g_object_ref0 (_tmp28_);
						_g_object_unref0 (previous_segment);
						previous_segment = _tmp29_;
						_g_object_unref0 (segment);
						kkc_language_model_entry_destroy (&entry);
					}
				}
			}
		}
	}
	result = start_segment;
	_g_object_unref0 (previous_segment);
	return result;
}

static gboolean
_kkc_language_model_entry_equal (const KkcLanguageModelEntry * s1,
                                 const KkcLanguageModelEntry * s2)
{
	if (s1 == s2) {
		return TRUE;
	}
	if (s1 == NULL) {
		return FALSE;
	}
	if (s2 == NULL) {
		return FALSE;
	}
	if (g_strcmp0 (s1->input, s2->input)) {
		return FALSE;
	}
	if (g_strcmp0 (s1->output, s2->output)) {
		return FALSE;
	}
	if (s1->id != s2->id) {
		return FALSE;
	}
	return TRUE;
}

static gint
_kkc_bigram_decoder_compare_nbest_node_gcompare_data_func (gconstpointer a,
                                                           gconstpointer b,
                                                           gpointer self)
{
	gint result;
	result = kkc_bigram_decoder_compare_nbest_node ((KkcNbestNode*) a, (KkcNbestNode*) b);
	return result;
}

static gpointer
_kkc_nbest_node_ref0 (gpointer self)
{
	return self ? kkc_nbest_node_ref (self) : NULL;
}

KkcSegment**
kkc_bigram_decoder_backward_search (KkcBigramDecoder* self,
                                    GeeArrayList** trellis,
                                    gint trellis_length1,
                                    gint nbest,
                                    gdouble max_distance,
                                    gdouble min_path_cost,
                                    gint* result_length1)
{
	KkcTrellisNode* bos_trellis_node = NULL;
	GeeArrayList* _tmp0_;
	gpointer _tmp1_;
	KkcTrellisNode* eos_trellis_node = NULL;
	GeeArrayList* _tmp2_;
	gpointer _tmp3_;
	GeeHashMap* trellis_nbest_map = NULL;
	GeeHashMap* _tmp44_;
	GeePriorityQueue* open_list = NULL;
	GeePriorityQueue* _tmp45_;
	GeePriorityQueue* close_list = NULL;
	GeePriorityQueue* _tmp46_;
	GeeHashSet* duplicates = NULL;
	GeeHashSet* _tmp47_;
	KkcNbestNode* eos_nbest_node = NULL;
	KkcTrellisNode* _tmp48_;
	KkcNbestNode* _tmp49_;
	GeeHashMap* _tmp50_;
	KkcTrellisNode* _tmp51_;
	KkcNbestNode* _tmp52_;
	GeePriorityQueue* _tmp53_;
	KkcNbestNode* _tmp54_;
	GeeArrayList* segments = NULL;
	GeeArrayList* _tmp123_;
	GeeArrayList* _tmp134_;
	gint _tmp135_ = 0;
	gpointer* _tmp136_;
	KkcSegment** _tmp137_;
	gint _tmp137__length1;
	KkcSegment** result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = trellis[0];
	_tmp1_ = gee_abstract_list_get ((GeeAbstractList*) _tmp0_, 0);
	bos_trellis_node = (KkcTrellisNode*) _tmp1_;
	_tmp2_ = trellis[trellis_length1 - 1];
	_tmp3_ = gee_abstract_list_get ((GeeAbstractList*) _tmp2_, 0);
	eos_trellis_node = (KkcTrellisNode*) _tmp3_;
	if (nbest == 1) {
		KkcSegment* segment = NULL;
		KkcSegment* next_segment = NULL;
		KkcSegment* _tmp40_;
		KkcSegment* _tmp41_;
		KkcSegment** _tmp42_;
		KkcSegment** _tmp43_;
		gint _tmp43__length1;
		segment = NULL;
		next_segment = NULL;
		{
			KkcTrellisNode* node = NULL;
			KkcTrellisNode* _tmp4_;
			KkcTrellisNode* _tmp5_;
			KkcTrellisNode* _tmp6_;
			_tmp4_ = eos_trellis_node;
			_tmp5_ = _tmp4_->previous;
			_tmp6_ = _kkc_trellis_node_ref0 (_tmp5_);
			node = _tmp6_;
			{
				gboolean _tmp7_ = FALSE;
				_tmp7_ = TRUE;
				while (TRUE) {
					KkcTrellisNode* _tmp11_;
					if (!_tmp7_) {
						KkcTrellisNode* _tmp8_;
						KkcTrellisNode* _tmp9_;
						KkcTrellisNode* _tmp10_;
						_tmp8_ = node;
						_tmp9_ = _tmp8_->previous;
						_tmp10_ = _kkc_trellis_node_ref0 (_tmp9_);
						_kkc_trellis_node_unref0 (node);
						node = _tmp10_;
					}
					_tmp7_ = FALSE;
					_tmp11_ = node;
					if (!(_tmp11_ != NULL)) {
						break;
					}
					{
						gint i = 0;
						KkcTrellisNode* _tmp12_;
						KkcLanguageModelEntry* _tmp13_;
						gint _tmp13__length1;
						gint _tmp14_ = 0;
						KkcLanguageModelEntry* _tmp15_;
						gint _tmp15__length1;
						_tmp12_ = node;
						_tmp13_ = kkc_trellis_node_get_entries (_tmp12_, &_tmp14_);
						_tmp13__length1 = _tmp14_;
						_tmp15_ = _tmp13_;
						_tmp15__length1 = _tmp13__length1;
						i = _tmp15__length1 - 1;
						{
							gboolean _tmp16_ = FALSE;
							_tmp16_ = TRUE;
							while (TRUE) {
								KkcLanguageModelEntry entry = {0};
								KkcTrellisNode* _tmp18_;
								KkcLanguageModelEntry* _tmp19_;
								gint _tmp19__length1;
								gint _tmp20_ = 0;
								KkcLanguageModelEntry* _tmp21_;
								gint _tmp21__length1;
								KkcLanguageModelEntry _tmp22_;
								KkcLanguageModelEntry _tmp23_;
								KkcLanguageModelEntry _tmp24_ = {0};
								KkcLanguageModelEntry _tmp25_;
								KkcBigramLanguageModel* _tmp26_;
								KkcBigramLanguageModel* _tmp27_;
								KkcLanguageModelEntry _tmp28_ = {0};
								KkcLanguageModelEntry _tmp29_;
								KkcLanguageModelEntry _tmp30_;
								const gchar* _tmp31_;
								KkcLanguageModelEntry _tmp32_;
								const gchar* _tmp33_;
								KkcSegment* _tmp34_;
								KkcSegment* _tmp35_;
								KkcSegment* _tmp36_;
								KkcSegment* _tmp37_;
								KkcSegment* _tmp38_;
								KkcSegment* _tmp39_;
								if (!_tmp16_) {
									gint _tmp17_;
									_tmp17_ = i;
									i = _tmp17_ - 1;
								}
								_tmp16_ = FALSE;
								if (!(i >= 0)) {
									break;
								}
								_tmp18_ = node;
								_tmp19_ = kkc_trellis_node_get_entries (_tmp18_, &_tmp20_);
								_tmp19__length1 = _tmp20_;
								_tmp21_ = _tmp19_;
								_tmp21__length1 = _tmp19__length1;
								_tmp22_ = _tmp21_[i];
								_tmp23_ = _tmp22_;
								kkc_language_model_entry_copy (&_tmp23_, &_tmp24_);
								entry = _tmp24_;
								_tmp25_ = entry;
								_tmp26_ = kkc_bigram_decoder_get_model (self);
								_tmp27_ = _tmp26_;
								kkc_language_model_get_bos ((KkcLanguageModel*) _tmp27_, &_tmp28_);
								_tmp29_ = _tmp28_;
								if (_kkc_language_model_entry_equal (&_tmp25_, &_tmp29_) == TRUE) {
									kkc_language_model_entry_destroy (&entry);
									break;
								}
								_tmp30_ = entry;
								_tmp31_ = _tmp30_.input;
								_tmp32_ = entry;
								_tmp33_ = _tmp32_.output;
								_tmp34_ = kkc_segment_new (_tmp31_, _tmp33_);
								_g_object_unref0 (segment);
								segment = _tmp34_;
								_tmp35_ = segment;
								_tmp36_ = next_segment;
								_tmp37_ = _g_object_ref0 (_tmp36_);
								_g_object_unref0 (_tmp35_->next);
								_tmp35_->next = _tmp37_;
								_tmp38_ = segment;
								_tmp39_ = _g_object_ref0 (_tmp38_);
								_g_object_unref0 (next_segment);
								next_segment = _tmp39_;
								kkc_language_model_entry_destroy (&entry);
							}
						}
					}
				}
			}
			_kkc_trellis_node_unref0 (node);
		}
		_tmp40_ = segment;
		_tmp41_ = _g_object_ref0 (_tmp40_);
		_tmp42_ = g_new0 (KkcSegment*, 1 + 1);
		_tmp42_[0] = _tmp41_;
		_tmp43_ = _tmp42_;
		_tmp43__length1 = 1;
		if (result_length1) {
			*result_length1 = _tmp43__length1;
		}
		result = _tmp43_;
		_g_object_unref0 (next_segment);
		_g_object_unref0 (segment);
		_kkc_trellis_node_unref0 (eos_trellis_node);
		_kkc_trellis_node_unref0 (bos_trellis_node);
		return result;
	}
	_tmp44_ = gee_hash_map_new (KKC_TYPE_TRELLIS_NODE, (GBoxedCopyFunc) kkc_trellis_node_ref, (GDestroyNotify) kkc_trellis_node_unref, KKC_TYPE_NBEST_NODE, (GBoxedCopyFunc) kkc_nbest_node_ref, (GDestroyNotify) kkc_nbest_node_unref, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	trellis_nbest_map = _tmp44_;
	_tmp45_ = gee_priority_queue_new (KKC_TYPE_NBEST_NODE, (GBoxedCopyFunc) kkc_nbest_node_ref, (GDestroyNotify) kkc_nbest_node_unref, _kkc_bigram_decoder_compare_nbest_node_gcompare_data_func, NULL, NULL);
	open_list = _tmp45_;
	_tmp46_ = gee_priority_queue_new (KKC_TYPE_NBEST_NODE, (GBoxedCopyFunc) kkc_nbest_node_ref, (GDestroyNotify) kkc_nbest_node_unref, _kkc_bigram_decoder_compare_nbest_node_gcompare_data_func, NULL, NULL);
	close_list = _tmp46_;
	_tmp47_ = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL, NULL, NULL, NULL);
	duplicates = _tmp47_;
	_tmp48_ = eos_trellis_node;
	_tmp49_ = kkc_nbest_node_new (_tmp48_);
	eos_nbest_node = _tmp49_;
	_tmp50_ = trellis_nbest_map;
	_tmp51_ = eos_trellis_node;
	_tmp52_ = eos_nbest_node;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp50_, _tmp51_, _tmp52_);
	_tmp53_ = open_list;
	_tmp54_ = eos_nbest_node;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp53_, _tmp54_);
	while (TRUE) {
		GeePriorityQueue* _tmp55_;
		gboolean _tmp56_;
		gboolean _tmp57_;
		KkcNbestNode* current_nbest_node = NULL;
		GeePriorityQueue* _tmp58_;
		gpointer _tmp59_;
		KkcNbestNode* _tmp60_;
		KkcTrellisNode* _tmp61_;
		KkcTrellisNode* _tmp62_;
		_tmp55_ = open_list;
		_tmp56_ = gee_collection_get_is_empty ((GeeCollection*) _tmp55_);
		_tmp57_ = _tmp56_;
		if (!(!_tmp57_)) {
			break;
		}
		_tmp58_ = open_list;
		_tmp59_ = gee_abstract_queue_poll ((GeeAbstractQueue*) _tmp58_);
		current_nbest_node = (KkcNbestNode*) _tmp59_;
		_tmp60_ = current_nbest_node;
		_tmp61_ = _tmp60_->node;
		_tmp62_ = bos_trellis_node;
		if (_tmp61_ == _tmp62_) {
			gchar* output = NULL;
			KkcNbestNode* _tmp63_;
			gchar* _tmp64_;
			GeeHashSet* _tmp65_;
			const gchar* _tmp66_;
			_tmp63_ = current_nbest_node;
			_tmp64_ = kkc_bigram_decoder_concat_nbest_node_outputs (self, _tmp63_);
			output = _tmp64_;
			_tmp65_ = duplicates;
			_tmp66_ = output;
			if (!gee_abstract_collection_contains ((GeeAbstractCollection*) _tmp65_, _tmp66_)) {
				KkcTrellisNode* _tmp67_;
				KkcNbestNode* _tmp68_;
				GeePriorityQueue* _tmp69_;
				KkcNbestNode* _tmp70_;
				GeePriorityQueue* _tmp71_;
				gint _tmp72_;
				gint _tmp73_;
				GeeHashSet* _tmp74_;
				const gchar* _tmp75_;
				_tmp67_ = eos_trellis_node;
				_tmp68_ = current_nbest_node;
				if ((_tmp67_->cumulative_cost - _tmp68_->fn) > max_distance) {
					_g_free0 (output);
					_kkc_nbest_node_unref0 (current_nbest_node);
					break;
				}
				_tmp69_ = close_list;
				_tmp70_ = current_nbest_node;
				gee_abstract_collection_add ((GeeAbstractCollection*) _tmp69_, _tmp70_);
				_tmp71_ = close_list;
				_tmp72_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp71_);
				_tmp73_ = _tmp72_;
				if (_tmp73_ == nbest) {
					_g_free0 (output);
					_kkc_nbest_node_unref0 (current_nbest_node);
					break;
				}
				_tmp74_ = duplicates;
				_tmp75_ = output;
				gee_abstract_collection_add ((GeeAbstractCollection*) _tmp74_, _tmp75_);
			}
			_g_free0 (output);
		} else {
			KkcNbestNode* _tmp76_;
			KkcTrellisNode* _tmp77_;
			guint _tmp78_;
			guint _tmp79_;
			KkcNbestNode* _tmp80_;
			KkcTrellisNode* _tmp81_;
			guint _tmp82_;
			guint _tmp83_;
			_tmp76_ = current_nbest_node;
			_tmp77_ = _tmp76_->node;
			_tmp78_ = kkc_trellis_node_get_endpos (_tmp77_);
			_tmp79_ = _tmp78_;
			_tmp80_ = current_nbest_node;
			_tmp81_ = _tmp80_->node;
			_tmp82_ = kkc_trellis_node_get_length (_tmp81_);
			_tmp83_ = _tmp82_;
			if (_tmp79_ >= _tmp83_) {
				guint i = 0U;
				KkcNbestNode* _tmp84_;
				KkcTrellisNode* _tmp85_;
				guint _tmp86_;
				guint _tmp87_;
				KkcNbestNode* _tmp88_;
				KkcTrellisNode* _tmp89_;
				guint _tmp90_;
				guint _tmp91_;
				_tmp84_ = current_nbest_node;
				_tmp85_ = _tmp84_->node;
				_tmp86_ = kkc_trellis_node_get_endpos (_tmp85_);
				_tmp87_ = _tmp86_;
				_tmp88_ = current_nbest_node;
				_tmp89_ = _tmp88_->node;
				_tmp90_ = kkc_trellis_node_get_length (_tmp89_);
				_tmp91_ = _tmp90_;
				i = ((gint) _tmp87_) - _tmp91_;
				{
					GeeArrayList* _trellis_node_list = NULL;
					GeeArrayList* _tmp92_;
					gint _trellis_node_size = 0;
					GeeArrayList* _tmp93_;
					gint _tmp94_;
					gint _tmp95_;
					gint _trellis_node_index = 0;
					_tmp92_ = trellis[i];
					_trellis_node_list = _tmp92_;
					_tmp93_ = _trellis_node_list;
					_tmp94_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp93_);
					_tmp95_ = _tmp94_;
					_trellis_node_size = _tmp95_;
					_trellis_node_index = -1;
					while (TRUE) {
						gint _tmp96_;
						gint _tmp97_;
						KkcTrellisNode* trellis_node = NULL;
						GeeArrayList* _tmp98_;
						gpointer _tmp99_;
						KkcNbestNode* nbest_node = NULL;
						KkcTrellisNode* _tmp100_;
						KkcNbestNode* _tmp101_;
						gdouble cost = 0.0;
						KkcNbestNode* _tmp102_;
						KkcTrellisNode* _tmp103_;
						KkcNbestNode* _tmp104_;
						KkcTrellisNode* _tmp105_;
						gboolean _tmp106_ = FALSE;
						KkcTrellisNode* _tmp107_;
						KkcTrellisNode* _tmp108_;
						KkcNbestNode* _tmp109_;
						KkcNbestNode* _tmp110_;
						KkcNbestNode* _tmp111_;
						KkcNbestNode* _tmp112_;
						KkcNbestNode* _tmp113_;
						KkcTrellisNode* _tmp114_;
						KkcNbestNode* _tmp115_;
						KkcNbestNode* _tmp116_;
						KkcNbestNode* _tmp117_;
						GeeHashMap* _tmp118_;
						KkcTrellisNode* _tmp119_;
						KkcNbestNode* _tmp120_;
						GeePriorityQueue* _tmp121_;
						KkcNbestNode* _tmp122_;
						_trellis_node_index = _trellis_node_index + 1;
						_tmp96_ = _trellis_node_index;
						_tmp97_ = _trellis_node_size;
						if (!(_tmp96_ < _tmp97_)) {
							break;
						}
						_tmp98_ = _trellis_node_list;
						_tmp99_ = gee_abstract_list_get ((GeeAbstractList*) _tmp98_, _trellis_node_index);
						trellis_node = (KkcTrellisNode*) _tmp99_;
						_tmp100_ = trellis_node;
						_tmp101_ = kkc_nbest_node_new (_tmp100_);
						nbest_node = _tmp101_;
						_tmp102_ = nbest_node;
						_tmp103_ = _tmp102_->node;
						_tmp104_ = current_nbest_node;
						_tmp105_ = _tmp104_->node;
						cost = kkc_bigram_decoder_path_cost (self, _tmp103_, _tmp105_, (gint) i);
						_tmp107_ = trellis_node;
						_tmp108_ = bos_trellis_node;
						if (_tmp107_ != _tmp108_) {
							_tmp106_ = cost < min_path_cost;
						} else {
							_tmp106_ = FALSE;
						}
						if (_tmp106_) {
							_kkc_nbest_node_unref0 (nbest_node);
							_kkc_trellis_node_unref0 (trellis_node);
							continue;
						}
						_tmp109_ = nbest_node;
						_tmp110_ = current_nbest_node;
						_tmp109_->gn = cost + _tmp110_->gn;
						_tmp111_ = nbest_node;
						_tmp112_ = nbest_node;
						_tmp113_ = nbest_node;
						_tmp114_ = _tmp113_->node;
						_tmp111_->fn = _tmp112_->gn + _tmp114_->cumulative_cost;
						_tmp115_ = nbest_node;
						_tmp116_ = current_nbest_node;
						_tmp117_ = _kkc_nbest_node_ref0 (_tmp116_);
						_kkc_nbest_node_unref0 (_tmp115_->next);
						_tmp115_->next = _tmp117_;
						_tmp118_ = trellis_nbest_map;
						_tmp119_ = trellis_node;
						_tmp120_ = nbest_node;
						gee_abstract_map_set ((GeeAbstractMap*) _tmp118_, _tmp119_, _tmp120_);
						_tmp121_ = open_list;
						_tmp122_ = nbest_node;
						gee_abstract_collection_add ((GeeAbstractCollection*) _tmp121_, _tmp122_);
						_kkc_nbest_node_unref0 (nbest_node);
						_kkc_trellis_node_unref0 (trellis_node);
					}
				}
			}
		}
		_kkc_nbest_node_unref0 (current_nbest_node);
	}
	_tmp123_ = gee_array_list_new (KKC_TYPE_SEGMENT, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	segments = _tmp123_;
	{
		GeeIterator* _nbest_node_it = NULL;
		GeePriorityQueue* _tmp124_;
		GeeIterator* _tmp125_;
		_tmp124_ = close_list;
		_tmp125_ = gee_abstract_collection_iterator ((GeeAbstractCollection*) _tmp124_);
		_nbest_node_it = _tmp125_;
		while (TRUE) {
			GeeIterator* _tmp126_;
			KkcNbestNode* nbest_node = NULL;
			GeeIterator* _tmp127_;
			gpointer _tmp128_;
			GeeArrayList* _tmp129_;
			KkcNbestNode* _tmp130_;
			KkcNbestNode* _tmp131_;
			KkcSegment* _tmp132_;
			KkcSegment* _tmp133_;
			_tmp126_ = _nbest_node_it;
			if (!gee_iterator_next (_tmp126_)) {
				break;
			}
			_tmp127_ = _nbest_node_it;
			_tmp128_ = gee_iterator_get (_tmp127_);
			nbest_node = (KkcNbestNode*) _tmp128_;
			_tmp129_ = segments;
			_tmp130_ = nbest_node;
			_tmp131_ = _tmp130_->next;
			_tmp132_ = kkc_bigram_decoder_nbest_node_to_segment (self, _tmp131_);
			_tmp133_ = _tmp132_;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp129_, _tmp133_);
			_g_object_unref0 (_tmp133_);
			_kkc_nbest_node_unref0 (nbest_node);
		}
		_g_object_unref0 (_nbest_node_it);
	}
	_tmp134_ = segments;
	_tmp136_ = gee_collection_to_array ((GeeCollection*) _tmp134_, &_tmp135_);
	_tmp137_ = _tmp136_;
	_tmp137__length1 = _tmp135_;
	if (result_length1) {
		*result_length1 = _tmp137__length1;
	}
	result = _tmp137_;
	_g_object_unref0 (segments);
	_kkc_nbest_node_unref0 (eos_nbest_node);
	_g_object_unref0 (duplicates);
	_g_object_unref0 (close_list);
	_g_object_unref0 (open_list);
	_g_object_unref0 (trellis_nbest_map);
	_kkc_trellis_node_unref0 (eos_trellis_node);
	_kkc_trellis_node_unref0 (bos_trellis_node);
	return result;
}

static gint
kkc_bigram_decoder_compare_nbest_node (KkcNbestNode* a,
                                       KkcNbestNode* b)
{
	gint result;
	g_return_val_if_fail (a != NULL, -1);
	g_return_val_if_fail (b != NULL, -1);
	if (a->fn == b->fn) {
		result = 0;
		return result;
	} else {
		if (a->fn < b->fn) {
			result = 1;
			return result;
		} else {
			result = -1;
			return result;
		}
	}
}

static gchar*
kkc_bigram_decoder_concat_nbest_node_outputs (KkcBigramDecoder* self,
                                              KkcNbestNode* nbest_node)
{
	GString* builder = NULL;
	GString* _tmp0_;
	GString* _tmp6_;
	const gchar* _tmp7_;
	gchar* _tmp8_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (nbest_node != NULL, NULL);
	_tmp0_ = g_string_new ("");
	builder = _tmp0_;
	while (TRUE) {
		GString* _tmp1_;
		KkcTrellisNode* _tmp2_;
		const gchar* _tmp3_;
		const gchar* _tmp4_;
		KkcNbestNode* _tmp5_;
		if (!(nbest_node != NULL)) {
			break;
		}
		_tmp1_ = builder;
		_tmp2_ = nbest_node->node;
		_tmp3_ = kkc_trellis_node_get_output (_tmp2_);
		_tmp4_ = _tmp3_;
		g_string_append (_tmp1_, _tmp4_);
		_tmp5_ = nbest_node->next;
		nbest_node = _tmp5_;
	}
	_tmp6_ = builder;
	_tmp7_ = _tmp6_->str;
	_tmp8_ = g_strdup (_tmp7_);
	result = _tmp8_;
	_g_string_free0 (builder);
	return result;
}

KkcBigramDecoder*
kkc_bigram_decoder_construct (GType object_type,
                              KkcBigramLanguageModel* model)
{
	KkcBigramDecoder * self = NULL;
	KkcBigramLanguageModel* _tmp0_;
	g_return_val_if_fail (model != NULL, NULL);
	self = (KkcBigramDecoder*) kkc_decoder_construct (object_type);
	_tmp0_ = _g_object_ref0 (model);
	_g_object_unref0 (self->priv->_model);
	self->priv->_model = _tmp0_;
	return self;
}

KkcBigramDecoder*
kkc_bigram_decoder_new (KkcBigramLanguageModel* model)
{
	return kkc_bigram_decoder_construct (KKC_TYPE_BIGRAM_DECODER, model);
}

KkcBigramLanguageModel*
kkc_bigram_decoder_get_model (KkcBigramDecoder* self)
{
	KkcBigramLanguageModel* result;
	KkcBigramLanguageModel* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_model;
	result = _tmp0_;
	return result;
}

static void
kkc_bigram_decoder_class_init (KkcBigramDecoderClass * klass,
                               gpointer klass_data)
{
	kkc_bigram_decoder_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &KkcBigramDecoder_private_offset);
	((KkcDecoderClass *) klass)->decode = (KkcSegment** (*) (KkcDecoder*, const gchar*, gint, gint*, gint, gint*)) kkc_bigram_decoder_real_decode;
	((KkcDecoderClass *) klass)->decode_with_costs = (KkcSegment** (*) (KkcDecoder*, const gchar*, gint, gint*, gint, gdouble, gdouble, gint*)) kkc_bigram_decoder_real_decode_with_costs;
	((KkcBigramDecoderClass *) klass)->path_cost = (gdouble (*) (KkcBigramDecoder*, KkcTrellisNode*, KkcTrellisNode*, gint)) kkc_bigram_decoder_real_path_cost;
	G_OBJECT_CLASS (klass)->get_property = _vala_kkc_bigram_decoder_get_property;
	G_OBJECT_CLASS (klass)->finalize = kkc_bigram_decoder_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), KKC_BIGRAM_DECODER_MODEL_PROPERTY, kkc_bigram_decoder_properties[KKC_BIGRAM_DECODER_MODEL_PROPERTY] = g_param_spec_object ("model", "model", "model", KKC_TYPE_BIGRAM_LANGUAGE_MODEL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
}

static void
kkc_bigram_decoder_instance_init (KkcBigramDecoder * self,
                                  gpointer klass)
{
	self->priv = kkc_bigram_decoder_get_instance_private (self);
}

static void
kkc_bigram_decoder_finalize (GObject * obj)
{
	KkcBigramDecoder * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, KKC_TYPE_BIGRAM_DECODER, KkcBigramDecoder);
	_g_object_unref0 (self->priv->_model);
	G_OBJECT_CLASS (kkc_bigram_decoder_parent_class)->finalize (obj);
}

static GType
kkc_bigram_decoder_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (KkcBigramDecoderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) kkc_bigram_decoder_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (KkcBigramDecoder), 0, (GInstanceInitFunc) kkc_bigram_decoder_instance_init, NULL };
	GType kkc_bigram_decoder_type_id;
	kkc_bigram_decoder_type_id = g_type_register_static (KKC_TYPE_DECODER, "KkcBigramDecoder", &g_define_type_info, 0);
	KkcBigramDecoder_private_offset = g_type_add_instance_private (kkc_bigram_decoder_type_id, sizeof (KkcBigramDecoderPrivate));
	return kkc_bigram_decoder_type_id;
}

GType
kkc_bigram_decoder_get_type (void)
{
	static volatile gsize kkc_bigram_decoder_type_id__once = 0;
	if (g_once_init_enter (&kkc_bigram_decoder_type_id__once)) {
		GType kkc_bigram_decoder_type_id;
		kkc_bigram_decoder_type_id = kkc_bigram_decoder_get_type_once ();
		g_once_init_leave (&kkc_bigram_decoder_type_id__once, kkc_bigram_decoder_type_id);
	}
	return kkc_bigram_decoder_type_id__once;
}

static void
_vala_kkc_bigram_decoder_get_property (GObject * object,
                                       guint property_id,
                                       GValue * value,
                                       GParamSpec * pspec)
{
	KkcBigramDecoder * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, KKC_TYPE_BIGRAM_DECODER, KkcBigramDecoder);
	switch (property_id) {
		case KKC_BIGRAM_DECODER_MODEL_PROPERTY:
		g_value_set_object (value, kkc_bigram_decoder_get_model (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

