/** * @param encoding input encoding or {@code null} to use the platform default */ public TabBitextReader(String filename, String encoding) { try { if (encoding == null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(filename))); } else { in = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding)); } nextLine = in.readLine(); prevLine = ""; nextPair = tab2StringPair(nextLine); } catch (IOException e) { throw new RuntimeException(e); } }
@Test public void testReader() throws Exception { // Create a simple plain text file. File input = File.createTempFile("input", "txt"); input.deleteOnExit(); // Populate the file with data. PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(input), "UTF-8")); writer.println("This is not actual.\tTo nie jest aktualne."); writer.println("Test\tTest"); writer.println("ab\tVery strange data indeed, much longer than input"); writer.close(); TabBitextReader reader = new TabBitextReader(input.getAbsolutePath(), "UTF-8"); int i = 1; for (StringPair srcAndTrg : reader) { assertTrue(srcAndTrg.getSource() != null); assertTrue(srcAndTrg.getTarget() != null); if (i == 1) { assertEquals("This is not actual.", srcAndTrg.getSource()); } else if (i == 2) { assertEquals("Test", srcAndTrg.getSource()); } else if (i == 3) { assertEquals("Very strange data indeed, much longer than input", srcAndTrg.getTarget()); } i++; } } }
@Override public StringPair next() { try { StringPair result = nextPair; sentencePos = nextPair.getSource().length() + 1; if (nextLine != null) { prevLine = nextLine; nextLine = in.readLine(); nextPair = tab2StringPair(nextLine); lineCount++; if (nextLine == null) { in.close(); } } return result; } catch (IOException e) { throw new RuntimeException(e); } }
/** * @param encoding input encoding or {@code null} to use the platform default */ public TabBitextReader(String filename, String encoding) { try { if (encoding == null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(filename))); } else { in = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding)); } nextLine = in.readLine(); prevLine = ""; nextPair = tab2StringPair(nextLine); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public StringPair next() { try { StringPair result = nextPair; sentencePos = nextPair.getSource().length() + 1; if (nextLine != null) { prevLine = nextLine; nextLine = in.readLine(); nextPair = tab2StringPair(nextLine); lineCount++; if (nextLine == null) { in.close(); } } return result; } catch (IOException e) { throw new RuntimeException(e); } }