forked from zygmuntz/kaggle-avito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
construct_line.py
76 lines (60 loc) · 1.76 KB
/
construct_line.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
"a helper module used by tsv2vw"
import json
def get_words( text ):
return text.replace( '|', ' ' ).replace( ':', ' ' ).replace( "\n", ' ' ).replace( ',', ' ' ).replace( '.', ' ' )
def construct_line( line ):
new_line = []
try:
label = line['is_blocked']
except KeyError:
# test
label = '1'
if label == '0':
label = '-1'
new_line.append( "{} 1 {}|i".format( label, line['itemid'] ))
# integer features: |i namespace
for c in ( 'price', 'phones_cnt', 'emails_cnt', 'urls_cnt' ):
v = line[c]
new_item = "{}:{}".format( c, v )
new_line.append( new_item )
# |c and |s namespaces
for c in ( 'category', 'subcategory' ):
v = line[c].replace( ' ', '_' )
new_item = "|{} {}".format( c[0], v )
new_line.append( new_item )
# |t and |d namespaces
for c in ( 'title', 'description' ):
v = get_words( line[c] )
new_item = "|{} {}".format( c[0], v )
new_line.append( new_item )
attrs = None
if line['attrs'] != '':
try:
attrs = json.loads( line['attrs'] )
except ValueError:
try:
attrs = json.loads( line['attrs'].replace( '/"', r'\"' ))
except ValueError:
print "json.loads() failed, trying eval()..."
try:
# will produce UnicodeDecodeError below
attrs = eval( line['attrs'] )
#print "OK"
except ValueError:
print line['attrs']
if attrs:
attribs = []
for k, v in attrs.items():
attribs.append(( k + '_' + v ).replace( ' ', '_' ).replace( ':', '_' ))
try:
attribs = map( lambda x: x.encode( 'utf-8' ), attribs )
except UnicodeDecodeError:
pass
# print "UnicodeDecodeError"
#print attribs
new_item = "|a {}".format( " ".join( attribs ))
new_line.append( new_item )
new_line = " ".join( new_line )
new_line += "\n"
return new_line