1 /* 2 Copyright (c) by respective owners including Yahoo!, Microsoft, and 3 individual contributors. All rights reserved. Released under a BSD 4 license as described in the file LICENSE. 5 */ 6 #pragma once 7 #include "global_data.h" 8 #include "example.h" 9 #include "hash.h" 10 #include "simple_label.h" 11 #include "parser.h" 12 13 namespace VW { 14 15 /* Caveats: 16 (1) Some commandline parameters do not make sense as a library. 17 (2) The code is not yet reentrant. 18 */ 19 vw* initialize(string s); 20 21 void cmd_string_replace_value( std::stringstream*& ss, string flag_to_replace, string new_value ); 22 23 char** get_argv_from_string(string s, int& argc); 24 25 /* 26 Call finish() after you are done with the vw instance. This cleans up memory usage. 27 */ 28 void finish(vw& all, bool delete_all=true); 29 30 void start_parser(vw& all, bool do_init = true); 31 void end_parser(vw& all); 32 33 typedef pair< unsigned char, vector<feature> > feature_space; //just a helper definition. 34 struct primitive_feature_space { //just a helper definition. 35 unsigned char name; 36 feature* fs; 37 size_t len; 38 }; 39 40 //The next commands deal with creating examples. Caution: VW does not all allow creation of many examples at once by default. You can adjust the exact number by tweaking ring_size. 41 42 /* The simplest of two ways to create an example. An example_line is the literal line in a VW-format datafile. 43 */ 44 example* read_example(vw& all, char* example_line); 45 example* read_example(vw& all, string example_line); 46 47 //The more complex way to create an example. 48 49 //after you create and fill feature_spaces, get an example with everything filled in. 50 example* import_example(vw& all, primitive_feature_space* features, size_t len); 51 example* import_example(vw& all, vector< feature_space > ec_info); 52 void parse_example_label(vw&all, example&ec, string label); 53 void setup_example(vw& all, example* ae); 54 example* new_unused_example(vw& all); 55 example* get_example(parser* pf); 56 float get_topic_prediction(example*ec, size_t i);//i=0 to max topic -1 57 float get_label(example*ec); 58 float get_importance(example*ec); 59 float get_initial(example*ec); 60 float get_prediction(example*ec); 61 float get_cost_sensitive_prediction(example*ec); 62 size_t get_tag_length(example* ec); 63 const char* get_tag(example* ec); 64 size_t get_feature_number(example* ec); 65 feature* get_features(vw& all, example* ec, size_t& feature_number); 66 void return_features(feature* f); 67 68 void add_constant_feature(vw& all, example*ec); 69 void add_label(example* ec, float label, float weight = 1, float base = 0); 70 71 //notify VW that you are done with the example. 72 void finish_example(vw& all, example* ec); 73 74 void copy_example_data(bool audit, example*, example*, size_t, void(*copy_label)(void*,void*)); 75 void copy_example_data(bool audit, example*, example*); // don't copy the label 76 77 // after export_example, must call releaseFeatureSpace to free native memory 78 primitive_feature_space* export_example(vw& all, example* e, size_t& len); 79 void releaseFeatureSpace(primitive_feature_space* features, size_t len); 80 81 // inlines 82 83 //First create the hash of a namespace. hash_space(vw & all,string s)84 inline uint32_t hash_space(vw& all, string s) 85 { 86 substring ss; 87 ss.begin = (char*)s.c_str(); 88 ss.end = ss.begin + s.length(); 89 return (uint32_t)all.p->hasher(ss,hash_base); 90 } 91 //Then use it as the seed for hashing features. hash_feature(vw & all,string s,unsigned long u)92 inline uint32_t hash_feature(vw& all, string s, unsigned long u) 93 { 94 substring ss; 95 ss.begin = (char*)s.c_str(); 96 ss.end = ss.begin + s.length(); 97 return (uint32_t)(all.p->hasher(ss,u) & all.parse_mask); 98 } 99 hash_feature_cstr(vw & all,char * fstr,unsigned long u)100 inline uint32_t hash_feature_cstr(vw& all, char* fstr, unsigned long u) 101 { 102 substring ss; 103 ss.begin = fstr; 104 ss.end = ss.begin + strlen(fstr); 105 return (uint32_t)(all.p->hasher(ss,u) & all.parse_mask); 106 } 107 get_weight(vw & all,uint32_t index,uint32_t offset)108 inline float get_weight(vw& all, uint32_t index, uint32_t offset) 109 { return all.reg.weight_vector[(((index << all.reg.stride_shift) + offset) & all.reg.weight_mask)];} 110 set_weight(vw & all,uint32_t index,uint32_t offset,float value)111 inline void set_weight(vw& all, uint32_t index, uint32_t offset, float value) 112 { all.reg.weight_vector[(((index << all.reg.stride_shift) + offset) & all.reg.weight_mask)] = value;} 113 num_weights(vw & all)114 inline uint32_t num_weights(vw& all) 115 { return (uint32_t)all.length();} 116 get_stride(vw & all)117 inline uint32_t get_stride(vw& all) 118 { return (uint32_t)(1 << all.reg.stride_shift);} 119 } 120