Advertisement
Shanix

Untitled

Mar 16th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. /* The spellcheck.c component will contain the main() function and any other supporting functions you decide to use.
  2.  
  3. It will be responsible for handling the command-line arguments, finding words in the text, checking to see if they're in the dictionary, and responding to user commands.
  4.  
  5. Other than main(), you're not required to define any specific functions in spellcheck.c, but you must decide on at least two additional functions you'd like to use to simplify your implementation. You can break the problem down however you'd like, but you must use at least two non-trivial functions in addition to main. You can use more if you want.*/
  6.  
  7. #include <stdio.h>
  8. #include <stdbool.h>
  9. #include <ctype.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "text.h"
  13.  
  14. #define INPUT_SIZE 100
  15.  
  16. char **dictionary;
  17. int dictionarySize;
  18.  
  19. char **wordLines;
  20. int linesSize;
  21.  
  22. /**
  23. * qsort comparing
  24. */
  25. int compareStrings( const void* a, const void* b )
  26. {
  27. const char *str1 = *(const char **)a;
  28. const char *str2 = *(const char **)b;
  29. int str1Len = strlen( str1 );
  30. int str2Len = strlen( str2 );
  31.  
  32. if ( str1Len >= str2Len ) {
  33. return strncmp(str1, str2, str1Len);
  34. } else {
  35. return strncmp(str1, str2, str2Len);
  36. }
  37.  
  38. }
  39.  
  40. /**
  41. * Function to check if a word is in the dictionary. Can be
  42. * easily modded to take in a dictionary. Will loop through
  43. * the dictionary until it finds a word or until the given word
  44. * is smaller than current position - meaning it wasn't found.
  45. * @param word a char array to check if in dictionary.
  46. * @return 1 or 0 if found or not respectively.
  47. */
  48. int isInDictionary( char *word )
  49. {
  50. //word to be compared, must be made lowercase
  51. //but we don't want to modify the word itself.
  52. char lowercaseWord[ strlen( word ) + 1 ];
  53. for ( int i = 0; i < strlen( word ); i++ ) {
  54. lowercaseWord[i] = tolower( word[i] );
  55. }
  56.  
  57. lowercaseWord[ strlen( word ) ] = '\0';
  58. for ( int i = 0; i < dictionarySize; i++ ) {
  59. if ( strcmp( lowercaseWord, dictionary[i] ) == 0 ) {
  60. return 1;
  61. } else if ( strcmp( lowercaseWord, dictionary[i] ) < 0 ) {
  62. break;
  63. }
  64. }
  65.  
  66. // int *value = bsearch( lowercaseWord, dictionary, dictionarySize, sizeof( dictionary[0] ), compareStrings );
  67.  
  68. return 0;
  69. }
  70.  
  71. /**
  72. * When called, markMisspelled will change color of output
  73. * to red. Once called again, standard color.
  74. */
  75. void markMisspelled( char *word )
  76. {
  77. printf( "\x1B[31m" );
  78. printf( "%s", word );
  79. printf( "\x1B[0m" );
  80. }
  81.  
  82. /**
  83. * Function to check spelling of a given line.
  84. * @param
  85. */
  86.  
  87. //Not finished
  88. int checkLineForMisspelling( char *line )
  89. {
  90. bool breakLine = false;
  91.  
  92. //read through line, if strings match, compare, else just print.
  93.  
  94. int capacity = 5;
  95. char *word = malloc( capacity );
  96.  
  97. //null terminating string
  98. for ( int i = 0; i < capacity; i++ ) {
  99. line[i] = '\0';
  100. }
  101.  
  102. for (int i = lineIndex; i < index; i++) {
  103. if ( breakLine ) {
  104.  
  105. } else {
  106. if ( line[i] == '\n' ) {
  107. break;
  108. } else {
  109. if ( isalpha( line[i] ) ) {
  110. word[index] = line[i];
  111. index++;
  112. if ( index == capacity ) {
  113. capacity *= 2;
  114. line = (char *)realloc(line, capacity);
  115.  
  116. //getting those null terminators in
  117. for ( int i = index; i < capacity; i++ ) {
  118. line[i] = '\0';
  119. }
  120. }
  121. } else {
  122. if ( word[0] != '\0' ) {
  123. if ( isInDictionary( word ) ) {
  124. printf( "%s", );
  125. } else {
  126.  
  127. }
  128. }
  129. printf( "%c", line[i] );
  130. }
  131.  
  132. }
  133. }
  134. }
  135.  
  136. // for loop through each line
  137. // create a word from all alpha characters, otherwise print
  138. // print word, red if not in dictionary
  139. // then correct is necessary.
  140. // then move on through loop
  141.  
  142. return breakLine;
  143. }
  144.  
  145. /**
  146. * Program Start.
  147. */
  148. int main( int argc, char *argv[] )
  149. {
  150. // Check command-line arguments and open the input file.
  151. FILE *fp = fopen( argv[ 1 ], "r" );
  152. FILE *dictionaryFile = fopen( "words.txt", "r" );;
  153.  
  154. if ( fp == NULL ) {
  155. printf( "usage: spellcheck <textfile.txt> [words.txt]\n" );
  156. exit( EXIT_FAILURE );
  157. }
  158.  
  159. if ( argc == 3 ) {
  160. fclose( dictionaryFile );
  161. dictionaryFile = fopen( argv[ 2 ], "r" );
  162. }
  163.  
  164. fclose( fp );
  165. fclose( dictionaryFile );
  166.  
  167. if ( argc > 3 || argc < 2 ) {
  168. printf( "usage: spellcheck <textfile.txt> [words.txt]\n" );
  169. exit( EXIT_FAILURE );
  170. }
  171.  
  172. //Words from text files.
  173. linesSize = 0;
  174. int *linePointer = &linesSize;
  175. wordLines = readLines( argv[ 1 ], linePointer );
  176.  
  177. //Dictionary.
  178. dictionarySize = 0;
  179. int *countPointer = &dictionarySize;
  180. if ( argc == 2 ) {
  181. dictionary = readLines( "words.txt", countPointer );
  182. } else {
  183. dictionary = readLines( argv[ 2 ], countPointer );
  184. }
  185.  
  186. // Check dictionary for errors.
  187. for ( int i = 0; i < dictionarySize; i++ ) {
  188. int stringLength = strlen( dictionary[i] );
  189. if ( dictionary[i][0] == 0 ) {
  190. printf( "Invalid word, line: %d\n", i + 1 );
  191. return EXIT_FAILURE;
  192. }
  193. for ( int j = 0; j < stringLength; j++ ) {
  194. if ( !isalpha( dictionary[i][j] ) ) {
  195. printf( "Invalid word, line: %d\n", i + 1 );
  196. return EXIT_FAILURE;
  197. }
  198. if ( dictionary[i][j] != tolower( dictionary[i][j] ) ) {
  199. printf( "Invalid word, line: %d\n", i + 1 );
  200. return EXIT_FAILURE;
  201. }
  202. }
  203. }
  204.  
  205. //sort.
  206. qsort( dictionary, dictionarySize, sizeof( dictionary[0] ), compareStrings );
  207.  
  208. int index = 0;
  209. while ( wordLines[index] != NULL ) {
  210.  
  211.  
  212.  
  213. char *input;
  214. input = (char *) malloc( 1 * sizeof( char ) );
  215.  
  216. // Prompt user for input
  217. printf( "(r)eplace, (a)dd word, (n)ext or (q)uit: ");
  218. scanf( "%s", input );
  219. input[0] = tolower( input[0] );
  220. if( input[0] == 'q' ) {
  221. fprintf( stderr, "Discarding changes" );
  222. break;
  223. } else if ( input[0] == 'r' ) {
  224. printf( "Replacing.\n" );
  225. } else if ( input[0] == 'a' ) {
  226. printf( "Adding Word.\n" );
  227. } else if ( input[0] == 'n' ) {
  228. printf( "Ignoring.\n" );
  229. } else {
  230. printf( "Unknown command\n" );
  231. i--;
  232. }
  233. }
  234.  
  235. //this will eventually do stuff.
  236. // spellcheckLoop();
  237.  
  238. //free it all up.
  239. freeLines( dictionary, dictionarySize );
  240.  
  241. //just to be safe.
  242. printf( "\n" );
  243.  
  244. //oh ye and exit too
  245. return EXIT_SUCCESS;
  246.  
  247. }
  248.  
  249. /**
  250. * UI for user input. Prompts the user to either (r)eplace an incorrect word,
  251. * (a)dd a word to the dictionary, move to the (n)ext word in the text file,
  252. * or (q)uit the program
  253.  
  254. char * getUserInput()
  255. {
  256. char *input;
  257. input = (char *) malloc( INIT_SIZE * sizeof( char ) );
  258. // Prompt user for input
  259. printf( "(r)eplace, (a)dd word, (n)ext or (q)uit: ");
  260. scanf( "%s", input );
  261. input[0] = tolower( input[0] );
  262. if( input[0] == 'r' ) {
  263. char *toReplace;
  264. toReplace = ( char * ) malloc( INIT_SIZE * sizeof( char ) );
  265. scanf( "%s", toReplace );
  266. // Merge the two strings
  267. strcat( input, toReplace );
  268. }
  269. return input;
  270. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement