-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordAndCount.cfc
71 lines (49 loc) · 2.4 KB
/
wordAndCount.cfc
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
<!--- 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 --->
<CFCOMPONENT DISPLAYNAME = "wordAndCount" HINT="component that takes a text string as the only argument and converts it into a two dimensional array of unique words & their respective value count">
<CFFUNCTION NAME="init" RETURNTYPE="wordAndCount" OUTPUT="false" ACCESS="public" HINT="function that initialise the wordAndCount component">
<CFRETURN this>
</CFFUNCTION>
<CFFUNCTION NAME="wordCount" RETURNTYPE="array" OUTPUT="true" ACCESS="public" HINT="function that converts an integer to its english text equivalent">
<CFARGUMENT NAME="inputString" TYPE="string" REQUIRED="yes" HINT="argument that is a string, which is converted into a two dimensional array of unique words & their respective value count">
<CFSCRIPT>
//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>
</CFFUNCTION>
</CFCOMPONENT>