Computer Science Canada

Sentence Count

Author:  sockoo [ Wed May 11, 2005 12:07 pm ]
Post subject:  Sentence Count

Hello All .. This will be my first post soo i'd like to say hi before i actualy ask for help .. i'll try and contribute more to the forums .. but i'v started something new in my class that .. im not to clear on .. ok ok enough of the small talk here's my question:
I have to make a word count application which allows the user to enter a sentence and then count and display the number of words entered.
Extension - Analyze the paragraph by not only counting the words but by displaying the number of 1 character words, 2 character words, 3 character words

this is what i got so far all i need help with is getting the 1..2..3 charcacter words

code:

var sentence : string
put "Word Count"
put "Please enter a sentence to count how many letters are in it"
get sentence : *
put "The Length Of Your Sentence Is "   ,  length(sentence)

Author:  jamonathin [ Wed May 11, 2005 1:22 pm ]
Post subject: 

Since its your first post and you dont know, its ok, but we don't do peoples homework on here, we can give you clues and tips, but we wont write you all of the actual code.

Anyways, think of how a sentence is made. Inbetween each word there's a space, unless it's the end of the sentence. Run a for loop for the length(sentence) and have it check where the spaces are, and how far apart, thats how you can find out where the words are.

Author:  sockoo [ Wed May 11, 2005 7:24 pm ]
Post subject: 

Thank you, i didnt want you to write me my code .. what would i learn that way ? .. i just wanted a sorce of direction or help of how to accomplish what i was giving and thanks for the help .. i'll post back with furthur updates or .. if i need any more help

Author:  Cervantes [ Thu May 12, 2005 3:20 pm ]
Post subject: 

If you're not too comfortable with string manipulation, check the tutorial to get some explainations and examples.
Ultimately, you don't want to count the number of spaces. The reason is that lots of people (myself included) leave two spaces after the end of my sentances. If you're only searching for spaces, if the user types this way, the output for the number of words will be incorrect. What you actually want to do (I would assume) is search for characters between spaces.

Author:  sockoo [ Fri May 13, 2005 8:52 am ]
Post subject: 

im still not having much luck .. i understand how to find where the space is but .. how would i go about finding .. what many characters are before the space ?

code:

for i : 1 .. length(sentence)
if sentence(i) = " " then


this is what i have but .. im not clear what to do after this obviously i'v found where the space is soo would i find the length of the sentence 1 .. i ? to find out how many charcaters is in the word ? .. and run if statements 1 .. 3 to find if it has 1 charc 2 charcs or 3 and so on ? ..

Author:  jamonathin [ Sun May 15, 2005 7:30 pm ]
Post subject: 

Here's the answer you want. I'm guessing you dont have it by now since you haven't posted it.
Turing:

var sentence : string
put "Enter a Sentence"
get sentence : * %Getting sentence with spaces ( : * )
var edit : string := " " + sentence %Adding a space to the begining of the sentence
var words : flexible array 1 .. 0 of string %All of the words
procedure wordcount (s : string)
    for i : 1 .. length (s)
        if s (i) = " " then %If there's a space
            new words, upper (words) + 1 %making the array 'words' 1 larger
            words (upper (words)) := "" %Making it equal to nothing so we can add to it
            for q : i + 1 .. length (s) %Add whatever other characters to the if statement as you please
                if s (q) = " " or s (q) = "," or s (q) = "." or s (q) = "?" or s (q) = "!" then
                    exit %Exiting the for loop
                else
                    words (upper (words)) += s (q) %Adding to the current word
                end if
            end for
            put words (upper (words)) %Displaying the word
        end if
    end for
end wordcount
cls
wordcount (edit)

Author:  sockoo [ Sun May 15, 2005 7:35 pm ]
Post subject: 

hmm thanks for the help but that isnt quiet what i was trying to ahieve ...
lets say the user enters " Keep It Simple Stupid "
sentence length: 22 (with spaces)
4 charc words: 1
2 charc words: 1
6 charc words: 2

Author:  MysticVegeta [ Mon May 16, 2005 7:03 am ]
Post subject: 

Check the Cervantes link, He has a part in the tut that gives how you can separate the words, and then you can go -> if length(seperateword) = 2 then
put 2
end if
Since i cant post the code, i will ask yu to check it out
http://www.compsci.ca/v2/viewtopic.php?t=8229

