summaryrefslogtreecommitdiff
path: root/src/runtime/c/pgf/aligner.c
blob: b23b3d2e975cfc36e10c0bc152f4c8b278cb31df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "data.h"
#include "linearizer.h"
#include "pgf.h"
#include <gu/utf8.h>

typedef struct {
	PgfLinFuncs* funcs;
	GuBuf* parent_stack;
	GuBuf* parent_current;
	GuBuf* phrases;
	PgfAlignmentPhrase* last_phrase;
	GuStringBuf* sbuf;
	size_t n_matches;
	GuExn* err;
	bool bind;
	PgfCapitState capit;
	GuPool* out_pool;
	GuPool* tmp_pool;
} PgfAlignerLin;

static void
pgf_aligner_flush_phrase(PgfAlignerLin* alin)
{
	size_t n_fids = gu_buf_length(alin->parent_current);

	if (alin->n_matches == n_fids &&
		alin->n_matches == alin->last_phrase->n_fids) {
		// if the current compound word has the same parents
		// as the last one then we just combine them with a space

		alin->last_phrase->phrase =
				gu_format_string(alin->out_pool, "%s %s", 
								 alin->last_phrase->phrase,
								 gu_string_buf_freeze(alin->sbuf, alin->tmp_pool));
	} else {
		// push the current word to the buffer of words
		
		PgfAlignmentPhrase* phrase = 
			gu_new_flex(alin->out_pool, PgfAlignmentPhrase, fids, n_fids);
		phrase->phrase = gu_string_buf_freeze(alin->sbuf, alin->out_pool);
		phrase->n_fids = n_fids;
		for (size_t i = 0; i < n_fids; i++) {
			phrase->fids[i] = gu_buf_get(alin->parent_current, int, i);
		}
		gu_buf_push(alin->phrases, PgfAlignmentPhrase*, phrase);
		
		alin->last_phrase = phrase;
	}

	alin->n_matches = 0;
}

static void
pgf_aligner_push_parent(PgfAlignerLin* alin, int fid)
{
	gu_buf_push(alin->parent_current, int, fid);

	if (alin->last_phrase != NULL) {
		for (size_t i = 0; i < alin->last_phrase->n_fids; i++) {
			if (fid == alin->last_phrase->fids[i]) {
				alin->n_matches++;
				break;
			}
		}
	}
}

static void
pgf_aligner_lzn_symbol_token(PgfLinFuncs** funcs, PgfToken tok)
{
	PgfAlignerLin* alin = gu_container(funcs, PgfAlignerLin, funcs);
	if (!gu_ok(alin->err)) {
		return;
	}

	// get the tree node id that generates this token
	size_t n_parents = gu_buf_length(alin->parent_stack);
	int fid = gu_buf_get(alin->parent_stack, int, n_parents-1);

	// how many nodes so far are involved in the current compound word
	size_t n_fids = gu_buf_length(alin->parent_current);

	if (alin->bind) {
		// here we glue tokens

		alin->bind = false;

		bool found = false;
		for (size_t i = 0; i < n_fids; i++) {
			int current_fid = gu_buf_get(alin->parent_current, int, i);
			if (fid == current_fid) {
				found = true;
				break;
			}
		}

		// add the tree node id to the list of parents if it has not
		// been added already.
		if (!found) {
			pgf_aligner_push_parent(alin, fid);
		}
	} else {
		// here we start a new (compound) word

		pgf_aligner_flush_phrase(alin);
		gu_string_buf_flush(alin->sbuf);
		gu_buf_flush(alin->parent_current);

		pgf_aligner_push_parent(alin, fid);
		
		if (alin->capit == PGF_CAPIT_NEXT)
			alin->capit = PGF_CAPIT_NONE;
	}

	GuOut* out = gu_string_buf_out(alin->sbuf);

	switch (alin->capit) {
	case PGF_CAPIT_NONE:
		gu_string_write(tok, out, alin->err);
		break;
	case PGF_CAPIT_FIRST: {
		GuUCS c = gu_utf8_decode((const uint8_t**) &tok);
		c = gu_ucs_to_upper(c);
		gu_out_utf8(c, out, alin->err);
		gu_string_write(tok, out, alin->err);
		alin->capit = PGF_CAPIT_NONE;
		break;
	}	
	case PGF_CAPIT_ALL:
		alin->capit = PGF_CAPIT_NEXT;
		// continue
	case PGF_CAPIT_NEXT: {
		const uint8_t* p = (uint8_t*) tok;
		while (*p) {
			GuUCS c = gu_utf8_decode(&p);
			c = gu_ucs_to_upper(c);
			gu_out_utf8(c, out, alin->err);
		}
		break;
	}
	}
}

