summaryrefslogtreecommitdiffstats
path: root/JLanguageTool/src/java/de/danielnaber/languagetool/bitext/WordFastTMReader.java
blob: cadad69dbe43950fa74cb48baece36d598789b00 (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
/* LanguageTool, a natural language style checker 
 * Copyright (C) 2010 Marcin Miłkowski (http://www.languagetool.org)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

package de.danielnaber.languagetool.bitext;

import java.io.IOException;
import java.util.Iterator;

/**
 * Reader of WordFast Translation Memory text files.
 * They are simple tab-delimited text files.
 * 
 * @author Marcin Miłkowski
 */
public class WordFastTMReader extends TabBitextReader {

  public WordFastTMReader(final String filename, final String encoding) throws IOException {
    super(filename, encoding);
    //skip the header (first line)
    if (nextLine != null) {
      nextLine = in.readLine();
      nextPair =  tab2StringPair(nextLine);
    }
  }
  
  public final StringPair tab2StringPair(final String line) {
    if (line == null) {
      return null;
    }
    final String[] fields = line.split("\t");
    sentencePos = fields[4].length() + 1;
    return new StringPair(fields[4], fields[6]);
  }
  
  @Override
  public Iterator<StringPair> iterator() {
    return new TabReader();
  }

  class TabReader implements Iterator<StringPair> {

    public boolean hasNext() { 
      return nextLine != null;
    }

    public StringPair next() {
      try {
        final StringPair result = nextPair;

        if (nextLine != null) {
          nextLine = in.readLine();
          nextPair = tab2StringPair(nextLine);
          if (nextLine == null) {
            in.close();
          }
        }
        return result;
      } catch(IOException e) { 
        throw new IllegalArgumentException(e); 
      }
    }

    // The file is read-only.
    public void remove() { 
      throw new UnsupportedOperationException(); 
    }
  }
  
}