MCPcopy Create free account
hub / github.com/davisking/dlib / load_sms_spam_collection

Function load_sms_spam_collection

examples/text_classification_ex.cpp:48–71  ·  view source on GitHub ↗

Reads the SMS Spam Collection: spam messages get the label +1 and ham messages -1.

Source from the content-addressed store, hash-verified

46
47// Reads the SMS Spam Collection: spam messages get the label +1 and ham messages -1.
48void 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

Callers 1

mainFunction · 0.85

Calls 3

clearMethod · 0.45
findMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected