WordSearch : parses a file and finds word associations

Download wordsearch.zip

Synopsis:

Mediator.cpp
Mediator.h
Misc.cpp
misc.h
Model.cpp
Model.h
TextUI.cpp
TextUI.h
TokenStream.cpp
TokenStream.h
TokenWindow.cpp
TokenWindow.h
WordMap.cpp
WordMap.h
wordsearch.cpp
wordsearch.h


Mediator.cpp

Synopsis
#include "wordsearch.h"

#include "Mediator.h"
#include "TextUI.h"
#include "Model.h"

//--------------------------------------------
class MediatorImpl : public Mediator
  {
  public:
    void DisplayError(const string& err);
    ostream& getOutStream();
    void run();
    void Dispatch(char cmd, const string& parm1, const string& parm2);
    
  private:
    TextUI* tui;
    Model* model;
    
    friend Mediator* createMediator();
    MediatorImpl();
  } ;

//--------------------------------------------
Mediator* createMediator()
  {
  return new MediatorImpl();
  }

//--------------------------------------------
void MediatorImpl::Dispatch(char cmd, const string& parm1, const string& parm2)
  {
  model->Dispatch(cmd, parm1, parm2);
  }

//--------------------------------------------
void MediatorImpl::DisplayError(const string& err)
  {
  tui->DisplayError(err);
  }

//--------------------------------------------
ostream& MediatorImpl::getOutStream()
  {
  return tui->getOutStream();
  }

//--------------------------------------------
void MediatorImpl::run()
  {
  while(tui->GetMessage())
    {
    tui->Dispatch();
    }
  }

//--------------------------------------------
MediatorImpl::MediatorImpl()
  {
  tui = new TextUI(*this);
  model = createModel(*this);
  }

Mediator.h

Synopsis
#pragma once

#include <iostream>
#include <string>
using std::string;
using std::ostream;

//--------------------------------------------
class Mediator
  {
  public:
    virtual void run() = 0;
    virtual void Dispatch(char cmd, const string& parm1, const string& parm2) = 0;
    virtual ostream& getOutStream() = 0;
    virtual void DisplayError(const string& str) = 0;
  } ;

Mediator* createMediator();

Misc.cpp

Synopsis
#include "wordsearch.h"
#include "Misc.h"

ostream& operator <<(ostream& os, const wordassoc& wa)
  {
  os << "'" << wa.first << "'"
    << " --> " 
    << "'" << wa.second << "'" ;
  return os;
  }


misc.h

Synopsis
#pragma once

#include <iostream>
using std::ostream;

ostream& operator <<(ostream& os, const wordassoc& wa);


Model.cpp

Synopsis
#include "wordsearch.h"
#include "Model.h"
#include "Mediator.h"
#include "WordMap.h"

//--------------------------------------------
class ModelImpl : public Model
  {
  public:
    ModelImpl(Mediator& m);
    void Dispatch(char cmd, const string& parm1, const string& parm2);
    void load(const string& name);
    void print();
    
  private:
    ModelImpl();
    
    Mediator& mediator;
    WordMap words;
  } ;

//--------------------------------------------
Model* createModel(Mediator& m)
  {
  return new ModelImpl(m);
  }

//--------------------------------------------
ModelImpl::ModelImpl(Mediator& m)
: mediator(m)
  {
  }

//--------------------------------------------
void ModelImpl::Dispatch(char cmd, const string& parm1, const string& parm2)
  {
  cmd = tolower(cmd);
  switch(cmd)
    {
    case 'l' : load(parm1); break;
    case 'p' : print(); break;
    default:
      mediator.DisplayError("Unknown command!");
    }
  }

//--------------------------------------------
void ModelImpl::load(const string& name)
  {
  if (words.load(name.c_str()))
    return;

  mediator.DisplayError("Error reading file");
  }

//--------------------------------------------
void ModelImpl::print()
  {
  words.print(mediator.getOutStream());
  }

Model.h

Synopsis
#pragma once

#include <string>
using std::string;

class Mediator;

//--------------------------------------------
class Model
  {
  public:
    virtual void Dispatch(char cmd, const string& parm1, const string& parm2) = 0;
  } ;

Model* createModel(Mediator& m);

TextUI.cpp

Synopsis
#include "wordsearch.h"

#include "TextUI.h"
#include "Mediator.h"
using namespace std;

//--------------------------------------------
TextUI::TextUI(Mediator& m)
: mediator(m)
  {
  Clear();
  Prompt();
  }

//--------------------------------------------
void TextUI::DisplayError(const string& err)
  {
  cout << err << endl;
  }

//--------------------------------------------
bool TextUI::GetMessage()
  {
  while(GetInput())
    Prompt();
  
  return cmd != 'q';
  }


//--------------------------------------------
void TextUI::Dispatch()
  {
  Clear();
  mediator.Dispatch(cmd, parm1, parm2);
  Prompt();
  }

//--------------------------------------------
ostream& TextUI::getOutStream()
  {
  return cout;
  }

//--------------------------------------------
void TextUI::Clear()
  {
  system("cls");
  }

//--------------------------------------------
void TextUI::Prompt()
  {
  cout << "Enter a command: " << endl;
  }

//--------------------------------------------
bool TextUI::GetInput()
  {
  char str[260];
  cin.getline(str, sizeof(str), '\n');
  
  string cmdstr;
  int tokcount = 0;
  for(char* p = strtok(str, " \t"); p; p = strtok(0, " \t"))
    {
    if (tokcount == 0)
      cmdstr = p;
    else if (tokcount == 1)
      parm1 = p;
    else if (tokcount == 2)
      parm2 = p;
    else
      {
      DisplayError("Too many parameters!");
      return true;
      }
    tokcount++;
    }
  
  if (cmdstr.length() > 1)
    {
    DisplayError("A command is one char!");
    return true;
    }
  
  cmd = cmdstr[0];
  return false;
  }

TextUI.h

Synopsis
#pragma once

#include <iostream>
using std::ostream;

class Mediator;

//--------------------------------------------
class TextUI
  {
  public:
    TextUI(Mediator& m);
    void DisplayError(const string& err);
    ostream& getOutStream();
    bool GetMessage();
    void Dispatch();

  private:
    TextUI();
    
    void Clear();
    void Prompt();
    bool GetInput();
    
  private:
    Mediator& mediator;
    
    string parm1;
    string parm2;
    char cmd;
  } ;


TokenStream.cpp

Synopsis
#include "wordsearch.h"
#include "TokenStream.h"
#include "TokenWindow.h"

class TokenStream::Private
  {
  public:
    TokenWindow tokwin;
    ifstream in;
  } ;

//--------------------------------------------
TokenStream::TokenStream(const string& fname)
: impl(* new TokenStream::Private())
  {
  impl.in.open(fname.c_str());
  }

//--------------------------------------------
TokenStream::~TokenStream()
  {
  impl.in.close();
  }

//--------------------------------------------
bool TokenStream::bad()
  {
  return impl.in.bad() || impl.in.fail();
  }

//--------------------------------------------
bool TokenStream::IsMore()
  {
  if (bad()) 
    return false;

  while(!impl.in.eof())
    {
    impl.tokwin.load(impl.in);
    
    if (impl.tokwin.isReady())
      return true;
    }
  
  return ! bad();
  }

//--------------------------------------------
wordtuple TokenStream::getForeTuple()
  {
  return impl.tokwin.getForeTuple();
  }

//--------------------------------------------
wordtuple TokenStream::getBackTuple()
  {
  return impl.tokwin.getBackTuple();
  }

TokenStream.h

Synopsis
#pragma once

#include <iostream>
using std::string;

//--------------------------------------------
class TokenStream
  {
  public:
    TokenStream(const string& fname);
    ~TokenStream();
    bool IsMore();
    bool bad();
    wordtuple getForeTuple();
    wordtuple getBackTuple();
    
  private:
    TokenStream();

    class Private;
    Private& impl;
  } ;


TokenWindow.cpp

Synopsis
#include "wordsearch.h"
#include "TokenWindow.h"
using namespace std;

//--------------------------------------------
static bool ispunc(char c)
  {
  return c == '.' ||
         c == ';' ||
         c == ',' || 
         c == '*' || 
         c == '\'' || 
         c == '(' || 
         c == ')' || 
         c == ':' || 
         c == '"';
  }

//--------------------------------------------
void TokenWindow::load(ifstream& in)
  {
  if (nexttok != "")
    {
    prevtok = curtok;
    curtok = nexttok;
    }
  in >> nexttok;
  if (in.eof()) 
    {
    nexttok = "";
    return;
    }

  string::iterator it;
  for(it = nexttok.begin(); it != nexttok.end(); ++it)
    {
    (*it) = tolower(*it);
    }

  for (it = nexttok.begin(); it != nexttok.end() && ispunc(*it); it++)
    nexttok.erase(it);

  for (string::reverse_iterator rit = nexttok.rbegin(); rit != nexttok.rend() &&  ispunc(*rit); rit++)
    (*rit) = 0;

  if (nexttok == "a" ||
      nexttok == "i" ||
      nexttok == "is" ||
      nexttok == "an" ||
      nexttok == "as" ||
      nexttok == "it" ||
      nexttok == "to" ||
      nexttok == "so" ||
      nexttok == "of" ||
      nexttok == "on" ||
      nexttok == "at" ||
      nexttok == "be" ||
      nexttok == "let" ||
      nexttok == "you" ||
      nexttok == "for" ||
      nexttok == "but" ||
      nexttok == "are" ||
      nexttok == "can" ||
      nexttok == "was" ||
      nexttok == "that" ||
      nexttok == "the"
      )
      nexttok = "";

  }

//--------------------------------------------
bool TokenWindow::isReady()
  {
  return curtok != "" && nexttok != "";
  }

//--------------------------------------------
wordtuple TokenWindow::getForeTuple()
  {
  if (atEnd())
    return zerotuple;
  
  return wordtuple(wordassoc(curtok, nexttok), 3);
  }

//--------------------------------------------
wordtuple TokenWindow::getBackTuple()
  {
  if (atBegin())
    return zerotuple;
  
  //return wordtuple(wordassoc(curtok, prevtok), 2);
  //return wordtuple(wordassoc(prevtok, curtok), 2);
  return zerotuple;
  }

//--------------------------------------------
bool TokenWindow::atEnd()
  {
  return nexttok == "";
  }

//--------------------------------------------
bool TokenWindow::atBegin()
  {
  return prevtok == "";
  }

TokenWindow.h

Synopsis
#pragma once

#include <fstream>
using std::ifstream;

//--------------------------------------------
class TokenWindow
  {
  public:
    void load(ifstream& in);
    bool isReady();
    wordtuple getForeTuple();
    wordtuple getBackTuple();
    
  private:
    bool atEnd();
    bool atBegin();
    
    string prevtok;
    string curtok;
    string nexttok;
  } ;


WordMap.cpp

Synopsis
#include "wordsearch.h"
#include "WordMap.h"
#include "TokenStream.h"
#include "Misc.h"
using namespace std;

//--------------------------------------------
bool WordMap::load(const string& fname)
  {
  TokenStream ts(fname.c_str());
  if (ts.bad())
    return false;

  while(ts.IsMore())
    {
    add(ts.getForeTuple());
    add(ts.getBackTuple());
    }

  return ts.bad();
  }

//--------------------------------------------
void WordMap::print(ostream& os)
  {
  for (Iterator it = begin(); it != end(); ++it)
    {
    os << (*it).first 
      << " ( " << (*it).second << " )"
      << endl;
    }
  }

//--------------------------------------------
void WordMap::add(const wordtuple& wt)
  {
  if (wt == zerotuple)
    return;
  
  Iterator it = find(wt.first);
  if (it == end())
    {
    insert(wt);
    }
  else
    {
    (*it).second += wt.second;
    }
  }

WordMap.h

Synopsis
#pragma once

#include <iostream>
using std::ostream;

//--------------------------------------------
class WordMap : private wordmap
  {
  public:
    typedef wordmap::iterator Iterator;
    
    bool load(const string& fname);
    void print(ostream& os);
    
  private:
    void add(const wordtuple& wt);
  } ;

wordsearch.cpp

Synopsis
// codetest.cpp : Defines the entry point for the console application.
#include "Mediator.h"

//--------------------------------------------
int main(int argc, char* argv[])
  {
  Mediator* mediator = createMediator();
  mediator->run();
  
  return 0;
  }


wordsearch.h

Synopsis
#pragma once
#pragma warning(disable: 4786)
#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers

#include <string>
#include <map>
using std::map;
using std::pair;
using std::string;

typedef pair<string, string> wordassoc;
typedef pair<wordassoc, long> wordtuple;
typedef map<wordassoc,  long> wordmap;
const wordtuple zerotuple(wordassoc("", ""), 0);








Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010