Author:  jamonathin [ Mon May 16, 2005 7:48 am ]
Post subject: 

Oh yeah I forgot about that tutorial, definately check it out, it has everything you need to know.

Author:  sockoo [ Mon May 16, 2005 9:10 am ]
Post subject: 

i'v read that tutorial over about 5 times ... but i'll re read it when i get home .. and go from there Rolling Eyes . thanks for the help guys

Author:  Notoroge [ Mon May 16, 2005 2:34 pm ]
Post subject: 

code:
var asdf, yummy : string
var countwords : int := 1
get asdf : *
yummy := asdf
asdf := "+" + yummy + " "
put "Sentence is ", length (asdf) - 2, " characters long."
for i : 1 .. length (asdf)
    if asdf (i) = " " then
        countwords := countwords + 1
    end if
end for
countwords := countwords - 1
put "There are ", countwords, " words in the sentence."
var words : array 1 .. countwords of string
var count, temp : int := 1
for i : 1 .. length (asdf)
    if asdf (i) = " " then
        words (count) := asdf (temp + 1 .. i - 1)
        count := count + 1
        temp := i
    end if
end for
temp := 0
for i : 1 .. countwords
    count := length (words (i))
    if count > temp then
        temp := count
    end if
end for
var lengths : array 1 .. temp of int
for i : 1 .. temp
    lengths (i) := 0
end for
for i : 1 .. countwords
    for r : 1 .. temp
        if length (words (i)) = r then
            lengths (r) := lengths (r) + 1
        end if
    end for
end for
for i : 1 .. temp
    if lengths (i) not= 0 then
        put "There are ", lengths (i), ", ", i, " lettered words."
    end if
end for

It's not elegant; and probably the dirtiest way to do it, but it took me ten minutes to make and it works. Very Happy
Update: It does crash when you put two or more spaces consecutively. This is only to point you in the right direction though. Kinda the concept of what you have to do. Fix it however you want.

Author:  Notoroge [ Mon May 16, 2005 2:46 pm ]
Post subject: 

jamonathin, I just thought of something. Instead of trapping for most commonly used punctuation notes, why not do something like,
code:
if ord (word : string) => [lower bound ASCII limit of letters] : int and ord (word : string) =< [upper bound ASCII limit of letters] : int then
Just a thought.

Author:  Token [ Mon May 16, 2005 3:28 pm ]
Post subject: 

hmm i just got an idea, why not export the sentence to a text file and then bring it in one word at a time, that way you could ealsily count the words, and as each word is in the temporary variable that you set up it could count the number of letters. if u dont understand what i'm saying or u need help let me kno

Author:  sockoo [ Mon May 16, 2005 4:58 pm ]
Post subject: 

Notorage , Omg! , u'v made my program to a dot Razz .. everything it was suppose to be .. except i learned nothing from it Razz .. even tho it was nice of you .. i would of rather a source of direction how to do it .. rather then u just giving me the answer .. but thank you Smile ! , now gotta read through ur code and make sure i understand everything you did and would be able to do it my self .. if needed in the furture!

Author:  Notoroge [ Mon May 16, 2005 5:03 pm ]
Post subject: 

I wanted to see if I could do it. I also purposefully left out any documentation and didn't bother to clean up the code or error-trap. Because I figured I'd might aswell make you read through everything, understand it, and clean it up yourself. Wink

Author:  Cervantes [ Tue May 17, 2005 4:52 pm ]
Post subject: 

Token: that takes far too much resources. A word count really should not have to access a file. It is a neat idea, though inefficient.

Here's a function that uses the boolean variable idea I was talking about earlier (was it this thread?):

Turing:

var sentence : string

function wordcount (S : string) : int
    var s := S + " " % adds a padding at the end
    var numOfWords := 0 %keep track of the number of words
    var foundSpace := true  %boolean to store whether to look for another space or skip ahead until other characters are found
    for i : 1 .. length (s)
        if s (i) = " " then
            if ~foundSpace then
                numOfWords += 1
                foundSpace := true
            end if
        else    %if a character other than space is found, we will want to start looking for the next space again.
            foundSpace := false
        end if
    end for
    result numOfWords
end wordcount

put "Enter a Sentence"
get sentence : * %Getting sentence with spaces ( : * )
put wordcount (sentence)

This was a progression from someone's code above, so that would explain some of the same comments/variable names. Anywho, I hope you follow that.

Author:  Token [ Tue May 17, 2005 5:07 pm ]
Post subject: 

umm actually it is pretty simple, a lot simpler than your idea. only 20 lines of code, and it deletes the resource file automaticly.

code:

var s : string
var file, words : int
loop
    words := 0
    put "\nSentence: " ..
    get s : *

    open : file, "temp.txt", put
    put : file, s
    close : file

    open : file, "temp.txt", get
    loop
        exit when eof (file)
        get : file, s
        words += 1
    end loop
    close : file
    File.Delete ("temp.txt")
    put "\nWords: ", words
end loop

Author:  Cervantes [ Tue May 17, 2005 6:09 pm ]
Post subject: 

No, you're missing the point. I'm not saying it isn't easier to code, I'm saying it's inefficient. There's no need to open a file, write stuff into it, then read it back. The only reason to write to files is to store data. You're not storing data, given that you are deleting the file a few lines later. Do you think Microsoft Word takes the data the user has entered and sends it off to a .txt (or better yet, a .doc!) and then retrevies it all back just to get a word count? That takes far too many resources, given the simple nature of the task.

Oh, and my code was also twenty lines.

Author:  Token [ Tue May 17, 2005 7:42 pm ]
Post subject: 

but it is so much simpler to add the counting of the letters of the words, look.

code:

var s : string
var file, words : int
var letters : array 1 .. 9 of int

for i : 1 .. 9
    letters (i) := 0
end for

loop
    words := 0
    put "\nSentence: " ..
    get s : *

    open : file, "temp.txt", put
    put : file, s
    close : file

    open : file, "temp.txt", get
    loop
        exit when eof (file)
        get : file, s
        words += 1
        letters (length (s)) += 1
    end loop
    close : file

    File.Delete ("temp.txt")
    put "\nWords: ", words

    for i : 1 .. 9
        if letters (i) not= 0 then
            put "Words with ", i, " letters: ", letters (i)
        end if
    end for

    put "All others had none."
end loop


whereas yours you would have to wright a whole new section of code or even a new function to return those values. and what is the problem with creating a text file and deleting it? it works the same as your program. and of corse microsoft word doesnt, but it isnt written in turing Laughing

Author:  Cervantes [ Tue May 17, 2005 8:18 pm ]
Post subject: 

Token wrote:
but it is so much simpler to add the counting of the letters of the words, look.

You need to understand the difference between "simple" and "good". They do not always go hand in hand.

Token wrote:

whereas yours you would have to wright a whole new section of code or even a new function to return those values.

I could just have my function return an array of values.

Token wrote:

and what is the problem with creating a text file and deleting it? it works the same as your program.

The output may be correct, but the steps you take to get that output are wrong. Remember that this is simply a tidbit of code to count words. If we were making a word processor, there would be so many other things like this. For example, counting lines, counting total characters (with and without spaces), counting paragraphs, counting pages, etc. I realize that with some of these you couldn't use the file techneque, but the point still stands: your code would become grotesque as you constantly create and delete files just to determine this information.

Token wrote:

and of corse microsoft word doesnt, but it isnt written in turing Laughing

That makes absolutely no difference.

Author:  jamonathin [ Wed May 18, 2005 7:51 am ]
Post subject: 

I'd quit token, lol. You won't win. Razz

Author:  Notoroge [ Wed May 18, 2005 7:56 am ]
Post subject: 

Just for the sake of argument, I think what he was getting at is that this is a simple Turing program for school that will never be required to process the complexity of information that a word-processor does, and thus in this case it's efficient enough to get by and simple.

I agree with you in that to count the words by doing this method for OO.o or MS-WORD, it would be rediculous to do so. But for the task at hand, it's good enough.

Author:  Token [ Wed May 18, 2005 11:21 am ]
Post subject: 

yay some support lol, thats exactly what i was saying we arent building a full word processor here just somthing used to analyse a sentence. and turing is a very basic language so its hard to do some of the things that you could do in C++ or whatever msword was created in. the only reason i feel this is better is because you can add so many options easily. also its all in oppinion what is 'good' its not like one takes drastically more time than the other so i dont see a problem with my code.


: