# coding: utf-8
import sys
import subprocess


def word2vec(train, output, size=100, window=5, sample=0, hs=1, negative=0, threads=4,
             min_count=5, alpha=0.025, debug=2, binary=1, cbow=0,
             save_vocab=None, read_vocab=None, verbose=False):
    process = ['word2vec']
    args = ['-train', '-output', '-size', '-window', '-sample', '-hs', '-negative', '-threads',
            '-min-count', '-alpha', '-debug', '-binary', '-cbow']
    values = [train, output, size, window, sample, hs, negative, threads,
              min_count, alpha, debug, binary, cbow]
    for arg, value in zip(args, values):
        process.append(arg)
        process.append(str(value))
    if save_vocab is not None:
        process.append('-save-vocab')
        process.append(str(save_vocab))
    if read_vocab is not None:
        process.append('-read-vocab')
        process.append(str(read_vocab))
    proc = subprocess.Popen(process, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    if verbose:
        for line in proc.stdout:
            sys.stdout.write(line)
            sys.stdout.flush()

    out, err = proc.communicate()
    if 'ERROR:' in out:
        raise Exception(out)

    # subprocess.call(process)


def word2clusters(train, output, classes, size=100, window=5, sample=0, hs=1, negative=0, threads=4,
                  min_count=5, alpha=0.025, debug=2, binary=0, cbow=0,
                  save_vocab=None, read_vocab=None):
    process = ['word2vec']
    args = ['-train', '-output', '-size', '-window', '-sample', '-hs', '-negative', '-threads',
            '-min-count', '-alpha', '-classes', '-debug', '-binary', '-cbow']
    values = [train, output, size, window, sample, hs, negative, threads,
              min_count, alpha, classes, debug, binary, cbow]
    for arg, value in zip(args, values):
        process.append(arg)
        process.append(str(value))
    if save_vocab is not None:
        process.append('-save-vocab')
        process.append(str(save_vocab))
    if read_vocab is not None:
        process.append('-read-vocab')
        process.append(str(read_vocab))
    subprocess.call(process)


def word2phase(train, output, min_count=5, threshold=100, debug=2):
    process = ['word2phrase']
    args = ['-train', '-output', '-min-count', '-threshold', '-debug']
    values = [train, output, min_count, threshold, debug]
    for arg, value in zip(args, values):
        process.append(arg)
        process.append(str(value))
    subprocess.call(process)

@@ -1188,40 +1188,8 @@
 t)%0A%0A
-    # subprocess.call(process)%0A%0A
 %0Adef
@@ -1407,16 +1407,31 @@
 cab=None
+, verbose=False
 ):%0A    p
@@ -2084,19 +2084,27 @@
 vocab))%0A
+%0A
    
+ proc =
  subproc
@@ -2107,28 +2107,281 @@
 process.
-call(process
+Popen(process, stdout=subprocess.PIPE, stderr=subprocess.PIPE)%0A%0A    if verbose:%0A        for line in proc.stdout:%0A            sys.stdout.write(line)%0A            sys.stdout.flush()%0A%0A    out, err = proc.communicate()%0A    if 'ERROR:' in out:%0A        raise Exception(out
 )%0A%0A%0Adef 
@@ -2441,16 +2441,31 @@
  debug=2
+, verbose=False
 ):%0A    p
@@ -2722,19 +2722,27 @@
 value))%0A
+%0A
    
+ proc =
  subproc
@@ -2749,18 +2749,271 @@
 ess.
-call(process
+Popen(process, stdout=subprocess.PIPE, stderr=subprocess.PIPE)%0A%0A    if verbose:%0A        for line in proc.stdout:%0A            sys.stdout.write(line)%0A            sys.stdout.flush()%0A%0A    out, err = proc.communicate()%0A    if 'ERROR:' in out:%0A        raise Exception(out
 )%0A

Add verbose option