you will be implementing a Lexicon class. This Lexicon class will maintain a lis
ID: 3728083 • Letter: Y
Question
you will be implementing a Lexicon class. This Lexicon class will maintain a list of words, and the user will be able to query the Lexicon as to whether or not a word is in the Lexicon, the user will be able to add words to the Lexicon, or to modify a word in the Lexicon. Constructors: Lexicon(); A constructor that allows the user to create an empty Lexicon: Lexicon(const string& fileName); A constructor that opens the file associated with fileName, and stores all of the words in the file in the Lexicon. If the file doesn’t exist, an exception should be thrown. (N.B. The ifstream constructor takes a C-style string as a parameter to open the file and not a C++ string object. To get the C-string equivalent of any given C++ string, use the member function c_str(). For example, to get the C-string equivalent of fileName, call fileName.c_str().) Operations: bool containsWord(const string& word); returns true if word is in the Lexicon, and false otherwise. list startWith(char ch); returns a list a of words that start with a particular letter, ch. list startWith(const string& p returns a list of words that start with prefix. list endWith(char ch); returns a list of words that end with a particular character, ch. list endWith(const string& suffix); returns a list of words that end with suffix. void addWord(const string& str); Adds str to the Lexicon if it is not there already. If it is there already, do nothing. void updateWord(const string& target, const string& replacement); Changes the spelling of target to become replacement. Operators: friend bool operator==(const Lexicon& d1, const Lexicon& d2); returns true iff d1 and d2 contain the same words, not necessarily in the same order. (N.B. be careful here. You can not just assume that you can compare the lexicons word by word, since they may not be stored in the same order.) friend operator <<(ostream& os, const Lexicon& d); Outputs the content of the Lexicon (in any order). Please submit Lexicon.h, Lexicon.cpp, and useLexicon.cpp files. Also, please use comments to document your code. These homeworks will become more and more complicated, and if I can’t figure out what your intuition is in solving these problems, I can’t give credit. I have provided 2 text files with the same lexicon, but I ran a program that has randomly permuted the words such that they are no longer listed in sorted order. When these files have been stored into Lexicon objects, your == function should return true.
TEXT1
-----------------------------------
TEXT2
-----------------------------------------------------
Explanation / Answer
#ifndef _lexicon_h
#define _lexicon_h
#include <string>
#include "FOR.cpp"
#include "set.h"
#include "stack.h"
/*
* Class: Lexicon
*/
#include <cctype>
class Lexicon {
public:
/*
* Constructor: Lexicon
* Usage: Lexicon lex;
* Lexicon lex(filename);
* -----------------------------
* Initializes a new lexicon. The default constructor creates an empty
* lexicon. The second form reads in the contents of the lexicon from
* the specified data file. The data file must be in one of two formats:
* (1) a space-efficient precompiled binary format or (2) a text file
* containing one word per line. The Stanford library distribution
* includes a binary lexicon file named <code>English.dat</code>
* containing a list of words in English. The standard code pattern
* to initialize that lexicon looks like this:
*
*<pre>
* Lexicon english("English.dat");
*</pre>
*/
Lexicon();
Lexicon(std::string filename);
/*
* Destructor: ~Lexicon
* Usage: (usually implicit)
* -------------------------
* The destructor deallocates any storage associated with the lexicon.
*/
~Lexicon();
/*
* Method: size
* Usage: int n = lex.size();
* --------------------------
* Returns the number of words contained in the lexicon.
*/
int size() const;
/*
* Method: isEmpty
* Usage: if (lex.isEmpty()) . . .
* -------------------------------
* Returns <code>true</code> if the lexicon contains no words.
*/
bool isEmpty() const;
/*
* Method: clear
* Usage: lex.clear();
* -------------------
* Removes all words from the lexicon.
*/
void clear();
/*
* Method: add
* Usage: lex.add(word);
* ---------------------
* Adds the specified word to the lexicon.
*/
void add(std::string word);
/*
* Method: addWordsFromFile
* Usage: lex.addWordsFromFile(filename);
* --------------------------------------
* Reads the file and adds all of its words to the lexicon.
*/
void addWordsFromFile(std::string filename);
/*
* Method: contains
* Usage: if (lex.contains(word)) . . .
* ------------------------------------
* Returns <code>true</code> if <code>word</code> is contained in the
* lexicon. In the <code>Lexicon</code> class, the case of letters is
* ignored, so "Zoo" is the same as "ZOO" or "zoo".
*/
bool contains(std::string word) const;
/*
* Method: containsPrefix
* Usage: if (lex.containsPrefix(prefix)) . . .
* --------------------------------------------
* Returns true if any words in the lexicon begin with <code>prefix</code>.
* Like <code>containsWord</code>, this method ignores the case of letters
* so that "MO" is a prefix of "monkey" or "Monday".
*/
bool containsPrefix(std::string prefix) const;
/*
* Macro: foreach
* Usage: foreach (string word in lexicon) . . .
* ---------------------------------------------
* Iterates over the words in the lexicon in alphabetical order.
*/
/* The foreach macro is defined in foreach.h */
/*
* Method: mapAll
* Usage: lexicon.mapAll(fn);
* lexicon.mapAll(fn, data);
* --------------------------------
* Calls the specified function on each word in the lexicon. The second
* form of the call allows the client to pass a data value of any type
* to the callback function.
*/
void mapAll(void (*fn)(std::string value));
template <typename ClientDataType>
void mapAll(void (*fn)(std::string value, ClientDataType & data),
ClientDataType & data);
#include "private/lexiconpriv.h"
};
#include "private/lexiconimpl.cpp"
#endif
/*
* File: foreach.h
* ---------------
* This interface defines the <code>foreach</code> keyword, which is
* used to simplify iteration. All iterable classes import this
* interface, so clients never need to do so explicitly. This version
* of the interface also supports C++ strings and arrays.
*/
#ifndef _foreach_h
#define _foreach_h
/*
* Statement: foreach
* Usage: foreach (type var in collection) { . . . }
* -------------------------------------------------
* The <code>foreach</code> statement steps through the elements in
* a collection. It works correctly with the collection classes in
* both the Standard Template Library and the Stanford C++ libraries,
* but can also be used with C++ strings and statically initialized
* arrays.
*
* <p>The following code, for example, prints every element in the
* string vector <code>lines</code>:
*
*<pre>
* foreach (string str in lines) {
* cout << str << endl;
* }
*</pre>
*
* Similarly, the following function calculates the sum of the character
* codes in a string:
*
*<pre>
* int sumCharacterCodes(string str) {
* int sum = 0;
* foreach (char ch in str) sum += ch;
* return sum;
* }
*</pre>
*
* As a simplification when iterating over maps, the <code>foreach</code>
* macro iterates through the keys rather than the key/value pairs.
*/
/* The foreach and in macros are defined in the foreachpriv.h file */
#include "private/foreachpriv.h"
#endif
/*main program*/
#include <iostream>
#include "console.h"
#include "lexicon.h"
#include "queue.h"
#include "simpio.h"
#include "vector.h"
using namespace std;
int main() {
// [TODO: fill in the code]
Lexicon eng("EnglishWords.dat");
foreach(string word in eng)
cout << word;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.