| 1 | """ |
|---|
| 2 | wordcount.cobra |
|---|
| 3 | |
|---|
| 4 | > cd \path\to\directory\of\wordcount.cobra |
|---|
| 5 | > cobra -c wordcount.cobra |
|---|
| 6 | > wordcount \path\to\some\textfile |
|---|
| 7 | ..\Docs\ReleaseNotes\Cobra-0.0.2.text: |
|---|
| 8 | chars = 1258 |
|---|
| 9 | words = 180 |
|---|
| 10 | sentences = 18 |
|---|
| 11 | lines = 61 |
|---|
| 12 | blank lines = 20 |
|---|
| 13 | """ |
|---|
| 14 | |
|---|
| 15 | class WordCounter |
|---|
| 16 | """ |
|---|
| 17 | This class stores the counts accumulated, in other words, the number of: |
|---|
| 18 | characters |
|---|
| 19 | words |
|---|
| 20 | sentences |
|---|
| 21 | lines |
|---|
| 22 | blank lines |
|---|
| 23 | |
|---|
| 24 | Several `countFoo` methods provide the ability to count: |
|---|
| 25 | * a file with a given name |
|---|
| 26 | * the contents of a TextReader |
|---|
| 27 | * any string |
|---|
| 28 | * a line at a time |
|---|
| 29 | |
|---|
| 30 | The count methods can be invoked as many times as you like. |
|---|
| 31 | |
|---|
| 32 | A word counter can be reused by invoking `reset`. |
|---|
| 33 | """ |
|---|
| 34 | |
|---|
| 35 | test |
|---|
| 36 | assert WordCounter().countText('').numWords==0 |
|---|
| 37 | |
|---|
| 38 | c = WordCounter().countText('aoeu') |
|---|
| 39 | assert c.numWords == 1 |
|---|
| 40 | assert c.numSentences == 0 |
|---|
| 41 | assert c.numLines == 1 |
|---|
| 42 | |
|---|
| 43 | c = WordCounter().countText('aoeu qwerty') |
|---|
| 44 | assert c.numWords == 2 |
|---|
| 45 | assert c.numSentences == 0 |
|---|
| 46 | assert c.numLines == 1 |
|---|
| 47 | |
|---|
| 48 | c = WordCounter().countText('aoeu\nqwerty') |
|---|
| 49 | assert c.numWords == 2 |
|---|
| 50 | assert c.numSentences == 0 |
|---|
| 51 | assert c.numLines == 2 |
|---|
| 52 | |
|---|
| 53 | c.reset |
|---|
| 54 | assert c.numWords == 0 |
|---|
| 55 | assert c.numLines == 0 |
|---|
| 56 | |
|---|
| 57 | sw = StringWriter() |
|---|
| 58 | print to sw |
|---|
| 59 | c.dump |
|---|
| 60 | s = sw.toString |
|---|
| 61 | assert s.contains('words') |
|---|
| 62 | assert s.contains('lines') |
|---|
| 63 | |
|---|
| 64 | var _numChars = 0 |
|---|
| 65 | var _numWords = 0 |
|---|
| 66 | var _numSentences = 0 |
|---|
| 67 | |
|---|
| 68 | var _numLines = 0 # all lines, even blank |
|---|
| 69 | var _numBlankLines = 0 |
|---|
| 70 | |
|---|
| 71 | get numChars from var |
|---|
| 72 | |
|---|
| 73 | get numWords from var |
|---|
| 74 | |
|---|
| 75 | get numSentences from var |
|---|
| 76 | |
|---|
| 77 | get numLines from var |
|---|
| 78 | |
|---|
| 79 | get numBlankLines from var |
|---|
| 80 | |
|---|
| 81 | def dump |
|---|
| 82 | print 'chars = [.numChars]' |
|---|
| 83 | print 'words = [.numWords]' |
|---|
| 84 | print 'sentences = [.numSentences]' |
|---|
| 85 | print 'lines = [.numLines]' |
|---|
| 86 | print 'blank lines = [.numBlankLines]' |
|---|
| 87 | |
|---|
| 88 | def countText(text as String) as WordCounter |
|---|
| 89 | """ |
|---|
| 90 | Counts the chars, words, etc. of any arbitrary string. |
|---|
| 91 | """ |
|---|
| 92 | text = text.replace('\r\n', '\n') # windows |
|---|
| 93 | text = text.replace('\r', '\n') # mac |
|---|
| 94 | for line in text.split(c'\n') |
|---|
| 95 | .countLine(line) |
|---|
| 96 | return this |
|---|
| 97 | |
|---|
| 98 | def countLine(line as String) as WordCounter |
|---|
| 99 | """ |
|---|
| 100 | Counts a single line of text. |
|---|
| 101 | """ |
|---|
| 102 | _numChars += line.length |
|---|
| 103 | line = line.trim |
|---|
| 104 | _numLines += 1 |
|---|
| 105 | if not line.length |
|---|
| 106 | _numBlankLines += 1 |
|---|
| 107 | else |
|---|
| 108 | while '..' in line |
|---|
| 109 | line = line.replace('..', '.') # so '...' does not make for 3 sentences |
|---|
| 110 | while '!!' in line |
|---|
| 111 | line = line.replace('!!', '!') |
|---|
| 112 | while '??' in line |
|---|
| 113 | line = line.replace('??', '?') |
|---|
| 114 | _numSentences += _countChars(line, c'.') + _countChars(line, c'!') + _countChars(line, c'?') |
|---|
| 115 | _numWords += line.split(nil).length |
|---|
| 116 | return this |
|---|
| 117 | |
|---|
| 118 | def countTextReader(tr as TextReader) as WordCounter |
|---|
| 119 | while true |
|---|
| 120 | line = tr.readLine |
|---|
| 121 | if line, .countLine(line) |
|---|
| 122 | else, break |
|---|
| 123 | return this |
|---|
| 124 | |
|---|
| 125 | def countFileNamed(fileName as String) as WordCounter |
|---|
| 126 | using tr = File.openText(fileName) |
|---|
| 127 | .countTextReader(tr) |
|---|
| 128 | return this |
|---|
| 129 | |
|---|
| 130 | def reset |
|---|
| 131 | _numChars = 0 |
|---|
| 132 | _numWords = 0 |
|---|
| 133 | _numSentences = 0 |
|---|
| 134 | _numLines = 0 |
|---|
| 135 | _numBlankLines = 0 |
|---|
| 136 | |
|---|
| 137 | def _countChars(s as String, c as char) as int |
|---|
| 138 | test |
|---|
| 139 | wc = WordCounter() |
|---|
| 140 | assert wc._countChars('', c'x')==0 |
|---|
| 141 | assert wc._countChars('x', c'x')==1 |
|---|
| 142 | assert wc._countChars('X', c'x')==0 # case sensitive |
|---|
| 143 | assert wc._countChars(' ! ! ', c'!')==2 |
|---|
| 144 | body |
|---|
| 145 | count = 0 |
|---|
| 146 | for ch in s, if c==ch, count += 1 |
|---|
| 147 | return count |
|---|
| 148 | |
|---|
| 149 | def main |
|---|
| 150 | args = CobraCore.commandLineArgs |
|---|
| 151 | if args.count < 2 |
|---|
| 152 | print ns'usage: wordcount FILES' |
|---|
| 153 | return |
|---|
| 154 | |
|---|
| 155 | wc = WordCounter() |
|---|
| 156 | for fileName in args[1:] |
|---|
| 157 | print '[fileName]:' |
|---|
| 158 | wc.countFileNamed(fileName) |
|---|
| 159 | wc.dump |
|---|
| 160 | wc.reset |
|---|