2 * Copyright (C) 2006-2007 Felipe Sánchez-Martínez
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 #ifndef __ALIGNMENTTEMPLATE_H_
21 #define __ALIGNMENTTEMPLATE_H_
30 #include <lttoolbox/FSTProcessor.H>
32 #include "Alignment.H"
33 #include "LexicalizedWords.H"
36 class AlignmentTemplate:public Alignment {
38 vector<string> restrictions;
39 int invalid; //While extracting the AT this flag is modified
41 static LexicalizedWords source_lexicalized_words;
42 static LexicalizedWords target_lexicalized_words;
44 //Return true if the translation 't' is ok
45 static bool is_translation_ok(string t);
49 //Set of constant to identify why an AT is discarded (invalid)
50 static const int VALID = 0;
51 static const int INVALID_WRONG_OPEN_WORDS = 1;
52 static const int INVALID_NO_OK_TRANSLATIONS = 2;
53 static const int INVALID_DIFFERENT_TRANSLATIONS = 3;
54 static const int INVALID_RESTRICTIONS = 4;
55 static const int INVALID_NO_EQUALCAT = 5;
56 static const int INVALID_EQUIVALENT_WORD_FOR_WORD = 6;
57 static const int INVALID_OTHERS = 7;
61 AlignmentTemplate(string al);
63 AlignmentTemplate(const AlignmentTemplate& al);
67 /** Use to set the number of times this AT occurs */
68 void set_count(int count);
72 /** Test wether this AT is valid, i. e. it is suitable to be used
73 * within the apertium machine translation system.
74 * if 'equalcat' is true the fisrt tag of aligned (open) words must be
77 bool is_valid(bool equalcat, bool noword4word, FSTProcessor &fstp, Alignment &bilph);
81 /** Test wheter the alignments between open words in source and
82 * target language are ok, i.e., all source open words are aligned
83 * with only one target open word, and vice versa.
85 bool are_open_word_aligments_ok();
87 /** This method returns the position of the open source word that is
88 * aligned with the open target word received as a parameter
89 * NOTE: It is supossed that ONLY one open source word is aligned
90 * with each open target word.
92 int get_open_source_word_pos(int target_pos);
94 /** This method returns the position of the open target word that is
95 * aligned with the open source word received as a parameter
96 * NOTE: It is supossed that ONLY one open target word is aligned
97 * with each open source word.
99 int get_open_target_word_pos(int source_pos);
101 /** Test if the alignment template performs no transformation, i.e.,
102 * it is equivalent to word-for-word translation.
103 * NOTE: It is supossed that the Alignment received as input
104 * matches the SL part of the AT and the restrictions.
106 bool is_equivalent_to_word_for_word(Alignment& al, FSTProcessor& fstp);
110 static void set_lexicalized_words(const LexicalizedWords& scw, const LexicalizedWords& tcw);
112 static AlignmentTemplate xtract_alignment_template(Alignment& al, FSTProcessor& fstp);
114 friend ostream& operator << (ostream& os, AlignmentTemplate& at);
116 friend class TransferRule;
118 friend class AlignmentTemplateGreaterThanByCount;
120 //friend class AlignmentTemplateLessThan;
124 //Definition of the compare function used to sort the
125 //alignment templates by their counts
126 class AlignmentTemplateGreaterThanByCount {
128 bool operator()(const AlignmentTemplate &at1, const AlignmentTemplate &at2) const {
129 return (at1.score>at2.score);
134 //Definition of the compare function used to sort the
135 //alignment templates
136 class AlignmentTemplateLessThan {
138 bool operator()(const AlignmentTemplate &at1, const AlignmentTemplate &at2) const {
139 //True if at1 is less than at2
141 if (at1.source.size()<at2.source.size())
143 else if (at1.source.size()>at2.source.size())
147 if (at1.target.size()<at2.target.size())
149 else if (at1.target.size()>at2.target.size())
153 if (at1.restrictions.size()<at2.restrictions.size())
155 else if (at1.restrictions.size()>at2.restrictions.size())
159 if (at1.alignment<at2.alignment)
161 else if (at1.alignment>at2.alignment)
165 for (unsigned i=0; i<at1.source.size(); i++) {
166 if (at1.source[i]<at2.source[i])
168 else if (at1.source[i]>at2.source[i])
172 for (unsigned i=0; i<at1.target.size(); i++) {
173 if (at1.target[i]<at2.target[i])
175 else if (at1.target[i]>at2.target[i])
179 for (unsigned i=0; i<at1.restrictions.size(); i++) {
180 if (at1.restrictions[i]<at2.restrictions[i])
182 else if (at1.restrictions[i]>at2.restrictions[i])