Difference: ShakespeareStart2016 (5 vs. 6)

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"
 
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