Beispiel #1
0
void EchoServer::compute(const std::string &word, const TcpConnectionPtr &conn, const TextQuery &tq)
{	
	if(word == "q")
		exit(0);

	typedef set<TextQuery::line_no> line_nums;
	line_nums locs = tq.run_query(word);
	line_nums::size_type size = locs.size();
	
	char ssize[128] = "";
	sprintf(ssize, "%d", size);

	string s;
	s = s + "\n" + word +  " occurs " + ssize + " "
		 + make_plural(size, "time", "s") + "\n";
	line_nums::const_iterator it = locs.begin();
	for ( ; it != locs.end(); ++it)
	{
		char sit[128] = "";
		sprintf(sit, "%d", (*it) + 1);

		s = s +  "\t(line " + sit + ") "
			 + tq.text_line(*it) + "\r\n";
	}
	s = s + "\nenter word to look for, or q to quit: ";
	conn->send(s);
}
int main(int argc, char **argv)
{

    if (argc != 2) {cerr << "No input file" << endl; return -2;}

    // get a file to read from which user will query words
    ifstream infile;
    if (!open_file(infile, argv[1])) {
        cerr << "No input file!" << endl;
        return -1;
    }
    TextQuery tq;
    tq.read_file(infile);  // builds query map

    // iterate with the user: prompt for a word to find and print results
    string sought;
    do {
        cout << "enter a word against which to search the text.\n"
             << "to quit, enter a single character ==>  ";
        cin  >> sought;

        // stop if hit eof on input or single character entered
        if (!cin || sought.size() < 2) break;

        // find all the occurrences of the users requested string
        vector<TextQuery::location> locs = tq.run_query(sought);

        // report no matches
        if (locs.empty()) {
            cout << "\nSorry. There are no entries for " 
                 << sought << ".\nTry again." << endl;
            continue;
        }
    
        // if the word was found, then print count and all occurrences
        vector<TextQuery::location>::size_type size = locs.size();
        cout << "\n" << sought << " occurs " << size
             << (size == 1 ? " time:" : " times:")
             << "\n" << endl;
    
        // print each line in which the word appeared
        vector<TextQuery::location>::iterator it = locs.begin();
        while (it != locs.end()) {
            cout << "\t(line: "
                 // don't confound user with text lines starting at 0
                 << it->first + 1 << ", pos: " << it->second + 1 << ") "
                 << tq.text_line(it->first) << endl;
            ++it;
         }
    } while (!sought.empty());

    cout << "Ok, bye!" << endl;

    // debugging aid -- look at the map that was built
    tq.display_map();
    return 0;
}
void print_results(const set<TextQuery::line_no>& locs, const string& sought, const TextQuery &file) {
    // Print how many times a word shows, and all lines it in.
    typedef set<TextQuery::line_no> line_nums;
    line_nums::size_type size = locs.size();
    cout << "\n" << sought << " occurs " << size << " " << make_plural(size, "time", "s") << endl;
    line_nums::const_iterator it = locs.begin();
    for (; it != locs.end(); ++it) {
        cout << "\t(line " << (*it) + 1 << ") " << file.text_line(*it) << endl;
    }
}
Beispiel #4
0
void TextQuery::print_result(const std::set<TextQuery::line_no>& locs, const std::string& sought, const TextQuery &file)
{
    typedef std::set<TextQuery::line_no> line_nums;
    line_nums::size_type size = locs.size();
    std::cout << "\n" << sought << " occurs" << size << " " << make_plural(size, "time", "s") << std::endl;
    line_nums::const_iterator it = locs.begin();
    for (; it != locs.end(); ++it) {
        std::cout << "\t(line " << (*it) + 1 << ")" << file.text_line(*it) << std::endl;
    }
}
Beispiel #5
0
int main(int argc, char *argv[])
{
	std::string s1("the"), s2("Her"), s3("in");
	const std::string file("text.txt");;
	TextQuery tq;
	std::ifstream fin(file.c_str());
	tq.read_file(fin);
	Query q = (Query(s1) & Query(s2)) | Query(s3);
	std::set<TextQuery::line_no> set = q.eval(tq);
	typedef std::set<TextQuery::line_no>::iterator iter_stq;
	for( iter_stq iter= set.begin() ; iter != set.end() ; iter++ )
	{
		std::cout << tq.text_line(*iter) << std::endl;
	}
	return 0;
}
int main(int argc, char *argv[])
{
    TextQuery tq;
    ifstream is("luo.txt");
    tq.read_file(is);
    string str("luo");
    set<TextQuery::line_no> s_lno = tq.run_query(str);
    cout << str << " occurs " << s_lno.size() << " times" << endl;
    for(set<TextQuery::line_no>::iterator iter = s_lno.begin();
                                          iter != s_lno.end(); ++iter) {
        cout << "(line " << *iter << ") ";
        cout << tq.text_line(*iter) <<endl;
    }

    return 0;
}