Index: src/cor/Makefile
===================================================================
--- src/cor/Makefile	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
+++ src/cor/Makefile	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
@@ -0,0 +1,56 @@
+include ../../config.mak
+
+ifeq ($(BUILD_STATIC), yes)
+  LDFLAGS += -static
+endif
+
+LDFLAGS +=
+CXXFLAGS += -O2 -fpermissive
+
+LIB_PATH=../lib
+COMMON_PATH=../common
+CMDLINE_FILE='"../cor/cmdline.h"'
+
+all: cor
+
+cor: main.cc corr.o $(LIB_PATH)/word.o \
+	$(LIB_PATH)/auttools.o cmdline.c common_cor.o common.o 
+	$(CXX) $(CXXFLAGS) -D _CMDLINE_FILE=$(CMDLINE_FILE) main.cc corr.o common.o \
+	$(LIB_PATH)/word.o $(LIB_PATH)/auttools.o cmdline.c common_cor.o \
+	-o cor $(LDFLAGS)
+
+corr.o: corr.cc corr.hh
+	$(CXX) $(CXXFLAGS) -c corr.cc
+
+common.o: $(COMMON_PATH)/cmdline_common.ggo $(COMMON_PATH)/common.cc \
+	 $(COMMON_PATH)/common.h
+	$(CXX) $(CXXFLAGS) -c -D _CMDLINE_FILE=$(CMDLINE_FILE) $(COMMON_PATH)/common.cc
+
+common_cor.o: cmdline.h common_cor.cc common_cor.h
+	$(CXX) $(CXXFLAGS) -c -D _CMDLINE_FILE=$(CMDLINE_FILE) common_cor.cc
+
+cmdline.c cmdline.h: cmdline.ggo
+	$(GENGETOPT) -i cmdline.ggo --conf-parser
+
+cmdline.ggo: cmdline_cor.ggo $(COMMON_PATH)/cmdline_common.ggo
+	cat cmdline_cor.ggo $(COMMON_PATH)/cmdline_common.ggo > cmdline.ggo
+
+.PHONY: install
+install:
+ifdef BIN_DIR
+	install -m 0755 cor $(BIN_DIR)
+endif
+
+.PHONY: uninstall
+uninstall:
+ifdef BIN_DIR
+	rm $(BIN_DIR)/cor
+endif
+
+clean: clean.cmdline
+	rm *.o || true
+	rm cor || true
+
+clean.cmdline:
+	rm cmdline.* || true
+
Index: src/cor/cmdline_cor.ggo
===================================================================
--- src/cor/cmdline_cor.ggo	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
+++ src/cor/cmdline_cor.ggo	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
@@ -0,0 +1,8 @@
+package "cor"
+version "0.1"
+
+option "dictionary-home"	-	"Dictionary home dir." string typestr="FILENAME" no hidden
+option "dictionary"		d	"Dictionary" string typestr="FILENAME" default="cor.bin" no
+option "distance"		n	"Maximal edit distance." int default="1" no
+option "replace"		r	"Replace original form with corrected form, place original form in the cor field. This option has no effect in single mode" flag off hidden
+#option "single"			-	"Place all alternatives in the same line" flag off
Index: src/cor/common_cor.cc
===================================================================
--- src/cor/common_cor.cc	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
+++ src/cor/common_cor.cc	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
@@ -0,0 +1,29 @@
+#include <stdlib.h>
+#include <string.h>
+#include "common_cor.h"
+
+char dictionary[256];
+
+void process_cor_options(gengetopt_args_info* args)
+{
+  if(args->dictionary_given)
+    {
+      expand_path(args->dictionary_arg,dictionary);
+      if(file_accessible(dictionary)!=0)
+	{
+	  fprintf(stderr,"Cannot open the dictionary file: %s\nAborting.\n",dictionary);
+	  exit(1);
+	}
+    }
+  else if (args->dictionary_home_given && args->language_given)
+    {
+      char buf[255];
+      expand_path(args->dictionary_home_arg, buf);
+      sprintf(dictionary,"%s/%s/cor.bin",buf,args->language_arg);
+      if(file_accessible(dictionary)!=0)
+	{
+	  fprintf(stderr,"Cannot open the dictionary file: %s\nAborting.\n",dictionary);
+	  exit(1);
+	}
+    }
+}
Index: src/cor/common_cor.h
===================================================================
--- src/cor/common_cor.h	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
+++ src/cor/common_cor.h	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
@@ -0,0 +1,19 @@
+#ifndef __COMMON_COR_H
+#define __COMMON_COR_H
+
+#include <stdio.h>
+
+//do wyrzucenia - definicja w Makefile! #define _CMDLINE_FILE "../cor/cmdline.h"
+#include "../common/common.h"
+
+#include "cmdline.h"
+
+#define DICT_FILE "cor.bin"
+
+extern int change_count;
+
+extern void process_cor_options(gengetopt_args_info* args);
+
+extern char dictionary[];
+
+#endif
Index: src/cor/corr.cc
===================================================================
--- src/cor/corr.cc	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
+++ src/cor/corr.cc	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
@@ -0,0 +1,142 @@
+//---------------------------------------------------------------------------
+
+#include "corr.hh"
+
+#define MAXPATH 256
+
+#define min(x,y) ((x<y)?(x):(y))
+#define max(x,y) ((x>y)?(x):(y))
+
+
+int Corr::ed(int i,int j)
+{
+  if(i==-1)
+    return j+1;
+  if(j==-1)
+    return i+1;
+  if(i==-2 || j==-2)
+    return n+1;
+
+  if(X[i]==Y[j])
+    return H2[i-1][j-1];
+  if(X[i-1]==Y[j] && X[i]==Y[j-1])
+    return 1+min(H2[i-2][j-2],min(H2[i][j-1],H2[i-1][j]));
+  return 1+min(H2[i-1][j-1],min(H2[i][j-1],H2[i-1][j]));
+
+/*
+  if(X[i]==Y[j])
+    return H[(i-1)+2][(j-1)+2];
+  if(X[i-1]==Y[j] && X[i]==Y[j-1])
+    return 1+min(H[(i-2)+2][(j-2)+2],min(H[(i)+2][(j-1)+2],H[(i-1)+2][(j)+2]));
+  return 1+min(H[(i-1)+2][(j-1)+2],min(H[(i)+2][(j-1)+2],H[(i-1)+2][(j)+2]));
+*/
+}
+
+int Corr::cuted(int j)
+{
+  int l=max(0,j-t);
+  int u=min(m,j+t);
+  int ce=j+t;
+  for(int k=l;k<=u;k++)
+  {
+    if(H2[k][j]<ce)//if(H[(k)+2][(j)+2]<ce)
+      ce=H2[k][j];//ce=H[(k)+2][(j)+2];
+  }
+  return ce;
+}
+
+/*
+void Corr::recomputeH(int j)
+{
+  for(int i=0;i<=m;i++)
+    H[(i)+2][(j)+2]=ed(i,j);
+}
+*/
+
+void Corr::recomputeH(int j)
+{
+  int lo=max(0,j-t-2);
+  int hi=min(m,j+t+2);
+  for(int i=lo;i<=hi;++i)
+    H2[i][j]=ed(i,j);//H[(i)+2][(j)+2]=ed(i,j);
+}
+
+
+int Corr::correct(const char* w, Words& tab)
+{
+  long int path[MAXPATH]={0};
+  int i;                                    // row index (X)
+  int j;                                    // column index (Y)
+  long state=0;
+
+  strcpy(X,w);
+  m=strlen(X)-1;
+  n=m+t;
+
+  for(i=(-2);i<=m;i++)
+    H[(i)+2][(-2)+2]=n;
+  for(i=(-1);i<=m;i++)
+    H[(i)+2][(-1)+2]=(i)+1;
+  for(j=(-2);j<=n;j++)
+    H[(-2)+2][(j)+2]=n;
+  for(j=(-1);j<=n;j++)
+    H[(-1)+2][(j)+2]=(j)+1;
+
+  for(j=0; j<=n; ++j)
+    for(i=0; i<=m; ++i)
+      H[i+2][j+2]=t+1;
+
+  int more=1;
+  bool cont=false;
+
+  strcpy(Y,"");
+  j=0;
+  state=0;
+  int count=0;
+  while(more)
+  {
+    if(!empty(state))
+    {
+      Y[j]=input(state);
+      recomputeH(j);
+      if(cuted(j)<=t)
+      {
+        int edd;
+        if(final(next(state)) && (edd=H[(m)+2][(j)+2])<=t)
+        {
+          char* out=new char[j+2];
+          strncpy(out,Y,j+1);
+          out[j+1]='\0';
+          //          if(cont) putchar(' ');
+          cont=true;
+	  //          printf("%i,%s", edd,out);
+	  //          cout << out << "(" << edd << ")" << endl;
+	  tab.add(out);
+	  count++;
+        }
+        path[j++]=state;
+        state=next(state);
+        continue;
+      }
+      else
+        if(continued(state))
+        {
+          state++;
+          continue;
+        }
+    }
+    //backtracking
+    do
+      if(j>0)
+        j--;
+      else
+        more=0;
+    while(more && !continued(path[j]));
+    state=path[j]+1;
+  }
+  return count;
+}
+
+
+//---------------------------------------------------------------------------
+
Index: src/cor/corr.hh
===================================================================
--- src/cor/corr.hh	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
+++ src/cor/corr.hh	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
@@ -0,0 +1,34 @@
+//---------------------------------------------------------------------------
+#ifndef _corr_hh
+#define _corr_hh
+//---------------------------------------------------------------------------
+
+#include "../lib/tfti.h"
+#include "../lib/word.h"
+
+class Corr : public TFTiv<char,char>
+{
+private:
+  int H[100][100];
+  char X[100];                         // misspelled string
+  char Y[100];                         // (possibly partial) candidate string
+  int m;                               // length of X
+  int n;                               // maximal length of Y
+
+  int ed(int,int);
+  int cuted(int);
+  void recomputeH(int);
+
+public:
+  int (*H2)[100];
+
+  int t;                               // threshold
+
+  Corr() : H2((int(*)[100])&H[2][2]) {};
+  Corr(const char* a) : TFTiv<char,char>(a), H2((int(*)[100])&H[2][2]) { };
+
+  int correct(const char* w, Words& tab);
+};
+
+//---------------------------------------------------------------------------
+#endif
Index: src/cor/main.cc
===================================================================
--- src/cor/main.cc	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
+++ src/cor/main.cc	(revision 5f4d9c3b32eea7b6643a751aa75bdb05b7d41576)
@@ -0,0 +1,155 @@
+#include <stdlib.h>
+#include <ctype.h>
+#include "../lib/iotools.h"
+//do wyrzucenia - definicja w Makefile! #define _CMDLINE_FILE "../cor/cmdline.h"
+#include "../common/common.h"
+#include "common_cor.h"
+#include "corr.hh"
+#include "cmdline.h"
+#include <locale.h>
+
+
+int main(int argc, char** argv) {
+
+//   setlocale(LC_CTYPE,"");
+//   setlocale(LC_COLLATE,"");
+
+  gengetopt_args_info args;
+
+  if(cmdline_parser(argc, argv, &args) != 0)
+    exit(1);
+
+  process_config_files(&args,argv[0]);
+  process_common_options(&args,argv[0]);
+  process_cor_options(&args);
+
+  Corr cor;
+
+  cor.load(dictionary);
+  cor.t=args.distance_arg;
+
+  char line[MAX_LINE+1];
+  long line_count = 0;
+
+  Segment seg;
+  Words tab;
+  char form1[MAX_LINE];
+  char* form;
+  int formcasing;
+  char corfield[MAX_LINE]="";
+
+  while (fgets(line, MAX_LINE, inputf))
+    {
+//       strcpy(outline,line);
+      ++line_count;
+
+//       if(!seg.parse(line))
+//         {
+//           fprintf(stderr,"Input error in line %d.\n",line_count);
+//           exit(1);
+//         }
+
+      char outline[128];
+      //printf("Starting cor... searching for %d fields\n", args.input_field_given);
+      //for (int i=0; i<args.input_field_given; ++i) {
+      //	printf("\t%d. %s\n", i, args.input_field_arg[i]);
+      //}
+
+      if (!process_seg(line, args))
+	fputs(line, outputf);
+      else
+	{
+	  char form[MAX_FORM];
+	  
+	  tab.clear();
+	  getfield(line,input_field_prefix,form);
+	  if (form==NULL) continue;
+	  
+	  formcasing=3;
+	  cor.correct(form, tab);
+	  
+	  if( tab.count() == 0 )
+	    {
+	      formcasing=casing(form);
+	      if( formcasing == 1 || formcasing == 2)
+		tolowers(form, form1), cor.correct(form1, tab);
+	    }
+	  
+	  if ( tab.count() == 0)
+	    fputs(line, failedf);
+	  else 
+	    {
+	      if(args.replace_flag)
+		{
+		  char corfield[128];
+		  strcpy(corfield, input_field_prefix);
+		  strcat(corfield, form);
+		  seg.aux[seg.auxn]=corfield;
+		  ++seg.auxn;
+		  for(int i=0; i<tab.count(); ++i)
+		    {
+		      seg.form=tab[i].form();
+		      restorecasing(seg.form,seg.form,formcasing);
+		      seg.print(outline);
+		      fputs(outline, outputf);
+		    }
+		  --seg.auxn;
+		}
+	      else
+		{
+		  if(one_line)
+		    {
+		      char* p=corfield;
+		      for(int i=0; i<tab.count(); ++i)
+			{
+			  restorecasing(tab[i].form(),tab[i].form(),formcasing);
+			  p += sprintf(p," %s%s",output_field_prefix,tab[i].form());
+			}
+		      sprintf(p,"\n");
+
+		      strcpy(outline,line);
+		      outline[strlen(outline)-1]='\0';
+		      strcat(outline,corfield);
+		      fputs(outline, outputf);
+		    }
+		  else if(one_field)
+		    {
+		      char* p=corfield;
+		      p += sprintf(p," %s",output_field_prefix);
+		      for(int i=0; i<tab.count(); ++i)
+			{
+			  restorecasing(tab[i].form(),tab[i].form(),formcasing);
+			  p += sprintf(p,(i==0)?"%s":";%s",tab[i].form());
+			}
+		      
+		      sprintf(p,"\n");
+
+		      strcpy(outline,line);
+		      outline[strlen(outline)-1]='\0';
+		      strcat(outline,corfield);
+		      fputs(outline, outputf);
+		    }
+		  else
+		    {
+		      for(int i=0; i<tab.count(); ++i)
+			{
+			  restorecasing(tab[i].form(),tab[i].form(),formcasing);
+			  sprintf(corfield," %s%s\n",output_field_prefix,tab[i].form());
+			  strcpy(outline,line);
+			  outline[strlen(outline)-1]='\0';
+			  strcat(outline,corfield);
+			  fputs(outline, outputf);
+			}
+		    }
+		}
+	    }
+	}
+      
+      if(args.interactive_flag)
+	{
+        fflush(outputf);
+        fflush(failedf);
+      }
+    }
+  cmdline_parser_free(&args);
+}
