Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,43 @@ There's always at least one input file and usually one or more output files. An

__[phraug2](https://github.com/zygmuntz/phraug2) is available. It offers improved handling of command line arguments.__ Check it out.

Pre-requisites
--------------
install the requirements
```bash
# run this in terminal
pip install -r requirements.txt
```

Format conversion
-----------------

`[...]` means that the parameter is optional.
`csv2libsvm` - updated arguments and quality of use.
```bash
# in terminal run:
csv2libsvm.py -h # or csv2libsvm.py --help
```

```console
# output:

csv2libsvm. by zygmuntz. edited by d33pster

`csv2libsvm.py <input file> <output file> [<label index = 0>] [<skip headers = 0>]`
HELP TEXT

Convert CSV to the LIBSVM format. If there are no labels in the input file, specify _label index_ = -1. If there are headers in the input file, specify _skip headers_ = 1.
| -h or --help : show this help text and exit.
COMPULSORY ARGUMENTS:
| -i or --infile : specify input file.
| -o or --outfile : specify output file.
OPTIONAL ARGUMENTS:
| -li or --label-index : specify label index. if no labels in input file, set to -1.
| -sh or --skip-headers : takes 0 or 1. if there are headers in input file, set to 1.
END
```
NOTE: Convert CSV to the LIBSVM format. If there are no labels in the input file, specify _label index_ = -1. If there are headers in the input file, specify _skip headers_ = 1.


`[...]` means that the parameter is optional.

`pivotedcsv2libsvm.py <input file> <output file> [<skip headers = 0>]`

Expand Down
79 changes: 69 additions & 10 deletions csv2libsvm.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,65 @@
import sys
import csv
from collections import defaultdict
from optioner import options


def helper():
print('csv2libsvm. by zygmuntz. edited by d33pster')
print('\nHELP TEXT\n')
print(' | -h or --help : show this help text and exit.')
print(' COMPULSORY ARGUMENTS:')
print(' | -i or --infile : specify input file.')
print(' | -o or --outfile : specify output file.')
print(' OPTIONAL ARGUMENTS:')
print(' | -li or --label-index : specify label index. if no labels in input file, set to -1.')
print(' | -sh or --skip-headers : takes 0 or 1. if there are headers in input file, set to 1.')
print('END')
exit(0)

# create arguments
shortargs = ['i', 'o', 'li', 'sh', 'h']
longargs = ['infile', 'outfile', 'label-index', 'skip-headers', 'help']
compulsory_short_args = ['i', 'o']
compulsory_long_args = ['infile', 'outfile']

argCTRL = options(shortargs, longargs, sys.argv[1:], compulsory_short_args, compulsory_long_args, ['-h', '--help'])

actualargs, check, error, falseargs = argCTRL._argparse()

if not check:
print(f'ERROR: {error}')
exit(1)
else:
if '-h' in actualargs or '--help' in actualargs:
helper()

# check optional args first
if '-li' in actualargs:
label_index = argCTRL._what_is_('li')
elif '--label-index' in actualargs:
label_index = argCTRL._what_is_('label-index')
else:
pass

if '-sh' in actualargs:
skip_headers = argCTRL._what_is_('sh')
elif '--skip-headers' in actualargs:
skip_headers = argCTRL._what_is_('skip-headers')
else:
pass

# compulsory args
if '-i' in actualargs:
input_file = argCTRL._what_is_('i')
else:
input_file = argCTRL._what_is_('infile')

if '-o' in actualargs:
output_file = argCTRL._what_is_('o')
else:
output_file = argCTRL._what_is_('outfile')


def construct_line( label, line ):
new_line = []
Expand All @@ -28,18 +87,18 @@ def construct_line( label, line ):

# ---

input_file = sys.argv[1]
output_file = sys.argv[2]
# input_file = sys.argv[1]
# output_file = sys.argv[2]

try:
label_index = int( sys.argv[3] )
except IndexError:
label_index = 0
# try:
# label_index = int( sys.argv[3] )
# except IndexError:
# label_index = 0

try:
skip_headers = sys.argv[4]
except IndexError:
skip_headers = 0
# try:
# skip_headers = sys.argv[4]
# except IndexError:
# skip_headers = 0

i = open( input_file, 'rb' )
o = open( output_file, 'wb' )
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
optioner>=1.4.1