Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env pike
- class string2id {
- // map from string to identifier
- mapping(string:int) string2id;
- // a number of assigned IDs
- int counter;
- // constructor
- void create()
- {
- string2id = ([]); // initialise string2id to empty mapping
- counter = 0;
- }
- // our main function
- int get_id (string str)
- {
- if (string2id[str] != 0)
- return string2id[str];
- ++counter;
- string2id[str] = counter;
- return counter;
- }
- };
- string2id mapper;
- void get_id_and_print (string feature)
- {
- int id = mapper.get_id(feature);
- write("Feature %s has an identifier %d\n", feature, id);
- }
- int main (array(string) args)
- {
- // creating an object of class string2id
- mapper = string2id();
- // bag of words
- get_id_and_print("apple");
- get_id_and_print("table");
- get_id_and_print("map");
- get_id_and_print("picture");
- get_id_and_print("table");
- get_id_and_print("man");
- get_id_and_print("apple");
- // very special features
- get_id_and_print("providefor_between_company1_and_company2");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement