• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Readability Score

 
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This task is from JetBrains Academy/Hyperskill.
reference https://hyperskill.org/
Description
In this stage, you will program the Automated readability index. It was introduced in 1968 and a lot of research works rely on this.The index is calculated by the following formula:

score = 4.71 * {characters}/{words} + 0.5 * {words}/{sentences} - 21.43

You can look at different ages corresponding to the different scores by the table in this article.

Also, your program should read a file instead of typing a text manually. You should pass the filename through the command line arguments.

The program should output the score itself and an approximate age needed to comprehend the text.
Use rounding function to calculate the score as integer.

You should also print how many characters, words, and sentences the text has.

The number of characters is any visible symbol (so, in the real text it's everything except space, newline "\n" and tab "\t").

Notice, that the text can contain multiple lines, not just a single line like in the previous stages. You should analyze all the lines.



Calling for review..
 
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Expunge the word “float” from your vocabulary. [At least whilst programming.]
Why have you got so many instances of the keyword static?

Divide your task into smaller parts. You need to read from the file before you do anything else, so show us reading on its own. Don't mix reading and anything else. Do the other tasks, like counting words and sentences (which I think you are doing wrongly) later.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is your class called "Main" ? Reading the name of this class does not tell me what it does .
Also you have a lot of code in your main method <-- It's a link
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Expunge the word “float” from your vocabulary. [At least whilst programming.]
Why have you got so many instances of the keyword static?

Divide your task into smaller parts. You need to read from the file before you do anything else, so show us reading on its own. Don't mix reading and anything else. Do the other tasks, like counting words and sentences (which I think you are doing wrongly) later.



I used float because double gives me wrong value/computation, I used double initially.

I will work on the other recommendations, thank you for your time
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:Why is your class called "Main" ? Reading the name of this class does not tell me what it does .
Also you have a lot of code in your main method <-- It's a link


I cloudn't change the name of the class because it was part of the skeleton code.
 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kelvin Okornoe wrote:. . . double gives me wrong value/computation . . .

In which case a float will be worse. You are getting wrong values because you aren't counting or calculating correctly.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Kelvin Okornoe wrote:. . . double gives me wrong value/computation . . .

In which case a float will be worse. You are getting wrong values because you aren't counting or calculating correctly.


wow!, I need to known what am probably doing wrong.

But my code did pass some test from JetBrains.
 
Bartender
Posts: 5567
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does this line in your opening post mean:
Notice, that the text can contain multiple lines, not just a single line like in the previous stages. You should analyze all the lines.

If the file is:

I was reading a
book. But when
I got sleepy, I
went to bed.


do we have two sentences?
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:What does this line in your opening post mean:
Notice, that the text can contain multiple lines, not just a single line like in the previous stages. You should analyze all the lines.

If the file is:

I was reading a
book. But when
I got sleepy, I
went to bed.


do we have two sentences?


No, am getting 5 sentences...something is wrong.
Will try and fix it.
Thank you
 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Analyse the whole of the text, not separate lines.
 
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Style point:
since some of the defined variable names don't tell the context what do they represent, just make the readability worse, I'd suggest to have them inlined in the formulae.

That would remove few lines of very hardly readable lines of code. I have in mind the following ones:

Often when the code is cleaner in terms of semantics, mechanics (minimal noise with unnecessary stuff), exemplary formatting and indentation - problem is much easier to solve and with much less frustration.

Comments like this one:

It doesn't tell much, quite opposite actually, it makes you wonder where the actual file reading is and where the calculation starts. What is the direct relation between them so they aren't separated in different methods (ignore invalid syntax):

Once you decompose code into self explaining methods along with their clear names, you figure out that such type of comments become redundant, so you can remove them, and as a bonus you get less places to maintain when the code changes.

Keep code clean and tidy as you write, at least to a bigger portion of it so you don't get lost on what's going on.

Such code as it is now is not only hard to understand, it is also hard to discuss, because everthing is in one place, one method - luckily we have line numbers one can refer to.
 
Piet Souris
Bartender
Posts: 5567
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Kelvin

I had a closer look at your code, and I think it is correct. Recognizing a sentence by ? !  and .  seems very reasonable.
 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not if you divide individual lines. That is how your code block became five sentences rather than two.
 
Liutauras Vilda
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Continuing on the points Piet and Campbell just mentioned, @Kelvin, do you think potentially would be easier if after reading the whole file you'd make everything as one line first?

Not necessarily saying it is the way to go, but certainly one of options. Make the input look in the way you thought it actually is until now.
 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would probably join the whole thing into one line, but there are other ways to solve that problem.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A total refactor and bug solved.



Thank for the assistance, looking forward to more review
 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Afraid I don't like the technique of getting rid of all the spaces. There must be a simpler way to count sentences than that. Much simpler.
Similarly I don't like that way of counting printing characters. The definition of line end characters given in your first post is incorrect, and won't work on a Windows® machine.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Afraid I don't like the technique of getting rid of all the spaces. There must be a simpler way to count sentences than that. Much simpler.
Similarly I don't like that way of counting printing characters. The definition of line end characters given in your first post is incorrect, and won't work on a Windows® machine.



Any ideas on how to count the sentences? Please.
 
Liutauras Vilda
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP

Run through your sentences counting this nonsensical: "Done! A boy with toy. Really? Yes... Oh boy!"

I counted 5 sentences. How many you got?
 
Liutauras Vilda
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:How many you got?


No need to answer
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:@OP

Run through your sentences counting this nonsensical: "Done! A boy with toy. Really? Yes... Oh boy!"

I counted 5 sentences. How many you got?



haha!, I got 7 from my code...It should be five.

The bug is probably coming from the regexp.

Thank you soo much, I will fix it.
 
Liutauras Vilda
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes...


Some help. This is an offending part which screws up things for you. And that happens only when after ... there is some more text.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Current Implementation. Bug fixed!

 
Liutauras Vilda
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kelvin Okornoe wrote:Current Implementation


As Campbell asked, what do you achieve by removing all the spaces before counting sentences? Perhaps trim() would be enough?
 
Liutauras Vilda
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This bit. String has length() method. So perhaps you don't need to convert to char array too.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:


This bit. String has length() method. So perhaps you don't need to convert to char array too.



Yes!, That will be more efficient.
Thank you
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Kelvin Okornoe wrote:Current Implementation


As Campbell asked, what do you achieve by removing all the spaces before counting sentences? Perhaps trim() would be enough?



I am not getting the understanding. Please throw more light
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I allowed to reference the code from github? So I don't keep re-posting the code here.
 
Liutauras Vilda
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. coderanch is a forum! Cozy one.
2. coderanchisaforum!Cozyone.

When you split by ! you get array with elements:
1. [coderanch is a forum] [Cozy one.]
2. [coderanchisaforum] [Cozyone.]

So what do you achieve by removing all the spaces?

Only the first and last spaces might affect the result, so really trim() (removes leading and trailing spaces) might be the wise thing to do right after reading the file.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:1. coderanch is a forum! Cozy one.
2. coderanchisaforum!Cozyone.

When you split by ! you get array with elements:
1. [coderanch is a forum] [Cozy one.]
2. [coderanchisaforum] [Cozyone.]

So what do you achieve by removing all the spaces?

Only the first and last spaces might affect the result, so really trim() (removes leading and trailing spaces) might be the wise thing to do right after reading the file.



Okay, I think I get the idea.

Thank you
 
Liutauras Vilda
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kelvin Okornoe wrote:Am I allowed to reference the code from github? So I don't keep re-posting the code here.


You are, just not all people will be interested to click on the link and go "somewhere". And to keep discussion and the tangible asset on the forum might be useful for future readers, while your Git repository technically can get demolished at any given point in time.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Kelvin Okornoe wrote:Am I allowed to reference the code from github? So I don't keep re-posting the code here.


You are, just not all people will be interested to click on the link and go "somewhere". And to keep discussion and the tangible asset on the forum might be useful for future readers, while your Git repository technically can get demolished at any given point in time.



I understand. Thank you,
 
Liutauras Vilda
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the moment let's assume your calculations are correct.

Do you think would be better if you'd create a class similar reading to TextStatistics, to which you could pass potentially a text or file and be able to:

TextStatistics myFavouriteBook = new TextStatistics(text);
or
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Current state of the code.

 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:For the moment let's assume your calculations are correct.

Do you think would be better if you'd create a class similar reading to TextStatistics, to which you could pass potentially a text or file and be able to:

TextStatistics myFavouriteBook = new TextStatistics(text);
or



Yes! I will do that.
Thank you
 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kelvin Okornoe wrote:Am I allowed to reference the code from github? So I don't keep re-posting the code here.

Afraid not. It would take too long to find the current version, and some people are reluctant to go to outside links.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am adding a method to count the number of syllables in a word.
This is my implementation so far.



I need a review to make it better and more rigorous.
Thank you
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Expunge the word “float” from your vocabulary. [At least whilst programming.]
Why have you got so many instances of the keyword static?

Divide your task into smaller parts. You need to read from the file before you do anything else, so show us reading on its own. Don't mix reading and anything else. Do the other tasks, like counting words and sentences (which I think you are doing wrongly) later.



I'm not sure this thread is still being watched. I'm an old (however 70 is the new 50) IBM assembly language programer from the early 80s learning java. I've been at it several years, but rate myself a beginner. I love these coderanch exercises? I look for them every month and learn so much. My question is about float. It was never addressed in this exercise. I'm interested in what is wrong here. Better solution?
 
Saloon Keeper
Posts: 15731
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For most applications that have a legitimate use case for floating point types (which is rare to begin with), double is the default to use. Most processors have native support for double precision floating points.

The keyword float should probably only be used when you have a valid use case for floating point types in the first place (again, not as common as people think) and you know that the target hardware doesn't have native support for double precision, or memory, networking or storage is extremely expensive.

An example of where float might be useful is when accessing sensors or controlling servomotors in small embedded devices.
 
Sheriff
Posts: 28330
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And there are a few classes in the standard API which use float values in some way. The java.awt.Font class comes to mind and I expect there are other java.awt classes which are guilty of using float values.
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic