/*
Creates a word list of arbitrary length, balanced for valence, and
within valence, balanced for word length and word frequency.
Uses the data file words.prn to obtain its normative data.
*/

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<stdlib.h>
#include<process.h>
#include<string.h>
#include<time.h>

#define MAXWORDS 1000

class word {
  public:
	 char text[15];
	 int valence, len, freq,chosen;
  };


int getwords(char *fname, word wordlist[]) {
	FILE *fp=fopen(fname,"r");
	int index=0;
	char str[80];

	while (fgets(str,256,fp)!=NULL) {
		  wordlist[index].chosen=0;
		  fscanf(fp,"%s",&wordlist[index].text);
		  fscanf(fp,"%d",&wordlist[index].valence);
		  fscanf(fp,"%d",&wordlist[index].len);
		  fscanf(fp,"%d",&wordlist[index++].freq);
	}
	return(index);
}

// we only want to pick words we can balance
void choosewords(word wordlist[], int totwords, int numwords, int anxw){
	int current,j,k,l,currother[5];
	int counter=0, nomatch=0;

	//printf("Choosing words\n");
	while (counter<(numwords-2)) {
	  for (j=1; (j<4 && (counter<numwords-1)) ; j++) {
		  if ((!anxw) && (j==3)) j++; // bypass the anxiety words
		  // pick a word
		  PICKWORD:
		  ;
		  current=random(totwords);
		  while ((wordlist[current].chosen) || wordlist[current].valence !=j)
			 current=random(totwords);
		  wordlist[current].chosen=1; counter++;
		  //printf("Word %d*=%s,%d,%d,%d\n",counter,wordlist[current].text,
		  //	wordlist[current].valence,wordlist[current].len,wordlist[current].freq
		  //	);

		  // try to match it with each other valence, 100 times at most

		  for (k=1;k<5;k++){currother[k]=999;}
		  for (k=1; k<5; k++) {

			 if(k==j) k++; // bypass the one we're on
			 if ((!anxw) && (k==3)) k++; // bypass the anxiety words
			 if((k==4) && (j==4)) k++;
			 else if (counter > (numwords-1));
			 else {
				 currother[k]=random(totwords);
				 nomatch=0;
				 while (((wordlist[currother[k]].chosen==1) ||
						  (wordlist[currother[k]].valence !=k) ||
						  (wordlist[currother[k]].len > (wordlist[current].len+2)) ||
						  (wordlist[currother[k]].len < (wordlist[current].len-2)) ||
						  (wordlist[currother[k]].freq > (wordlist[current].freq+10)) ||
						  (wordlist[currother[k]].freq < (wordlist[current].freq-10)))
						  && (nomatch++<100)
						  ) currother[k]=random(totwords);
				 if (nomatch>99) {
					 //pick another original word
					 wordlist[current].chosen=0;
					 counter-=(1+(k-1)); if (k==4) counter+=1;
					 for(l=0;l<k;l++)
						if(currother[l]!=999) wordlist[currother[l]].chosen=0;
					 //printf("rejected...\n");
					 goto PICKWORD;
					 }
				 wordlist[currother[k]].chosen=1; counter++;
				 //printf("Word %d =%s,%d,%d,%d\n",counter,wordlist[currother[k]].text,
				 //				  wordlist[currother[k]].valence,
				 //				  wordlist[currother[k]].len,
				 //				  wordlist[currother[k]].freq);
				 }
		  }
	  }
	}
}

void printchosen(word wordlist[],int totwords) {
	for (int i=0;i<totwords;i++) {
		if(wordlist[i].chosen)
		  printf("%-15s\t%d\t%d\t%d\t\n", wordlist[i].text, wordlist[i].valence,
					 wordlist[i].len, wordlist[i].freq);
	}
}



main(int argc, char *argv[]) {
	word wordlist[MAXWORDS];
	char fname[12]="words.prn";
	int anxw=0;

	int numwords=atoi(argv[1]),totwords;

	if (argc<2) {
	  printf("To run the wordlist program you must type\n");
	  printf("the total number of words you want.\n");
	  printf("  You may specify a word file by typing that\n");
	  printf("after the number.\n");
	  printf("  You may include anxiety words by specifying\n");
	  printf("'ANX' after the word file.\n");
	  printf("  So, a sample command might be\n");
	  printf("      wordlist 16 words.prn ANX\n");
	  printf("which would generate a list of 16 words balanced\n");
	  printf("for positive, negative, neutral, and anxiety words.\n");
	  exit(0);
	  }
	if (argc>2) strcpy(fname,argv[2]);
	if (argc>3) anxw=1;

	randomize();
	totwords=getwords(fname,wordlist);
	choosewords(wordlist,totwords,numwords,anxw);
	printchosen(wordlist,totwords);

}