Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
concord.cpp
Go to the documentation of this file.
1
29#include <iostream>
30#include <list>
31#include <locale>
32#include <map>
33#include <string>
34
35#include <examples.h>
36
37
38// Split a line of text into words.
39void split (const std::string& text, const std::string& separators,
40 std::list<std::string,std::allocator<std::string> > &words)
41{
42
43 const std::string::size_type n = text.length ();
44 std::string::size_type start = text.find_first_not_of (separators);
45
46 while (start < n) {
47 std::string::size_type stop = text.find_first_of (separators, start);
48 if (stop > n) stop = n;
49 words.push_back (text.substr (start, stop-start));
50 start = text.find_first_not_of (separators, stop+1);
51 }
52}
53
54
56{
57 typedef std::multimap<std::string,int,
58 std::less<std::string>,
59 std::allocator<std::pair<const std::string,
60 int> > > wordDictType;
61public:
62 void addWord (std::string, int);
63 void readText (std::istream &);
64 void printConcordance (std::ostream &);
65private:
66 wordDictType wordMap;
67};
68
69
70void concordance::addWord (std::string word, int line)
71{
72 // First get range of entries with same key.
73 wordDictType::iterator low = wordMap.lower_bound (word);
74 wordDictType::iterator high = wordMap.upper_bound (word);
75
76 // Loop over entires, see if any match current line.
77 for ( ; low != high; ++low)
78 if ( (*low).second == line)
79 return;
80
81 // Didn't occur, add now.
82 wordMap.insert (wordDictType::value_type (word, line));
83}
84
85
86void allLower (std::string &s)
87{
88 for (std::size_t i = 0; i < s.size (); i++)
89 if ((std::isupper)(s [i], std::locale ()))
90 s [i] = (std::tolower)(s [i], std::locale ());
91}
92
93
94void concordance::readText (std::istream & in)
95{
96 std::string line;
97
98 for (int i = 1; std::getline (in, line); i++) {
99 allLower (line);
100 std::list<std::string, std::allocator<std::string> > words;
101 split (line, " , .;:", words);
102 std::list<std::string, std::allocator<std::string> >::iterator wptr;
103 for (wptr = words.begin (); wptr != words.end (); ++wptr)
104 addWord (*wptr, i);
105 }
106}
107
108
110{
111 std::string lastword ("");
112 wordDictType::iterator pairPtr;
113 wordDictType::iterator stop = wordMap.end ();
114
115 for (pairPtr = wordMap.begin (); pairPtr != stop; ++pairPtr)
116 // If word is same as previous, just print line number.
117 if (lastword == (*pairPtr).first)
118 out << ' ' << (*pairPtr).second;
119 else {
120 // First entry of word.
121 lastword = (*pairPtr).first;
122 std::cout << '\n' << lastword << ": " << (*pairPtr).second;
123 }
124 std::cout << '\n';
125}
126
127
128int main ()
129{
130 std::cout << "Concordance sample program, from Chapter 7\n"
131 << "Enter text, then end-of-file:\n";
132
133 concordance words;
134
135 words.readText (std::cin);
136 words.printConcordance (std::cout);
137
138 std::cout << "End of concordance sample program\n";
139
140 return 0;
141}
#define i
Definition 17.names.cpp:126
#define start
Definition 17.names.cpp:547
#define line
Definition 17.names.cpp:490
#define s
#define n
Definition 17.names.cpp:130
const int out
const int in
void addWord(std::string, int)
Definition concord.cpp:70
void printConcordance(std::ostream &)
Definition concord.cpp:109
void readText(std::istream &)
Definition concord.cpp:94
void allLower(std::string &s)
Definition concord.cpp:86
void split(const std::string &text, const std::string &separators, std::list< std::string, std::allocator< std::string > > &words)
Definition concord.cpp:39
int main()
Definition concord.cpp:128