Few more
[apertium.git] / trunk / crossdics / src / dictools / DicFormat.java
blobd461f518f7d62dd351968f4b4cc16c7ef31ec42a
1 /*
2 * Copyright (C) 2007 Universitat d'Alacant / Universidad de Alicante
3 * Author: Enrique Benimeli Bofarull
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 * 02111-1307, USA.
21 package dictools;
23 import dics.elements.dtd.DictionaryElement;
24 import dics.elements.dtd.EElement;
25 import dics.elements.dtd.SectionElement;
26 import dics.elements.utils.EElementList;
27 import dics.elements.utils.EHashMap;
28 import dics.elements.utils.Msg;
30 /**
32 * @author Enrique Benimeli Bofarull
35 public class DicFormat {
37 /**
40 private DictionaryElement dicFormatted;
42 /**
45 private String[] arguments;
47 /**
50 public static final int BIL = 0;
52 /**
55 public static final int MON = 1;
57 /**
60 private int dicType;
62 /**
65 private String out;
67 /**
70 private Msg msg;
72 /**
76 public DicFormat() {
77 msg = new Msg();
80 /**
82 * @param dic
84 public DicFormat(final DictionaryElement dic) {
85 msg = new Msg();
86 dicFormatted = dic;
89 /**
91 * @return
93 public final DictionaryElement format() {
94 final EHashMap eMap = new EHashMap();
95 for (SectionElement section : dicFormatted.getSections()) {
96 int duplicated = 0;
97 final EElementList elements = section.getEElements();
98 for (final EElement e : elements) {
99 final String e1Key = e.toString();
100 if (!eMap.containsKey(e1Key)) {
101 eMap.put(e1Key, e);
102 } else {
103 // EElement other = (EElement)eMap.get(e1Key);
104 String left = e.getValue("L");
105 String right = e.getValue("R");
106 msg.err("Duplicated: " + left + "/" + right);
107 duplicated++;
110 String errorMsg = duplicated + " duplicated entries in section '"
111 + section.getID() + "'";
112 msg.err(errorMsg);
113 msg.out(errorMsg);
116 DicSort dicSort = new DicSort(dicFormatted);
118 * dicSort.setDicType(getDicType()); dicSort.setOut(this.getOut());
119 * DictionaryElement formatted = dicSort.sort();
120 * formatted.printXML(getOut()); return formatted;
122 return dicFormatted;
129 public final void doFormat() {
130 processArguments();
131 actionFormat();
138 private void processArguments() {
139 if (arguments[1].equals("-mon")) {
140 dicType = DicSort.MON;
141 } else {
142 dicType = DicSort.BIL;
144 DictionaryReader dicReader = new DictionaryReader(arguments[2]);
145 DictionaryElement dic = dicReader.readDic();
146 dicReader = null;
147 setDicFormatted(dic);
149 if (getArguments().length == 4) {
150 if (getArguments()[3].equals("out.dix")) {
151 out = DicTools.removeExtension(getArguments()[3]);
152 out = out + "-formatted.dix";
153 } else {
154 setOut(getArguments()[3]);
163 private final void actionFormat() {
164 final DictionaryElement dicFormatted = format();
165 dicFormatted.printXML(getOut());
169 * @param dicFormatted
170 * the dicFormatted to set
172 private final void setDicFormatted(DictionaryElement dicFormatted) {
173 this.dicFormatted = dicFormatted;
177 * @return the arguments
179 public final String[] getArguments() {
180 return arguments;
184 * @param arguments
185 * the arguments to set
187 public final void setArguments(String[] arguments) {
188 this.arguments = arguments;
192 * @return the dicType
194 public final int getDicType() {
195 return dicType;
199 * @param dicType
200 * the dicType to set
202 public final void setDicType(int dicType) {
203 this.dicType = dicType;
207 * @return the out
209 public final String getOut() {
210 return out;
214 * @param out
215 * the out to set
217 public final void setOut(String out) {
218 this.out = out;