-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_io.c
More file actions
57 lines (46 loc) · 1.8 KB
/
Copy pathmodel_io.c
File metadata and controls
57 lines (46 loc) · 1.8 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
#include "model_io.h"
#include <stdio.h>
// Saves the model data (word probabilities) to a binary file.
void save_model (char* filename, WordProbability* word_probs, int word_prob_count)
{
FILE *file = fopen(filename, "wb");
if (file == NULL)
{
// If the file is not opened, print an error message. Return early.
printf("Error opening file to write %s\n", filename);
return;
}
// Write the count of word probabilities to the file.
fwrite(&word_prob_count, sizeof(int), 1, file);
// Write the actual word probabilities to the file.
fwrite(word_probs, sizeof(WordProbability), word_prob_count, file);
fclose(file);
printf("Model saved to %s\n", filename);
}
// Loads the model data (word probabilities) from a binary file.
int load_model (char* filename, WordProbability* word_probs, int max_words)
{
FILE *file = fopen(filename, "rb");
if (file == NULL)
{
// If the file is not opened, print an error message. Return early.
printf("Error opening file to read %s\n", filename);
return -1;
}
int word_prob_count;
// Read the count of word probabilities from the file.
fread(&word_prob_count, sizeof(int), 1, file);
// If the count of word probabilities is greater than the maximum number of words, print an error message and return early.
if (word_prob_count > max_words)
{
printf("Model file contains too many words\n");
fclose(file);
return -1;
}
// Load the word probabilities from the file into the provided array.
fread(word_probs, sizeof(WordProbability), word_prob_count, file);
fclose(file);
// Print a message indicating that the model was successfully loaded.
printf("Model loaded from %s\n", filename);
return word_prob_count;
}