Refactoring
This commit is contained in:
parent
fc08393782
commit
d92c98e2d9
1 changed files with 32 additions and 19 deletions
|
|
@ -1,6 +1,4 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <argp.h>
|
||||
#include "../libs/utf8.h"
|
||||
#define SIZEOFARR(a) (sizeof(a)/sizeof(a[0]))
|
||||
|
|
@ -69,7 +67,8 @@ struct Gap_sentence
|
|||
short unsigned int length;
|
||||
};
|
||||
|
||||
int import_text(char path[],int max_chars,char words[][max_chars]){
|
||||
int import_text(char path[],int max_chars,char words[][max_chars])
|
||||
{
|
||||
FILE *text;
|
||||
text = fopen(path, "r");
|
||||
if(text == NULL)
|
||||
|
|
@ -82,13 +81,14 @@ int import_text(char path[],int max_chars,char words[][max_chars]){
|
|||
int word_index = 0;
|
||||
int character_index = 0;
|
||||
int word_found = 0; //checks if multiple spaces are behind each other, we don't want to add a new word
|
||||
while(1){
|
||||
unsigned short int read_entire_file = 0;
|
||||
while(!read_entire_file){
|
||||
char curr_char = fgetc(text);
|
||||
if(curr_char==EOF)
|
||||
{
|
||||
break;
|
||||
{
|
||||
read_entire_file = 1;
|
||||
}
|
||||
if (!arrContains(curr_char,allowed_chars,SIZEOFARR(allowed_chars)))
|
||||
else if (!arrContains(curr_char,allowed_chars,SIZEOFARR(allowed_chars)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
|
@ -113,7 +113,8 @@ int import_text(char path[],int max_chars,char words[][max_chars]){
|
|||
return 0;
|
||||
}
|
||||
|
||||
int import_gap_sentence(char path[],struct Gap_sentence *gap_sentence){
|
||||
int import_gap_sentence(char path[],struct Gap_sentence *gap_sentence)
|
||||
{
|
||||
FILE *gap_file;
|
||||
gap_file = fopen(path,"r");
|
||||
|
||||
|
|
@ -155,21 +156,31 @@ int import_gap_sentence(char path[],struct Gap_sentence *gap_sentence){
|
|||
return 0;
|
||||
}
|
||||
|
||||
int get_matching_sentences(int text_occurances[],struct Gap_sentence *gap_sentence,int max_words, int max_chars, char words[][max_chars]){
|
||||
int get_matching_sentences(int text_occurances[],struct Gap_sentence *gap_sentence,int max_words, int max_chars, char words[][max_chars])
|
||||
{
|
||||
unsigned int possibleMatch_index = 0;
|
||||
int text_occurance_count= 0;
|
||||
unsigned int text_occurance_count= 0;
|
||||
unsigned short int read_entire_file = 0;
|
||||
|
||||
while(possibleMatch_index<max_words)
|
||||
//Until we read all words
|
||||
while(1)
|
||||
{
|
||||
//find next occurance of first word
|
||||
while(1){
|
||||
if(utf8casecmp(gap_sentence->words[0],words[possibleMatch_index])==0){
|
||||
break;
|
||||
//find next occurance of first word of gap_sentence in the text
|
||||
unsigned short int found = 0;
|
||||
while(!found)
|
||||
{
|
||||
if(possibleMatch_index>max_words)
|
||||
{
|
||||
goto read_all_words;
|
||||
}
|
||||
if(possibleMatch_index>max_words){
|
||||
break;
|
||||
else if(utf8casecmp(gap_sentence->words[0],words[possibleMatch_index])==0)
|
||||
{
|
||||
found = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
possibleMatch_index++;
|
||||
}
|
||||
possibleMatch_index++;
|
||||
}
|
||||
|
||||
for(int i = 1; i<gap_sentence->length;i++)
|
||||
|
|
@ -183,7 +194,8 @@ int get_matching_sentences(int text_occurances[],struct Gap_sentence *gap_senten
|
|||
}
|
||||
}
|
||||
//If we are looking at a gap lenght
|
||||
else{
|
||||
else
|
||||
{
|
||||
// -1 because i gets increased by 1 after 'continue'
|
||||
i += gap_sentence->gapsizes[i]-1;
|
||||
continue;
|
||||
|
|
@ -197,6 +209,7 @@ int get_matching_sentences(int text_occurances[],struct Gap_sentence *gap_senten
|
|||
possibleMatch_index++;
|
||||
|
||||
}
|
||||
read_all_words:
|
||||
return text_occurance_count-1;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue