-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordAndCount.cfm
277 lines (209 loc) · 12.4 KB
/
wordAndCount.cfm
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
270
271
272
273
274
275
276
277
<CFPARAM NAME="FORM.textFormField" DEFAULT="0">
<CFPARAM NAME="textString" DEFAULT="">
<CFPARAM NAME="submit" DEFAULT="no">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>wordCount</TITLE>
</HEAD>
<BODY>
<!--- Author: Charles Robertson
Function: wordCount
Description: takes a text string as the only argument and converts it into a two dimensional array of unique words & their respective value count
Date: 13.05.09
Comments: extensive use of list to array converion
Language: CFScript --->
<CFSCRIPT>
function wordCount(inputString){
//initialise variables
var thisString = Trim(LCase(ARGUMENTS.inputString));
// add to discount list, if more rules required; characters that act as regular expression operators, need to be escaped like '.' -> '\.'
var discountList = ';/\,/--/-/\./:/\"/=/%/£/!/@/~';
var tempString = "";
var valueCountList = "";
var discountArr = ArrayNew(1);
var thisStringArr = ArrayNew(1);
var tempStringArr = ArrayNew(1);
var valueCountArr = ArrayNew(1);
var stringValueCountValueArr = ArrayNew(1);
discountArr = ListToArray(discountList,"/");
for(i = 1;i lte Arraylen(discountArr);i=i+1){
thisString = REReplaceNoCase(thisString,discountArr[i]," ","ALL");
}
thisString = REReplaceNoCase(thisString," ",",","ALL");
thisString = REReplaceNoCase(thisString,"[,]+",",","ALL");
thisStringArr = ListToArray(thisString);
for(i = 1;i lte ArrayLen(thisStringArr);i=i+1){
if(ListFind(tempString,thisStringArr[i]) EQ 0){
valueCountList = ListAppend(valueCountList,ListValueCount(thisString,thisStringArr[i]));
tempString = ListAppend(tempString,thisStringArr[i]);
}
}
tempStringArr = ListToArray(tempString);
valueCountArr = ListToArray(valueCountList);
stringValueCountValueArr = ArrayNew(2);
// two dimensional array
for(i = 1;i lte Arraylen(tempStringArr);i=i+1){
stringValueCountValueArr[i][1] = tempStringArr[i];
stringValueCountValueArr[i][2] = valueCountArr[i];
}
return stringValueCountValueArr;
}
</CFSCRIPT>
<CFSET wordAndCountInstance = createObject("component","wordAndCount").init()>
<CFIF textString IS "">
<CFSET cfcVar = wordAndCountInstance.wordCount("The Sandman's coming in his train of cars. With moonbeam windows and with wheels of stars. So hush you little ones and have no fear. The man-in-the-moon he is the engineer.")>
<CFSET cfVar = wordCount('The old lady pulled her spectacles down and looked over them about the room; then she put them up and looked out under them. She seldom or never looked THROUGH them for so small a thing as a boy; they were her state pair, the pride of her heart, and were built for "style," not service -- she could have seen through a pair of stove-lids just as well.')>
</CFIF>
<CFIF submit IS "yes">
<CFIF FORM.textFormField IS "">
<!--- <CFLOCATION URL="#cgi.SCRIPT_NAME###formPosition"> --->
<CFELSE>
<CFIF REFind('[A-Za-z]',textFormField)>
<CFIF REFind('[\+\*\?\[\]\^\$\(\)\{\}\|\\\/##]',textFormField) EQ 0>
<CFSET textString = wordCount(FORM.textFormField)>
<CFSET cfcVar = wordAndCountInstance.wordCount(FORM.textFormField)>
<CFSET cfVar = wordCount(FORM.textFormField)>
<!--- <CFLOCATION URL="#cgi.SCRIPT_NAME#?numberString=#URLEncodedFormat(numberString)#&cfcVar=#URLEncodedFormat(cfcVar)#&cfVar=#URLEncodedFormat(cfVar)###formPosition"> --->
</CFIF>
</CFIF>
</CFIF>
</CFIF>
<DIV>
<DIV STYLE="position:absolute;width:98%;text-align:center;">
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN="top">
<TD ALIGN="center">
<DIV STYLE="position:relative;width:800px;">
<FORM ACTION="<CFOUTPUT>#cgi.SCRIPT_NAME#</CFOUTPUT>?submit=yes" METHOD="post">
<TABLE BORDER="0" WIDTH="800" CELLPADDING="10" CELLSPACING="2">
<TR VALIGN="top">
<TD COLSPAN="2" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:bold;border:1px solid #838383;" BGCOLOR="#C0C0C0" ALIGN="center"><FONT FACE="Arial"><B>Converts a string into constituent unique word and value count pairs</B></FONT></TD>
</TR>
<TR VALIGN="top">
<TD WIDTH="300" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#FFB66C"><FONT FACE="Arial"><B>Author</B></FONT></TD>
<TD WIDTH="500" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#E2E2E2"><FONT FACE="Arial">Charles Robertson</FONT></TD>
</TR>
<TR VALIGN="top">
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#FFB66C"><FONT FACE="Arial"><B>Function</B></FONT></TD>
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#E2E2E2"><FONT FACE="Arial">wordCount</FONT></TD>
</TR>
<TR VALIGN="top">
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#FFB66C"><FONT FACE="Arial"><B>Description</B></FONT></TD>
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#E2E2E2"><FONT FACE="Arial">takes a text string as the only argument and converts it into a two dimensional array of unique words & their respective value count</FONT></TD>
</TR>
<TR VALIGN="top">
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#FFB66C"><FONT FACE="Arial"><B>Date</B></FONT></TD>
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#E2E2E2"><FONT FACE="Arial">13.05.09</FONT></TD>
</TR>
<TR VALIGN="top">
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#FFB66C"><FONT FACE="Arial"><B>Comments</B></FONT></TD>
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#E2E2E2"><FONT FACE="Arial">extensive use of list to array converion</FONT></TD>
</TR>
<TR VALIGN="top">
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#FFB66C"><FONT FACE="Arial"><B>Language</B></FONT></TD>
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#E2E2E2"><FONT FACE="Arial">CFScript</FONT></TD>
</TR>
<TR VALIGN="top">
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#FFB66C"><FONT FACE="Arial"><B>Example:</B><BR><BR>
Please type in a string.<BR>Screened characters: <I>; , - -- . " : = % £ ! @ ~</I><BR>Illegal characters: <I>+ * ? [ ] ^ $ ( ) { } | \ / #</I></FONT></TD>
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#E2E2E2"><FONT FACE="Arial"><INPUT TYPE="text" NAME="textFormField" STYLE="border-right:dimgray 1px solid;border-top:dimgray 1px solid;font-size: 10pt;border-left:dimgray 1px solid;border-bottom:dimgray 1px solid;font-family:Arial,Helvetica;background-color:transparent;background:#FFF url('images/textfield_bck.gif') repeat-x;width:350px;padding-left:5px;"><BR><BR>
<INPUT TYPE="image" SRC="images/submit.png"></FONT></TD>
</TR>
<TR VALIGN="top">
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#FFCECE"><FONT FACE="Arial"><B>Output from a CFScript function called 'wordCount', on this page</B><BR><BR>Default value: <I>'The old lady pulled her spectacles down and looked over them about the room; then she put them up and looked out under them. She seldom or never looked THROUGH them for so small a thing as a boy; they were her state pair, the pride of her heart, and were built for "style," not service -- she could have seen through a pair of stove-lids just as well.'</I></FONT></TD>
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#E2E2E2"><FONT FACE="Arial"><TABLE BORDER="0" WIDTH="400" CELLPADDING="0" CELLSPACING="2">
<TR VALIGN="top">
<TD COLSPAN="2" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:bold;">Question 1</TD>
</TR>
<TR VALIGN="top">
<TD COLSPAN="2" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:bold;"> </TD>
</TR>
<CFLOOP FROM=1 TO="#ArrayLen(cfVar)#" INDEX="i">
<TR VALIGN="top">
<TD WIDTH="150" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;"><CFOUTPUT>#cfVar[i][1]#</CFOUTPUT></TD>
<TD WIDTH="50" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;"><CFOUTPUT>#cfVar[i][2]#</CFOUTPUT></TD>
</TR>
</CFLOOP>
</TABLE></FONT></TD>
</TR>
<TR VALIGN="top">
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#FFCECE"><FONT FACE="Arial"><B>Output from a CFC function called 'wordCount', written mainly in CFScript, and encapsulated within 'wordAndCount.cfc'</B><BR><BR>Default value: <I>'The Sandman's coming in his train of cars. With moonbeam windows and with wheels of stars. So hush you little ones and have no fear. The man-in-the-moon he is the engineer.'</I></FONT></TD>
<TD STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;text-align:left;" BGCOLOR="#E2E2E2"><FONT FACE="Arial"><TABLE BORDER="0" WIDTH="400" CELLPADDING="0" CELLSPACING="2">
<TR VALIGN="top">
<TD COLSPAN="2" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:bold;">Question 1</TD>
</TR>
<TR VALIGN="top">
<TD COLSPAN="2" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:bold;"> </TD>
</TR>
<CFLOOP FROM=1 TO="#ArrayLen(cfcVar)#" INDEX="i">
<TR VALIGN="top">
<TD WIDTH="150" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;"><CFOUTPUT>#cfcVar[i][1]#</CFOUTPUT></TD>
<TD WIDTH="50" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:normal;"><CFOUTPUT>#cfcVar[i][2]#</CFOUTPUT></TD>
</TR>
</CFLOOP>
</TABLE></FONT></TD>
</TR>
<TR VALIGN="top">
<TD COLSPAN="2" STYLE="color:#000000;font-family:Arial;font-size:11px;font-weight:bold;"> </TD>
</TR>
</TABLE>
</FORM>
<DIV STYLE="position:relative;width:784px;height:400px;border:1px solid #838383;overflow-y:auto;overflow-x:hidden;padding:8px;scrollbar-arrow-color:#3C3C3C;scrollbar-track-color:#BEBEBE;scrollbar-shadow-color:#FFFFFF;scrollbar-face-color:#BEBEBE;scrollbar-highlight-color:#FFFFFF;scrollbar-darkshadow-color:#BEBEBE;scrollbar-3dlight-color:#BEBEBE;text-align:left;font-family:Arial;font-size:12px;color:#3C3C3C;word-wrap:break-word;">
<B>CFScript code as a standalone function or encapsulated within a CFC</B><BR>
<CFSET openingCFScriptTag = HTMLCodeFormat("<CFSCRIPT>")>
<CFOUTPUT>#openingCFScriptTag#</CFOUTPUT>
<CFSAVECONTENT VARIABLE="CFScriptcode">
function wordCount(inputString){
//initialise variables
var thisString = Trim(LCase(ARGUMENTS.inputString));
// add to discount list, if more rules required; characters that act as regular expression operators, need to be escaped like '.' -> '\.'
var discountList = ';/\,/--/-/\./:/\"/=/%/£/!/@/~';
var tempString = "";
var valueCountList = "";
var discountArr = ArrayNew(1);
var thisStringArr = ArrayNew(1);
var tempStringArr = ArrayNew(1);
var valueCountArr = ArrayNew(1);
var stringValueCountValueArr = ArrayNew(1);
discountArr = ListToArray(discountList,"/");
for(i = 1;i lte Arraylen(discountArr);i=i+1){
thisString = REReplaceNoCase(thisString,discountArr[i]," ","ALL");
}
thisString = REReplaceNoCase(thisString," ",",","ALL");
thisString = REReplaceNoCase(thisString,"[,]+",",","ALL");
thisStringArr = ListToArray(thisString);
for(i = 1;i lte ArrayLen(thisStringArr);i=i+1){
if(ListFind(tempString,thisStringArr[i]) EQ 0){
valueCountList = ListAppend(valueCountList,ListValueCount(thisString,thisStringArr[i]));
tempString = ListAppend(tempString,thisStringArr[i]);
}
}
tempStringArr = ListToArray(tempString);
valueCountArr = ListToArray(valueCountList);
stringValueCountValueArr = ArrayNew(2);
// two dimensional array
for(i = 1;i lte Arraylen(tempStringArr);i=i+1){
stringValueCountValueArr[i][1] = tempStringArr[i];
stringValueCountValueArr[i][2] = valueCountArr[i];
}
return stringValueCountValueArr;
}
</CFSAVECONTENT>
<CFSET CFScriptcoreCode = HTMLCodeFormat(CFScriptcode)>
<CFOUTPUT>#CFScriptcoreCode#</CFOUTPUT>
<CFSET endCFScriptTag = HTMLCodeFormat("</CFSCRIPT>")>
<CFOUTPUT>#endCFScriptTag#</CFOUTPUT>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<BR>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<BR><BR>
</DIV><BR><BR>
</DIV>
</TD>
</TR>
</TABLE>
</DIV>
</DIV>
</BODY>
</HTML>