static void
pgf_aligner_lzn_begin_phrase(PgfLinFuncs** funcs, PgfCId cat, int fid, int lindex, PgfCId fun)
{
	PgfAlignerLin* alin = gu_container(funcs, PgfAlignerLin, funcs);
	gu_buf_push(alin->parent_stack, int, fid);
}

static void
pgf_aligner_lzn_end_phrase(PgfLinFuncs** funcs, PgfCId cat, int fid, int lindex, PgfCId fun)
{
	PgfAlignerLin* alin = gu_container(funcs, PgfAlignerLin, funcs);
	gu_buf_pop(alin->parent_stack, int);
}
	
static void
pgf_aligner_lzn_symbol_ne(PgfLinFuncs** funcs)
{
	PgfAlignerLin* alin = gu_container(funcs, PgfAlignerLin, funcs);
	gu_raise(alin->err, PgalinNonExist);
}

static void
pgf_aligner_lzn_symbol_bind(PgfLinFuncs** funcs)
{
	PgfAlignerLin* alin = gu_container(funcs, PgfAlignerLin, funcs);
	alin->bind = true;
}

static void
pgf_aligner_lzn_symbol_capit(PgfLinFuncs** funcs, PgfCapitState capit)
{
	PgfAlignerLin* alin = gu_container(funcs, PgfAlignerLin, funcs);
	alin->capit = capit;
}

static PgfLinFuncs pgf_file_lin_funcs = {
	.symbol_token = pgf_aligner_lzn_symbol_token,
	.begin_phrase = pgf_aligner_lzn_begin_phrase,
	.end_phrase   = pgf_aligner_lzn_end_phrase,
	.symbol_ne    = pgf_aligner_lzn_symbol_ne,
	.symbol_bind  = pgf_aligner_lzn_symbol_bind,
	.symbol_capit = pgf_aligner_lzn_symbol_capit
};

GuSeq*
pgf_align_words(PgfConcr* concr, PgfExpr expr,
                GuExn* err, GuPool* pool)
{
	GuPool* tmp_pool = gu_local_pool();
	
	GuEnum* cts =
		pgf_lzr_concretize(concr, expr, err, tmp_pool);
	if (!gu_ok(err)) {
		gu_pool_free(tmp_pool);
		return NULL;
	}

	GuBuf* phrases = gu_new_buf(PgfAlignmentPhrase*, pool);

	PgfCncTree ctree = gu_next(cts, PgfCncTree, tmp_pool);
	if (!gu_variant_is_null(ctree)) {
		ctree = pgf_lzr_wrap_linref(ctree, tmp_pool);
		
		PgfAlignerLin alin = {
			.funcs = &pgf_file_lin_funcs,
			.parent_stack = gu_new_buf(int, tmp_pool),
			.parent_current = gu_new_buf(int, tmp_pool),
			.phrases = phrases,
			.last_phrase = NULL,
			.sbuf = gu_string_buf(tmp_pool),
			.n_matches = 0,
			.err = err,
			.bind = true,
			.capit = PGF_CAPIT_NONE,
			.out_pool = pool,
			.tmp_pool = tmp_pool
		};
		gu_buf_push(alin.parent_stack, int, -1);

		pgf_lzr_linearize(concr, ctree, 0, &alin.funcs, tmp_pool);
		if (!gu_ok(err)) {
			gu_pool_free(tmp_pool);
			return NULL;
		}

		pgf_aligner_flush_phrase(&alin);
	}

	gu_pool_free(tmp_pool);
	return gu_buf_data_seq(phrases);
}