Reads the SMS Spam Collection: spam messages get the label +1 and ham messages -1.
| 46 | |
| 47 | // Reads the SMS Spam Collection: spam messages get the label +1 and ham messages -1. |
| 48 | void load_sms_spam_collection( |
| 49 | const string& filename, |
| 50 | std::vector<string>& messages, |
| 51 | std::vector<double>& labels |
| 52 | ) |
| 53 | { |
| 54 | ifstream fin(filename); |
| 55 | if (!fin) |
| 56 | throw runtime_error("Unable to open " + filename); |
| 57 | messages.clear(); |
| 58 | labels.clear(); |
| 59 | string line; |
| 60 | while (getline(fin, line)) |
| 61 | { |
| 62 | const auto tab = line.find('\t'); |
| 63 | if (tab == string::npos) |
| 64 | continue; |
| 65 | const string label = line.substr(0, tab); |
| 66 | if (label != "spam" && label != "ham") |
| 67 | throw runtime_error("Unexpected label in " + filename + ": " + label); |
| 68 | labels.push_back(label == "spam" ? +1 : -1); |
| 69 | messages.push_back(line.substr(tab + 1)); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // ---------------------------------------------------------------------------------------- |
| 74 |