-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsentiment.c
More file actions
269 lines (231 loc) · 9.44 KB
/
sentiment.c
File metadata and controls
269 lines (231 loc) · 9.44 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define TEXT_BUFFER_SIZE 1000
#define PHRASES_BUFFER_SIZE 30
/* struct definitions */
struct phrase_dictionary {
char *text;
int numOfWords;
int score;
};
/* typedef */
typedef struct phrase_dictionary phrase_dictionary_t;
/* function definitions */
FILE *openFile(char *filename);
void print(phrase_dictionary_t **dictionary, int dictionarySize, int score);
int calculateScore(phrase_dictionary_t **dictionary, int dictionarySize);
int strcasecmp(const char *s1, const char *s2);
void releaseMemory(phrase_dictionary_t **dictionary, int dictionarySize);
phrase_dictionary_t *searchWordInDictionary(phrase_dictionary_t **dictionary, int dictionarySize, char *word);
int main(int argc, char* argv[]){
FILE *file = NULL;
char printPhrases = '0';
/* too less or too much parameters */
if (argc < 2 || argc > 3) {
fprintf(stderr, "\nmissing operand after '%s'\n\nUsage: %s [-v] phrase-file\n", argv[0], argv[0]);
exit(EXIT_SUCCESS);
}
argv++;
/* handle input params */
if(argc == 2) {
file = openFile(*(argv));
} else if(argc == 3) {
if(strcmp(*(argv), "-v") == 0) {
printPhrases = '1';
argv++;
file = openFile(*(argv));
}
}
/* read phrases */
char phrases_input_buffer[PHRASES_BUFFER_SIZE];
phrase_dictionary_t **dictionary = calloc(0, sizeof(phrase_dictionary_t *));
int dictionarySize = 0;
/* read phrases line by line */
while(fgets(phrases_input_buffer, PHRASES_BUFFER_SIZE, file) != NULL) {
/* increase dictionary and check if allocation has worked*/
dictionarySize++;
dictionary = (phrase_dictionary_t **) realloc(dictionary, (dictionarySize) * sizeof(phrase_dictionary_t *));
if(dictionary == NULL) {
fprintf(stderr, "Memory for dictionary could not be reallocated!\n");
exit(EXIT_FAILURE);
}
dictionary[dictionarySize - 1] = (phrase_dictionary_t *) calloc(1, sizeof(phrase_dictionary_t));
if(dictionary[dictionarySize - 1] == NULL) {
fprintf(stderr, "Memory for dictionary entry could not be reallocted!\n");
exit(EXIT_FAILURE);
}
/* split by tabulator to extract text and score */
const char *delimiter = "\t";
char *delimited = strtok(phrases_input_buffer, delimiter);
/* phrases with more than 30 characters are rejected and throw an error */
if(strlen(delimited) > PHRASES_BUFFER_SIZE) {
fprintf(stderr, "Phrase %s rejected due to length > 30!", delimited);
exit(EXIT_FAILURE);
}
dictionary[dictionarySize - 1]->text = (char *) calloc(PHRASES_BUFFER_SIZE, sizeof(char));
if(dictionary[dictionarySize - 1]->text == NULL) {
fprintf(stderr, "Memory could not be allocated!");
exit(EXIT_FAILURE);
}
/* copy phrase to dictionary */
strcpy(dictionary[dictionarySize - 1]->text, delimited);
/* count number of words */
int wordCount = 1;
int i;
for(i = 0;i < strlen(delimited); i++) {
if(delimited[i] == ' ' && delimited[i + 1] != ' ')
wordCount++;
}
dictionary[dictionarySize - 1]->numOfWords = wordCount;
/* get score of phrase (part right of tabulator) */
delimited = strtok(NULL, delimiter);
int numberPart = strtol(delimited, NULL, 10);
if(numberPart != 0) {
dictionary[dictionarySize - 1]->score = numberPart;
}
}
/* create space to store occurrences */
phrase_dictionary_t **occurrenceDictionary = calloc(0, sizeof(phrase_dictionary_t *));
int occurrenceDictionarySize = 0;
if(occurrenceDictionary == NULL) {
fprintf(stderr, "Could not allocate memory!");
exit(EXIT_FAILURE);
}
char currentWord[PHRASES_BUFFER_SIZE];
char **wordContainer = calloc(0, sizeof(char *));
int containerSize = 0;
/* read word by word from stdin */
while(scanf(" %49[^ \t\n]", currentWord) != EOF) {
containerSize++;
/* wordcontainer holds all words */
wordContainer = realloc(wordContainer, containerSize * (sizeof(char *)));
if(wordContainer == NULL) {
fprintf(stderr, "Memory could not be reallocated!");
exit(EXIT_FAILURE);
}
wordContainer[containerSize - 1] = calloc(strlen(currentWord), sizeof(char));
if(wordContainer[containerSize - 1] == NULL) {
fprintf(stderr, "Memory could not be allocated!");
exit(EXIT_FAILURE);
}
strcpy(wordContainer[containerSize - 1], currentWord);
}
int i;
for(i = 0; i < containerSize; i++) {
/* check, if words from wordcontainer occur in the dictionary */
const phrase_dictionary_t *entry = searchWordInDictionary(dictionary, dictionarySize, wordContainer[i]);
if(entry != NULL) {
if(entry->numOfWords == 1) {
occurrenceDictionarySize++;
occurrenceDictionary = realloc(occurrenceDictionary, occurrenceDictionarySize * sizeof(phrase_dictionary_t *));
if(occurrenceDictionary == NULL) {
fprintf(stderr, "Memory could not be reallocated!");
exit(EXIT_FAILURE);
}
occurrenceDictionary[occurrenceDictionarySize - 1] = calloc(1, sizeof(phrase_dictionary_t));
if(occurrenceDictionary[occurrenceDictionarySize - 1] == NULL) {
fprintf(stderr, "Memory could not be allocated!");
exit(EXIT_FAILURE);
}
}
/* check if dictionary entry consists of more than one word */
if(entry->numOfWords > 1) {
if(i + entry->numOfWords > containerSize)
continue;
int index;
for(index = i + 1; index < i + entry->numOfWords; index++) {
if(strstr(entry->text, wordContainer[index]) != NULL) {
/* special handling of multi-word phrases */
occurrenceDictionarySize++;
occurrenceDictionary = realloc(occurrenceDictionary, occurrenceDictionarySize * sizeof(phrase_dictionary_t *));
if(occurrenceDictionary == NULL) {
fprintf(stderr, "Memory could not be reallocated!");
exit(EXIT_FAILURE);
}
occurrenceDictionary[occurrenceDictionarySize - 1] = calloc(1, sizeof(phrase_dictionary_t));
if(occurrenceDictionary[occurrenceDictionarySize - 1] == NULL) {
fprintf(stderr, "Memory could not be allocated!");
exit(EXIT_FAILURE);
}
memcpy(occurrenceDictionary[occurrenceDictionarySize - 1], entry, sizeof(phrase_dictionary_t));
}
}
} else {
memcpy(occurrenceDictionary[occurrenceDictionarySize - 1], entry, sizeof(phrase_dictionary_t));
}
}
}
/* calculate score */
int score = calculateScore(occurrenceDictionary, occurrenceDictionarySize);
/* print */
print((printPhrases == '1' ? occurrenceDictionary : NULL), occurrenceDictionarySize, score);
/* free dictionary */
releaseMemory(dictionary, dictionarySize);
/* free wordcontainer */
for(i = 0; i < containerSize; i++) {
free(wordContainer[i]);
}
free(wordContainer);
return EXIT_SUCCESS;
}
/* open a file */
FILE *openFile(char *filename) {
FILE *file = fopen(filename, "r");
if(file == NULL) {
fprintf(stderr, "\n%s is not a valid file!", filename);
exit(EXIT_FAILURE);
}
return file;
}
/* print phrases in valid json format */
void print(phrase_dictionary_t **dictionary, int dictionarySize, int score) {
/* opening bracket */
printf("{");
/* print items as json array */
if(dictionary != NULL) {
printf("\"matches\": [\n");
int i;
for(i = 0; i < dictionarySize; i++) {
printf("{\"phrase\": \"%s\", \"score\": %d}", dictionary[i]->text, dictionary[i]->score);
if(i != (dictionarySize - 1))
printf(",\n");
}
printf("],\n");
}
/* score is printed always */
printf("\"score\": %d}\n", score);
}
/* calculate score of given dictionary */
int calculateScore(phrase_dictionary_t **dictionary, int dictionarySize) {
int i;
int sum = 0;
for(i = 0; i < dictionarySize; i++) {
sum += dictionary[i]->score;
}
return sum;
}
/* search for word inside of a dictionary */
phrase_dictionary_t *searchWordInDictionary(phrase_dictionary_t **dictionary, int dictionarySize, char *word) {
int i;
for(i = 0; i < dictionarySize; i++) {
if(strcasecmp(dictionary[i]->text, word) == 0) {
return dictionary[i];
} else if(dictionary[i]->numOfWords > 1) { /* for handling multi-word phrases */
if(strstr(dictionary[i]->text, word) != NULL) {
return dictionary[i];
}
}
}
return NULL;
}
/* free memory of a dictionary */
void releaseMemory(phrase_dictionary_t **dictionary, int dictionarySize) {
int i;
for(i = 0; i < dictionarySize; i++) {
free(dictionary[i]->text);
free(dictionary[i]);
}
free(dictionary);
}