view main.c++ @ 2:0748d902784c

Fix off-by-ones, don't use std globally
author Jordi Gutiérrez Hermoso <jordigh@gmail.com>
date Mon, 27 Dec 2010 13:28:19 -0600
parents 60954ea75cce
children 56c58a177cde
line wrap: on
line source

#include "levenshtein.c++"

#include <fstream>
#include <sstream>
#include <algorithm>

void string_to_upper(std::string& str)
{
  std::transform( str.begin(), str.end(), str.begin(),  &toupper);
}

int main()
{
  using namespace std;
  ifstream ifs("twl06.txt");
  string word;
  vector<string> wl;
  while(ifs >> word)
  {
    wl.push_back(word);
  }

  string phrase = "tihs sententcnes iss nout varrry goud";
  stringstream ss; ss << phrase;
  size_t total_distance = 0;
  while( ss >> word)
  {
    string_to_upper(word);
    size_t m = 50;
    for(size_t i = 0; i < wl.size(); i++)
    {
      m = min(levenshtein(wl[i],word,m), m);
      if(m == 0)
        break;
    }
    total_distance += m;
  }
  cout << total_distance << endl;
}