#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 Judit Acs <judit@sch.bme.hu>
#
# Distributed under terms of the MIT license.

from argparse import ArgumentParser
from sys import stdin, stdout


def parse_args():
    p = ArgumentParser()
    p.add_argument('--tagging-type', choices=['BE', 'BEMS'], default='BE',
                   help="Use 2 or 4 tag tagset.")
    return p.parse_args()


def tag_stream(instream, outstream, tagging_type):
    for line in instream:
        try:
            line = line.decode('utf8')
        except AttributeError:
            pass
        fd = line.rstrip('\n').split('\t')
        segmented = fd[-1]
        segments = []
        for segment in segmented.split(' '):
            if tagging_type == 'BE':
                segments.append('B{}'.format('E' * (len(segment)-1)))
            elif tagging_type == 'BEMS':
                if len(segment) == 1:
                    segments.append('S')
                else:
                    segments.append('B{}E'.format('M' * (len(segment)-2)))
        try:
            outstream.write(u'{}\t{}\n'.format('\t'.join(fd[:-1]),
                                               ''.join(segments)).encode('utf8'))
        except TypeError:
            outstream.write('{}\t{}\n'.format('\t'.join(fd[:-1]),
                                              ''.join(segments)))


def main():
    args = parse_args()
    tag_stream(stdin, stdout, tagging_type=args.tagging_type)

if __name__ == '__main__':
    main()

@@ -392,16 +392,269 @@
 gset.%22)%0A
+    p.add_argument('--columns', type=str, default=%22-1%22,%0A                   help=%22Column to convert to BE tagging.%22%0A                   %22More columns may be specified separated by comma.%22%0A                   %22By default only the last column is converted%22)%0A
     retu
@@ -721,18 +721,81 @@
 ing_type
+, columns):%0A    columns = set(int(c) for c in columns.split(',')
 )
-:
 %0A    for
@@ -968,27 +968,143 @@
     
-segmented = fd%5B-1%5D%0A
+output_line = %5B%5D%0A        for i, field in enumerate(fd):%0A            if i+1 in columns or (-1 in columns and i == len(fd) - 1):%0A        
     
@@ -1125,24 +1125,32 @@
  %5B%5D%0A        
+  
+      
 for segment 
@@ -1156,16 +1156,12 @@
  in 
-segmente
+fiel
 d.sp
@@ -1170,16 +1170,24 @@
 t(' '):%0A
+        
         
@@ -1223,32 +1223,40 @@
                 
+ 
+       
 segments.append(
@@ -1305,16 +1305,24 @@
         
+     
+   
 elif tag
@@ -1358,16 +1358,24 @@
         
+        
 if len(s
@@ -1400,32 +1400,40 @@
                 
+ 
+       
 segments.append(
@@ -1457,14 +1457,30 @@
     
-else:%0A
+        else:%0A        
     
@@ -1550,33 +1550,24 @@
 t)-2)))%0A    
-    try:%0A
             
@@ -1573,84 +1573,111 @@
  out
-stream.write(u'%7B%7D%5Ct%7B%7D%5Cn'.format('%5Ct'.join(fd%5B:-1%5D),%0A                
+put_line.append(''.join(segments))%0A            else:%0A                output_line.append(field)%0A
         
     
@@ -1664,32 +1664,37 @@
 (field)%0A        
+try:%0A
                 
@@ -1679,32 +1679,66 @@
  try:%0A          
+  outstream.write(u'%7B%7D%5Cn'.format(%0A
              ''.
@@ -1734,32 +1734,40 @@
         
-'
+   '%5Ct
 '.join(
-segments
+output_line
 )).encod
@@ -1835,16 +1835,13 @@
 ite(
+u
 '%7B%7D%5C
-t%7B%7D%5C
 n'.f
@@ -1860,80 +1860,19 @@
 oin(
-fd%5B:-1%5D),%0A                                              ''.join(segments
+output_line
 )))%0A
@@ -1969,16 +1969,38 @@
 ing_type
+, columns=args.columns
 )%0A%0Aif __

add column selection support