Repository navigation
Expand file tree
/
Copy pathreader.c
More file actions
127 lines (114 loc) · 3.5 KB
/
Copy pathreader.c
File metadata and controls
127 lines (114 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "analizer.h"
#include "reader.h"
#include "manager.h"
static int n = 0;
static FILE *file = NULL;
static int numberOfWords = 0;
static int position = 0;
void add_sufix_r( struct ngram *pointer, char *text, int number){
if( pointer->capacity == 0){
pointer->sufixes = malloc( number * sizeof *pointer->sufixes);
assert(pointer->sufixes != NULL);
pointer->capacity = number;
} else {
realloc_sufixes( pointer );
}
pointer->sufixes[(*pointer).number] = strdup( text );
(*pointer).number++;
}
void add_ngram_r( struct data *data, char **text ){
register int i = 0;
int sufix_pos = n - 1;
int numberOfPrefixesOccurance = 0;
int numberOfSufixes = 0;
initialize_ngram( data );
add_prefix( data->ngrams[data->number], text, position );
position += n - 1;
numberOfPrefixesOccurance = atoi( text[position] );
position++;
numberOfSufixes = atoi( text[position] );
position++;
for( i = 0; i < numberOfSufixes; i++ ) {
add_sufix_r(data->ngrams[data->number], text[position], numberOfSufixes); //dodaje sufix do istniejącego ngramu
position++;
}
data->ngrams[data->number]->occurance = numberOfPrefixesOccurance;
data->number += 1;
}
void read( char *ifile, struct data *d ) {
int i = 0;
long int size = 0;
int numberOfSufixes = 0;
int numberOfPrefixesOccurance = 0;
int numberOfPrefixesOccuranceBefore = 0;
char *content = NULL;
char *string = NULL;
char **text = NULL;
struct ngram *pointer = NULL;
numberOfWords = 0;
position = 0;
file = fopen(ifile, "r");
if (file == NULL) {
return;
}
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
if (size == 0) {
fprintf(stderr, "Plik %s nie zawiera tekstu", ifile);
return;
}
content = calloc(size + 1, 1);
fread(content, 1, size, file);
fclose(file);
text = malloc((size + 1) * sizeof *text);
i = 0;
string = strtok(content, " \n");
while (string != NULL) {
text[i] = strdup(string);
i++;
string = strtok(NULL, " \n");
}
while (text[numberOfWords] != NULL)
numberOfWords++;
if (numberOfWords == 0)
return;
n = atoi(text[position]);
if( n == 0 ) {
fprintf(stderr, "Plik %s jest nieprawidłowy\n", ifile);
return;
}
if ( n != get_number("m") ) {
fprintf(stderr, "Ngramy w pliku pośrednim %s mają nieprawidłowy stopień %d oczekiwano %d \n", ifile, n,
get_number("m"));
return;
}
setN(n);
position++;
if (d->ngrams == NULL)
initialize_data(d);
while (text[position] != NULL) {
pointer = find_ngram(text, d, position);
if (pointer != NULL) {
position += n - 1;
numberOfPrefixesOccurance = atoi(text[position]);
numberOfPrefixesOccuranceBefore = pointer->occurance;
position++;
numberOfSufixes = atoi(text[position]);
position++;
for (i = 0; i < numberOfSufixes; i++) {
add_sufix_r(pointer, text[position], numberOfSufixes); //dodaje sufix do istniejącego ngramu
position++;
}
pointer->occurance = numberOfPrefixesOccuranceBefore + numberOfPrefixesOccurance;
} else {
realloc_data(d);
add_ngram_r(d, text); //dodaje nowy ngram
}
}
return;
}