from itertools import chain
import io

from chainer import Variable as V

from wrapper import xp

def gen_lines(filename):
    with open(filename) as f:
        for line in f:
            yield line.split()

def gen_batch(lines, vocab, batch_size):
    batch = []
    wtoi = vocab.wtoi
    for line in lines:
        batch.append([wtoi['<s>']]+
                [wtoi[word] for word in line] + [wtoi['</s>']])
        if len(batch) == batch_size:
            yield fill_batch(batch)
            batch = []
    if batch:
        yield batch

def gen_word(batch):
    batch_len = len(batch)
    line_len = len(batch[0])
    for l in range(line_len):
        yield V(xp.array(
            [batch[k][l] for k in range(batch_len)], dtype=xp.int32))

def fill_batch(batch, token=-1):
    max_len = max(len(x) for x in batch)
    return [x + [token] * (max_len - len(x) + 1) for x in batch]

@@ -37,21 +37,21 @@
 o%0A%0Afrom 
-chain
+wrapp
 er impor
@@ -56,22 +56,10 @@
 ort 
-Variable as V%0A
+xp
 %0Afro
@@ -75,18 +75,29 @@
  import 
-xp
+wrap_variable
 %0A%0Adef ge
@@ -532,16 +532,31 @@
  batch%0A%0A
+@wrap_variable%0A
 def gen_
@@ -672,10 +672,8 @@
 eld 
-V(
 xp.a
@@ -677,29 +677,16 @@
 p.array(
-%0A            
 %5Bbatch%5Bk
@@ -717,16 +717,40 @@
 h_len)%5D,
+%0A                       
  dtype=x
@@ -757,17 +757,16 @@
 p.int32)
-)
 %0A%0Adef fi

use decorator