malikatwork
asked on
Reading Text file
I this program I have to open a text file (comics.txt) and count the number of words, sentences and syllables.
My problem is that I do not know how to read through the text to find sentence enders or syllables.
Code:
==================
#include <stdio.h>
#define STRING 30
#define LENGTH 25
#define WORD 15
#define SYLLABLE 6
int
main (void)
{
int total_sents, total_sylls, total_words, shift_ctr;
double index;
char sentence [STRING][LENGTH];
char word [WORD];
char syllable [SYLLABLE];
FILE *inp;
inp = fopen("a:\\comics.txt", "r");
/* if the file will not open */
if(inp == NULL){
printf ("unable to open text readability file!!!\n");
return(1);
}
printf("---------------\n" );
/*This is the loop to count the number of words in the document */
total_words = 0;
while (fscanf (inp, "%s",word) !=EOF);
total_words += 1;
/* I am unable to count the number of sentences ending in '.', '!', '?',
because I do not know how
* to set up a loop to read through the text file. This is also the reason
I cannot put in my
* settings to count the number of syllables. I understand that counting
the words invovles using the
* %s (string) character, but I need help on reading through the document
and checking each
*individual character for one of the endings above to see if it truly is
the end of a sentence */
index = (206.835 - 84.6 * (total_sylls/total_words) - 1.015
* (total_words/total_sents)) ;
printf("---------------\n" );
if((index >= 91) && (index <= 100)){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: 5th Grader.\n");
}else if(index >= 81){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: 6th Grader.\n");
}else if(index >= 71){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: 7th Grader.\n");
}else if(index >= 66){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: 8th Grader.\n");
}else if(index >= 61){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: 9th Grader.\n");
}else if(index >= 51){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: High-school Student.\n");
}else if(index >= 30){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: College Student.\n");
}else if(index >= 0){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: College Graduate.\n");
}else{
printf ("Index is %3f\n", index);
printf ("Text is at the following level: Law School Graduate.\n");
}
}}}
fclose(inp);
printf("---------------\n" );
return (0);
}
===================
Sample Text File:
====================
Sherman's Lagoon: How about all these sheep on the island, Huh?
It's what shark dreams are made of. Yep. Have you guys had your
fill of sheep yet?
We're getting there.
Why did you start a second sheep?
I didn't see the other one.
Peanuts:
I read where someone said, if a man has the love of a dog, he is already a
millionaire.
And that'll buy a lot of cookies.
========================== ========
My problem is that I do not know how to read through the text to find sentence enders or syllables.
Code:
==================
#include <stdio.h>
#define STRING 30
#define LENGTH 25
#define WORD 15
#define SYLLABLE 6
int
main (void)
{
int total_sents, total_sylls, total_words, shift_ctr;
double index;
char sentence [STRING][LENGTH];
char word [WORD];
char syllable [SYLLABLE];
FILE *inp;
inp = fopen("a:\\comics.txt", "r");
/* if the file will not open */
if(inp == NULL){
printf ("unable to open text readability file!!!\n");
return(1);
}
printf("---------------\n"
/*This is the loop to count the number of words in the document */
total_words = 0;
while (fscanf (inp, "%s",word) !=EOF);
total_words += 1;
/* I am unable to count the number of sentences ending in '.', '!', '?',
because I do not know how
* to set up a loop to read through the text file. This is also the reason
I cannot put in my
* settings to count the number of syllables. I understand that counting
the words invovles using the
* %s (string) character, but I need help on reading through the document
and checking each
*individual character for one of the endings above to see if it truly is
the end of a sentence */
index = (206.835 - 84.6 * (total_sylls/total_words) - 1.015
* (total_words/total_sents))
printf("---------------\n"
if((index >= 91) && (index <= 100)){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: 5th Grader.\n");
}else if(index >= 81){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: 6th Grader.\n");
}else if(index >= 71){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: 7th Grader.\n");
}else if(index >= 66){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: 8th Grader.\n");
}else if(index >= 61){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: 9th Grader.\n");
}else if(index >= 51){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: High-school Student.\n");
}else if(index >= 30){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: College Student.\n");
}else if(index >= 0){
printf ("Index is %3f\n", index);
printf ("Text is at the following level: College Graduate.\n");
}else{
printf ("Index is %3f\n", index);
printf ("Text is at the following level: Law School Graduate.\n");
}
}}}
fclose(inp);
printf("---------------\n"
return (0);
}
===================
Sample Text File:
====================
Sherman's Lagoon: How about all these sheep on the island, Huh?
It's what shark dreams are made of. Yep. Have you guys had your
fill of sheep yet?
We're getting there.
Why did you start a second sheep?
I didn't see the other one.
Peanuts:
I read where someone said, if a man has the love of a dog, he is already a
millionaire.
And that'll buy a lot of cookies.
==========================
ASKER
Could I get some sample code on how to read each line into a buffer and go through each character ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It's pretty easy, really. This looks like homework so I'll tell you what you need to do.
For the first cut, follow these simple rules. :)
1) read each line into a buffer.
2) scan each line, examining every character.
a) if the character is alphabetic, increment the wordcount and skip to the first non-alaphabetic.
b) if the character is sentence ending punctuation (?!.) increment the sentence count and skip to the next alphabetic character.
3) if end of line is reached, repeat from step 1.
Syllables is a whole other issue. Tackle these easy ones first.
Kent