wordfreq.c 3.95 KB
Newer Older
1
2
/****************************************************************
 *								*
3
 * Copyright (c) 2017-2018 YottaDB LLC and/or its subsidiaries. *
4
5
6
7
8
9
10
11
12
13
14
 * All rights reserved.						*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

#include "libyottadb.h"	/* for ydb_* macros/prototypes/typedefs */

15
16
#include <sys/types.h>	/* needed for "getpid" */
#include <unistd.h>	/* needed for "getpid" */
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>	/* for "printf" */
#include <string.h>	/* for "strtok" */
#include <ctype.h>	/* for "toupper" */

/* count and report word frequencies for http://www.cs.duke.edu/csed/code/code2007/ */
int main()
{
	ydb_buffer_t	tmp1, tmp2, index, words, words_tmp1, null;
	ydb_buffer_t	index_subscr[2], value;
	char		tmp1buff[64], tmp2buff[64], valuebuff[64], words_tmp1buff[64], linebuff[1024], *lineptr, *ptr, *tmp_ptr;
	int		status;

	/* Initialize all array variable names we are planning to use. Randomly use locals vs globals to store word frequencies. */
30
	/* If using globals, delete any previous contents */
31
32
	if (getpid() % 2)
	{
33
34
		YDB_LITERAL_TO_BUFFER("words", &words);
		YDB_LITERAL_TO_BUFFER("index", &index);
35
36
	} else
	{
37
		YDB_LITERAL_TO_BUFFER("^words", &words);
38
		ydb_delete_s(&words, 0, NULL, YDB_DEL_TREE);
39
		YDB_LITERAL_TO_BUFFER("^index", &index);
40
		ydb_delete_s(&index, 0, NULL, YDB_DEL_TREE);
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
	}
	value.buf_addr = &valuebuff[0];
	value.len_used = 0;
	value.len_alloc = sizeof(valuebuff);
	do
	{
		lineptr = fgets(linebuff, sizeof(linebuff), stdin);
		if (NULL == lineptr)
			break;
		ptr = strtok(lineptr, " ");
		while (NULL != ptr)
		{	/* convert word pointed to by "ptr" to lowercase */
			tmp_ptr = ptr;
			while (*tmp_ptr)
			{
				*tmp_ptr = tolower(*tmp_ptr);
				tmp_ptr++;
			}
			/* */
			tmp1.buf_addr = ptr;
			tmp1.len_used = tmp_ptr - ptr;
			if (tmp_ptr[-1] == '\n')
				tmp1.len_used--;	/* - 1 to remove trailing newline */
			if (tmp1.len_used)
			{
				tmp1.len_alloc = tmp1.len_used;
				status = ydb_incr_s(&words, 1, &tmp1, NULL, &value);	/* M line : set value=$incr(words(tmp1)) */
68
				YDB_ASSERT(YDB_OK == status);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
			}
			ptr = strtok(NULL, " ");
		}
	} while (1);
	tmp1.buf_addr = tmp1buff;			/* M line : set tmp1="" */
	tmp1.len_used = 0;
	tmp1.len_alloc = sizeof(tmp1buff);
	words_tmp1.buf_addr = &words_tmp1buff[0];
	words_tmp1.len_used = 0;
	words_tmp1.len_alloc = sizeof(words_tmp1buff);
	null.buf_addr = NULL;
	null.len_used = 0;
	null.len_alloc = 0;
	do
	{
		status = ydb_subscript_next_s(&words, 1, &tmp1, &tmp1);	/* M line : set tmp1=$order(words(tmp1)) */
85
		if (YDB_ERR_NODEEND == status)
86
			break;
87
88
		YDB_ASSERT(YDB_OK == status);
		YDB_ASSERT(0 != tmp1.len_used);
89
		status = ydb_get_s(&words, 1, &tmp1, &words_tmp1);	/* M line : set words_tmp1=words(tmp1) */
90
		YDB_ASSERT(YDB_OK == status);
91
92
93
		index_subscr[0] = words_tmp1;
		index_subscr[1] = tmp1;
		status = ydb_set_s(&index, 2, index_subscr, &null);	/* M line : set index(words_tmp1,tmp1)="" */
94
		YDB_ASSERT(YDB_OK == status);
95
96
97
98
	} while (1);
	do
	{
		status = ydb_subscript_previous_s(&index, 1, &tmp1, &tmp1);	/* M line : set tmp1=$order(index(tmp1),-1) */
99
		if (YDB_ERR_NODEEND == status)
100
			break;
101
102
		YDB_ASSERT(YDB_OK == status);
		YDB_ASSERT(0 != tmp1.len_used);
103
104
105
106
107
108
109
110
		tmp2.buf_addr = tmp2buff;			/* M line : set tmp2="" */
		tmp2.len_used = 0;
		tmp2.len_alloc = sizeof(tmp2buff);
		index_subscr[0] = tmp1;
		index_subscr[1] = tmp2;
		do
		{
			status = ydb_subscript_next_s(&index, 2, index_subscr, &tmp2); /* M line : set tmp2=$order(index(tmp1,tmp2)) */
111
			if (YDB_ERR_NODEEND == status)
112
				break;
113
114
			YDB_ASSERT(YDB_OK == status);
			YDB_ASSERT(0 != tmp2.len_used);
115
116
117
118
119
120
121
122
			tmp1.buf_addr[tmp1.len_used] = '\0';
			tmp2.buf_addr[tmp2.len_used] = '\0';
			printf("%s\t%s\n", tmp1.buf_addr, tmp2.buf_addr);
			index_subscr[1] = tmp2;
		} while (1);
	} while (1);
	return YDB_OK;
}