|
Author |
Topic: calculating frequency of word lengths (Read 285 times) |
|
irgeorge
|
calculating frequency of word lengths
« on: Nov 27th, 2004, 1:20am » |
|
I'm trying to create a little applet that loads a text file and then counts up and shows the frequency of word lengths in the file. So far I have this: Code: String words = "car three bear bee to a is"; String list[] = splitStrings(words); int one = 0; int two = 0; int three = 0; int four = 0; int five = 0; for(int i=0; i< list.length; i++) { int word = list[i].length(); //println(list[i] + word); if(word == 1) { one++; } else if (word == 2) { two++; } else if (word == 3) { three++; } else if (word == 4) { four++; } else if (word == 5) { five++; } } println("Single letter words:" + one); println("Two letter words:" + two); println("Three letter words:" + three); println("Four letter words:" + four); println("Five letter words:" + five); |
| Which even I know isn't very efficient, I've tried the loadStrings(); function but I can't seem to get it working. I need advice on how to load a text file and how not to have to declare every possible word length as a variable. Anyhelp much appreciated.
|
|
|
|
st33d
|
Re: calculating frequency of word lengths
« Reply #1 on: Nov 27th, 2004, 3:19am » |
|
In the reference section I read this code: Code: String lines[] = loadStrings("list.txt"); println("there are " + lines.length + " lines"); for (int i=0; i < lines.length; i++) { println(lines[i]); } |
| Thought, well, okay I'll have a go then. Pasted it into Processing. Saved it as list.pde and then went to the sketchbook folder and into default. Opened up the "list" folder. Opened up the "data" folder. Created a new text file with the ole right click new txt file please. Wrote some gibberish and pressed return a few times. Saved the file. Ran the Processing code. First time I've used the command and it worked. The only trouble I can see you having after that is it creates a new array division each time it detects a new line in the txt file. Can't be that hard. Hope this is a little help.
|
I could murder a pint.
|
|
|
irgeorge
|
Re: calculating frequency of word lengths
« Reply #2 on: Nov 27th, 2004, 12:35pm » |
|
Whoops, didn't notice that it did it line by line. Thanks
|
|
|
|
st33d
|
Re: calculating frequency of word lengths
« Reply #3 on: Nov 27th, 2004, 5:33pm » |
|
A little less long winded than your first code would be: Code: String words = "car three bear bee to a is"; String list[] = splitStrings(words); int [] count = new int[5]; for (int i = 0; i<count.length; i++){ count[i] = 0; } for(int i = 0; i< list.length; i++) { int word = list[i].length(); count[word-1]++; } for (int i = 0; i<count.length; i++){ println((i+1)+" letter words:"+count[i]); } |
| Any multiple turn into an array. Any pattern run through a for loop. Keep (insert home town) tidy.
|
« Last Edit: Nov 27th, 2004, 5:33pm by st33d » |
|
I could murder a pint.
|
|
|
irgeorge
|
Re: calculating frequency of word lengths
« Reply #4 on: Nov 27th, 2004, 8:20pm » |
|
Fantastic, I've been trying to do somthing like that. Just trying to get my head around loops. Thanks! Edit: Just tried your code, only problem is that if I use if say a piece of text with 200 words in it, it would output "200 letter words: 0" etc. I'm trying to work out how to fix it, but if you've got any ideas...
|
« Last Edit: Nov 27th, 2004, 9:16pm by irgeorge » |
|
|
|
|
irgeorge
|
Re: calculating frequency of word lengths
« Reply #5 on: Nov 27th, 2004, 11:03pm » |
|
Oh wait, I've got it sorted in a slightly dodgey way: (see comment) Code: String lines[] = loadStrings("independent.txt"); String words = join(lines, " "); String list[] = splitStrings(words); //every things ok as long as source text is not less than 20 (see below) int [] count = new int[20]; for (int i = 0; i<count.length; i++){ count[i] = 0; } for(int i = 0; i< list.length; i++) { int word = list[i].length(); count[word]++; } for (int i = 0; i<count.length; i++){ println((i+1)+" letter words:"+count[i]); } |
|
|
|
|
|
|