Difference: ShakespeareStart2016 (1 vs. 8)

Revision 82018-04-26 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Shakespeare in Class file activity

Getting ready for the final project - viewing and search the text.

Revision 72017-11-28 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Shakespeare in Class file activity

Getting ready for the final project - viewing and search the text.
Line: 29 to 29
  int main() {
Changed:
<
<
ifstream skakefile;
>
>
ifstream shakefile;
 
Changed:
<
<
skakefile.open(SHAKEFILE); if (skakefile.fail()) {
>
>
shakefile.open(SHAKEFILE); if (shakefile.fail()) {
  cout << "Couldn't open: " << SHAKEFILE << endl; exit(1); }

Revision 62017-11-28 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Shakespeare in Class file activity

Getting ready for the final project - viewing and search the text.
Line: 8 to 8
 What we will do in this hands on activity is to set up a small program that reads in portions of Shakespeare, and displays it on the screen.

Step 1 - files and project setup

Changed:
<
<
  1. Create a C++ project "ssreader" on your system in Netbeans.
>
>
  1. Create a C++ project "demoSSReader" on your system in Netbeans.
 
  1. Download "Shakespeare.txt" file by right clicking here: Shakespeare.txt and saving it to your system.
Changed:
<
<
  1. Copy the file into the top directory of your project (this will likely be "yourhomedir/NetBeansProjects/ssreader")

Step 2 - Program to read Shakespeare.

  • Consider the following code to create a input file stream, and open it to the Shakespeare file. Add this to your project main function,
>
>
  1. Copy the file into the top directory of your project (this will likely be "yourhomedir/NetBeansProjects/demoSSReader")

Step 2 - Simple Program to read Shakespeare into a vector of lines.

  • Consider the following code to create a input file stream, and open it to the Shakespeare file. Add this to your project main function. It also includes code we developed for the Poem lab.
 %CODE{"c++"}%
Deleted:
<
<
#include
 #include
Added:
>
>
#include
 #include
Added:
>
>
#include #include
  using namespace std;
Added:
>
>
const string SHAKEFILE = "shakespeare.txt";
 
Changed:
<
<
ifstream ssfile;
>
>
vector getWords(string line); string RemoveSpecials(string str);
 
Changed:
<
<
// Open the file ssfile.open("Shakespeare.txt"); if (ssfile.is_open()) { cout << "File not found:" << filename << " Goodbye!!" << endl;
>
>
int main() {

ifstream skakefile;

skakefile.open(SHAKEFILE); if (skakefile.fail()) { cout << "Couldn't open: " << SHAKEFILE << endl; exit(1); } //write code to search for matches

  return 0; }
Changed:
<
<
%ENDCODE%
  • Add a loop to read each line, display it, and ask if the user wants to continue.
%CODE{"c++"}% char done;
>
>
vector getWords(string line) { istringstream iss(line); vector words;
  string s; do {
Changed:
<
<
getline(ssfile, s); cout << s << endl;

cout << "Enter \"c\" to continue, anything else to stop:"; cin >> done;

} while (ssfile.eof() && done == 'c'); %ENDCODE%

Step 3 - Make the program count words

Let's create a function that given an open file stream and a word in a string, runs through a file, counting the number number time that word is in the file.

Consider the following c++string function "find". It's documented here.

Lets start by writing a function "countWords":

<-- SyntaxHighlightingPlugin -->
int countWords(istream& is, string word);
<-- end SyntaxHighlightingPlugin -->

Search for boat. search for king. It might be nice to print out the lines where you find matches.

>
>
iss >> s; if (s.length() > 0 && s = " ") { words.push_back(s); //cout << s << " "; } s=""; } while (iss); return words;
 
Changed:
<
<
Does your program account for the same word more than once in a line? Print out each line as you find it.
>
>
}
 
Changed:
<
<
Try to make it count EVERY occurrence of the word. %CODE{"c++"}% int countWords(istream& is, string word){ int total = 0; string s; while (is.eof()) { getline(is, s); size_t pos = s.find(word); while (pos = string::npos) { cout << pos << ":" << s << endl; total++; pos = s.find(word,pos+1);
>
>
string RemoveSpecials(string str) { int i = 0, len = str.length(); while (i < len) { char c = str[i]; if (((c >= '0')&&(c <= '9')) || ((c >= 'A')&&(c <= 'Z')) || ((c >= 'a')&&(c <= 'z')) || c = '\'' || c = ' ') { if ((c >= 'A')&&(c <= 'Z')) str[i] += 32; //Assuming dictionary contains small letters only. ++i; } else { str.erase(i, 1); --len;
  } }
Changed:
<
<
return total;

>
>
return str;
 } %ENDCODE%
Deleted:
<
<

Step 4 - Loop the program

 
Changed:
<
<
Modify code to allow user to loop repeated search for different words.
>
>
Write code to read each line and push it into a vector of strings. Test it out. How many lines are there?

Step 3 - Make the program count words

Let's add functionality to ask for a word, and display the lines with the matching word. Also display the number of matching words

 
Changed:
<
<
Problem - file is at the end! What can we do?
  1. First must clear error condition (EOF)
  2. Then must move the "file pointer" back to start.
>
>
Search for Castle. Search for king. It might be nice to print out the lines where you find matches.
 
Deleted:
<
<
%CODE{"c++"}% inputFile.clear(); inputFile.seekg(0, ios::beg);
 
Changed:
<
<
%ENDCODE%</sticky>
>
>

Step 4 - Search Book titles

Can you count the number of books, and print the titles
 
Changed:
<
<

Step 5 - Search for phrases

>
>

Step 5 - Search Book ends

What line is each on?
 
Changed:
<
<
What do we need to change to search for a phrase rather than a word?

Step 6 - Additional iteas

  • Print out the names of all the books in the file.
  • Ask for a word, and only display paragraphs with that word.
  • Ask for a word, tell user how many occurrences of it there are in the words.</sticky>
>
>

Step 6 - Additional ideas

Can you search for a phrase?
 
META TOPICMOVED by="JimSkon" date="1479329793" from="SCMP118/Fall2016.ShakesspeareStart2016" to="SCMP118/Fall2016.ShakespeareStart2016"

Revision 52017-11-28 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Shakespeare in Class file activity

Getting ready for the final project - viewing and search the text.
Line: 89 to 89
  %CODE{"c++"}% inputFile.clear(); inputFile.seekg(0, ios::beg);
Changed:
<
<
%ENDCODE%
>
>
%ENDCODE%</sticky>
 

Step 5 - Search for phrases

What do we need to change to search for a phrase rather than a word?

Changed:
<
<

Step 6 - Addtional iteas

>
>

Step 6 - Additional iteas

 
  • Print out the names of all the books in the file.
  • Ask for a word, and only display paragraphs with that word.
  • Ask for a word, tell user how many occurrences of it there are in the words.</sticky>

Revision 42016-11-17 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Shakespeare in Class file activity

Getting ready for the final project - viewing and search the text.
Line: 61 to 61
 Does your program account for the same word more than once in a line? Print out each line as you find it.

Try to make it count EVERY occurrence of the word.

Added:
>
>
%CODE{"c++"}% int countWords(istream& is, string word){ int total = 0; string s; while (is.eof()) { getline(is, s); size_t pos = s.find(word); while (pos = string::npos) { cout << pos << ":" << s << endl; total++; pos = s.find(word,pos+1); } } return total;
 
Added:
>
>
} %ENDCODE%
 

Step 4 - Loop the program

Modify code to allow user to loop repeated search for different words.

Line: 72 to 89
  %CODE{"c++"}% inputFile.clear(); inputFile.seekg(0, ios::beg);
Changed:
<
<

%ENDCODE%</sticky>
>
>
%ENDCODE%
 

Step 5 - Search for phrases

Revision 32016-11-17 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Shakespeare in Class file activity

Getting ready for the final project - viewing and search the text.
Line: 14 to 14
 

Step 2 - Program to read Shakespeare.

  • Consider the following code to create a input file stream, and open it to the Shakespeare file. Add this to your project main function,
%CODE{"c++"}%
Changed:
<
<
string filename = "Shakespeare.txt";
>
>
#include #include #include

using namespace std;

  ifstream ssfile;

// Open the file

Changed:
<
<
ssfile.open(filename);
>
>
ssfile.open("Shakespeare.txt");
  if (ssfile.is_open()) { cout << "File not found:" << filename << " Goodbye!!" << endl; return 0;
Line: 30 to 35
  char done; string s; do {
Deleted:
<
<
while (ssfile.eof()) {
  getline(ssfile, s); cout << s << endl;

cout << "Enter \"c\" to continue, anything else to stop:"; cin >> done;

Changed:
<
<
}
>
>
  } while (ssfile.eof() && done == 'c'); %ENDCODE%

Revision 22016-11-17 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"
Changed:
<
<

Shakespeare in Class activity

Getting ready for the final project - viewing the text.
>
>

Shakespeare in Class file activity

Getting ready for the final project - viewing and search the text.
  The goal here is to get everyone to step one on the final project. This project involved building a object orient c++ program that allows you to search for words in the complete works of Shakespeare.
Line: 12 to 12
 
  1. Download "Shakespeare.txt" file by right clicking here: Shakespeare.txt and saving it to your system.
  2. Copy the file into the top directory of your project (this will likely be "yourhomedir/NetBeansProjects/ssreader")

Step 2 - Program to read Shakespeare.

Changed:
<
<
  • Add the following function to allow your project to read text, search for paragraph ends (two newlines in a row).
>
>
  • Consider the following code to create a input file stream, and open it to the Shakespeare file. Add this to your project main function,
 %CODE{"c++"}%
Deleted:
<
<
string readParagraph(istream & is) { string line; string paragraph; int lineNum = 0; ;

//scan for the next paragraph do { getline(is, line); } while (line.length() == 0 && is.eof());

// return nothing if eof if (is.eof()) { return ""; } // Get the next paragraph do { // Only put a newline after first line if (lineNum++ > 0) { paragraph += "\n"; } paragraph += line; getline(is, line); } while (line.length() > 0 && is.eof());

return paragraph; } %ENDCODE%

  • Add code to main.cpp to open the Shakespeare file.

%CODE{"c++"}%

  string filename = "Shakespeare.txt"; ifstream ssfile;
Line: 59 to 24
  return 0; } %ENDCODE%
Changed:
<
<
  • Add a loop to read each paragraph, display it, and ask if the user wants to continue.
>
>
  • Add a loop to read each line, display it, and ask if the user wants to continue.
  %CODE{"c++"}%
Deleted:
<
<
int main() { string filename = "Shakespeare.txt"; ifstream ssfile;

// Open the file ssfile.open(filename);

if (ssfile.is_open()) { cout << "File not found:" << filename << " Goodbye!!" << endl; return 0; }

  char done;
Added:
>
>
string s;
  do { while (ssfile.eof()) {
Changed:
<
<
string s = readParagraph(ssfile);
>
>
getline(ssfile, s);
  cout << s << endl;

cout << "Enter \"c\" to continue, anything else to stop:"; cin >> done; } } while (ssfile.eof() && done == 'c');

Added:
>
>
%ENDCODE%
 
Added:
>
>

Step 3 - Make the program count words

 
Changed:
<
<
return 0; }
>
>
Let's create a function that given an open file stream and a word in a string, runs through a file, counting the number number time that word is in the file.

Consider the following c++string function "find". It's documented here.

Lets start by writing a function "countWords": %CODE{"c++"}% int countWords(istream& is, string word);

 %ENDCODE%
Changed:
<
<

Step 3 - Add change a features

>
>
Search for boat. search for king. It might be nice to print out the lines where you find matches.

Does your program account for the same word more than once in a line? Print out each line as you find it.

Try to make it count EVERY occurrence of the word.

Step 4 - Loop the program

Modify code to allow user to loop repeated search for different words.

Problem - file is at the end! What can we do?

  1. First must clear error condition (EOF)
  2. Then must move the "file pointer" back to start.

<-- SyntaxHighlightingPlugin -->
inputFile.clear(); inputFile.seekg(0, ios::beg);

<br />
<-- end SyntaxHighlightingPlugin -->
</sticky>

Step 5 - Search for phrases

What do we need to change to search for a phrase rather than a word?

Step 6 - Addtional iteas

 
  • Print out the names of all the books in the file.
  • Ask for a word, and only display paragraphs with that word.
Changed:
<
<
  • Ask for a word, tell user how many occurrences of it there are in the words.
>
>
  • Ask for a word, tell user how many occurrences of it there are in the words.</sticky>
 
META TOPICMOVED by="JimSkon" date="1479329793" from="SCMP118/Fall2016.ShakesspeareStart2016" to="SCMP118/Fall2016.ShakespeareStart2016"

Revision 12016-11-16 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

Shakespeare in Class activity

Getting ready for the final project - viewing the text.

The goal here is to get everyone to step one on the final project. This project involved building a object orient c++ program that allows you to search for words in the complete works of Shakespeare.

What we will do in this hands on activity is to set up a small program that reads in portions of Shakespeare, and displays it on the screen.

Step 1 - files and project setup

  1. Create a C++ project "ssreader" on your system in Netbeans.
  2. Download "Shakespeare.txt" file by right clicking here: Shakespeare.txt and saving it to your system.
  3. Copy the file into the top directory of your project (this will likely be "yourhomedir/NetBeansProjects/ssreader")

Step 2 - Program to read Shakespeare.

  • Add the following function to allow your project to read text, search for paragraph ends (two newlines in a row).
<-- SyntaxHighlightingPlugin -->
string readParagraph(istream & is) {
        string line;
        string paragraph;
        int lineNum = 0;
        ;

        //scan for the next paragraph
        do {
            getline(is, line);
        } while (line.length() == 0 && !is.eof());

        // return nothing if eof
        if (is.eof()) {
            return "";
        }
        // Get the next paragraph
        do {
            // Only put a newline after first line
            if (lineNum++ > 0) {
                paragraph += "\n";
            }
            paragraph += line;
            getline(is, line);
        } while (line.length() > 0 && !is.eof());

        return paragraph;
    }
<-- end SyntaxHighlightingPlugin -->

  • Add code to main.cpp to open the Shakespeare file.

<-- SyntaxHighlightingPlugin -->
string filename = "Shakespeare.txt";
    ifstream ssfile;

    // Open the file
    ssfile.open(filename);

    if (!ssfile.is_open()) {
        cout << "File not found:" << filename << " Goodbye!!" << endl;
        return 0;
    }
<-- end SyntaxHighlightingPlugin -->

  • Add a loop to read each paragraph, display it, and ask if the user wants to continue.

<-- SyntaxHighlightingPlugin -->
int main() {
    string filename = "Shakespeare.txt";
    ifstream ssfile;

    // Open the file
    ssfile.open(filename);

    if (!ssfile.is_open()) {
        cout << "File not found:" << filename << " Goodbye!!" << endl;
        return 0;
    }
    char done;
    do {
        while (!ssfile.eof()) {
            string s = readParagraph(ssfile);
            cout << s << endl;

            cout << "Enter \"c\" to continue, anything else to stop:";
            cin >> done;
        }
    } while (!ssfile.eof() && done == 'c');


    return 0;
}
<-- end SyntaxHighlightingPlugin -->

Step 3 - Add change a features

  • Print out the names of all the books in the file.
  • Ask for a word, and only display paragraphs with that word.
  • Ask for a word, tell user how many occurrences of it there are in the words.

META TOPICMOVED by="JimSkon" date="1479329793" from="SCMP118/Fall2016.ShakesspeareStart2016" to="SCMP118/Fall2016.ShakespeareStart2016"
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback