WordCount
#11
(06-24-2018, 03:04 PM)ekultek Wrote:
(06-23-2018, 07:31 PM)r0z Wrote: Javascript:

Spoiler(Show)
Code:
const fs = require('fs'); 

function readFile(file) { 
    var text = fs.readFileSync(file).toString().split(' '); 
    return text.length; 
}


This fails if there is extra whitespace in the sentences. IE try it with this:

Code:
The    cat in    the hat    jumped over    the        fat    rat

Solved:
Spoiler(Show)
Code:
const fs = require('fs');

function readFile(file) {
   var text = fs.readFileSync(file).toString().split(' ').filter(x => x != '');
   return text.length;
}
Smile
Reply
#12
I would just do a splitter with regex: "\\s*[ ]\\s*".
This would get how many words there are splitting all of them with one or several spaces/tabs/new lines...
Then get splitted.length() and that's it Tongue
Reply
#13
(06-24-2018, 04:57 PM)r0z Wrote:
(06-24-2018, 03:04 PM)ekultek Wrote:
(06-23-2018, 07:31 PM)r0z Wrote: Javascript:

Spoiler(Show)
Code:
const fs = require('fs'); 

function readFile(file) { 
    var text = fs.readFileSync(file).toString().split(' '); 
    return text.length; 
}


This fails if there is extra whitespace in the sentences. IE try it with this:

Code:
The    cat in    the hat    jumped over    the        fat    rat

Solved:
Spoiler(Show)
Code:
const fs = require('fs');

function readFile(file) {
   var text = fs.readFileSync(file).toString().split(' ').filter(x => x != '');
   return text.length;
}
Smile


Code:
This\t. Is a test to.   Make a monkey wrench\t\tand piss you off\nbecause\n\t\twhy not 

Broken again Smile
Reply
#14
(06-28-2018, 10:45 PM)ekultek Wrote:
(06-24-2018, 04:57 PM)r0z Wrote:
(06-24-2018, 03:04 PM)ekultek Wrote:
(06-23-2018, 07:31 PM)r0z Wrote: Javascript:

Spoiler(Show)
Code:
const fs = require('fs'); 

function readFile(file) { 
    var text = fs.readFileSync(file).toString().split(' '); 
    return text.length; 
}


This fails if there is extra whitespace in the sentences. IE try it with this:

Code:
The    cat in    the hat    jumped over    the        fat    rat

Solved:
Spoiler(Show)
Code:
const fs = require('fs');

function readFile(file) {
   var text = fs.readFileSync(file).toString().split(' ').filter(x => x != '');
   return text.length;
}
Smile


Code:
This\t. Is a test to.   Make a monkey wrench\t\tand piss you off\nbecause\n\t\twhy not 

Broken again Smile

Nop, tabulation are spaces, newlines are technically spaces and carriage return \r is just to back to beginning, but if you use the codes i'll have to write a parser, lol.
Reply
#15
Tabs are still considered spaces, programmatically a tab character translates to a space, new lines translate to a \00 not a space (line endings in C, gotta remember everything is based off of C, well most things). Double tab characters are translated into multiple spaces. Your script will read them as ascii, not as a space
Reply
#16
If my script is reading as ascii them it will read \00(that you said new lines are translated) as NULL, is what it means...

my script will cast them to a string, them technically it will read as spaces also.

Most imperative languages are C-based, indeed, but JS are multiparadigm, there are a lot of differences.
Reply
#17
Quick and easy in Python:

Code:
from string import punctuation
from sys import argv


def count_words(text):
   for char in punctuation:
       text = text.replace(char, "")
   return len(text.split())


if __name__ == "__main__":
   if len(argv) == 1:
       print("USAGE: python %s file.txt" % argv[0])
       exit(1)
   try:
       text = open(argv[1]).read()
       word_count = count_words(text)
       print("Word Count: %d" % word_count)
   except IOError as e:
       print(str(e))
Reply
#18
Can i cheat.....

me@box# wc -w wctest.txt
10 wctest.txt
me@box# cat wctest.txt
Code:
The    cat in    the hat    jumped over    the        fat    rat
Reply
#19
Code:
import sys

f = open(sys.argv[1],'r')
counter = 0

for item in f:
    if item.split() != []:
        counter += len(item.split())

print("Words in file:",counter)
Reply