-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_lyrics.cpp
75 lines (67 loc) · 1.71 KB
/
read_lyrics.cpp
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
/*
* read_lyrics.cpp
* by Cameron Flowers
* Comp15 12/7/2014
* Overview -- Code to read in song data from a specified file.
*/
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
//
// read_lyics
// purpose: read in song data from a disk file
// arguments: the name of the file, bool to ask for progress status
// returns: nothing
// does: calls a function each time a word is found
//
void read_lyrics(const char * filename, bool show_progress)
{
ifstream in(filename); // creates an input stream
int song_count = 0; // for progress indicator
string artist, title, word;
// -- While more data to read...
while ( ! in.eof() )
{
// -- First line is the artist
getline(in, artist);
if (in.fail()) break;
// -- Second line is the title
getline(in, title);
if (in.fail()) break;
if ( show_progress )
{
song_count++;
if (song_count % 10000 == 0) {
cout << "At " << song_count <<
" Artist: " << artist <<
" Title:" << title << endl;
}
}
// -- Then read all words until we hit the
// -- special <BREAK> token
while ( in >> word && word != "<BREAK>" ){
//
// -- Found a word
}
// -- Important: skip the newline left behind
in.ignore();
}
}
//
// alpha_only
// purpose: converts a string to lowercase and alphabetic characters
// only. E.g., "Bang!" becomes "bang"
// arguments: a string
// returns: the alpha-only string
// does: converts the string
//
string alpha_only(string s){
ostringstream ss;
for (size_t i=0;i<s.length();i++) {
if (isalnum(s[i])){
ss << (char)(tolower(s[i]));
}
}
return ss.str();
}