0

I was given this formula called FRES (Flesch reading-ease test) that is used to measure the readability of a document:

enter image description here

My task is to write a python function that returns the FRES of a text. Hence I need to convert this formula into a python function.

I have re-implemented my code from a answer I got to show what I have so far and the result it has given me:

import nltk
import collections
nltk.download('punkt')
nltk.download('gutenberg')
nltk.download('brown')
nltk.download('averaged_perceptron_tagger')
nltk.download('universal_tagset')

import re
from itertools import chain
from nltk.corpus import gutenberg
VC = re.compile('[aeiou]+[^aeiou]+', re.I)
def count_syllables(word):
    return len(VC.findall(word))

def compute_fres(text):
    """Return the FRES of a text.
    >>> emma = nltk.corpus.gutenberg.raw('austen-emma.txt')
    >>> compute_fres(emma) # doctest: +ELLIPSIS
    99.40...
    """

for filename in gutenberg.fileids():
    sents = gutenberg.sents(filename)
    words = gutenberg.words(filename)
    num_sents = len(sents)
    num_words = len(words)
    num_syllables = sum(count_syllables(w) for w in words)
    score = 206.835 - 1.015 * (num_words / num_sents) - 84.6 * (num_syllables / num_words)
return(score)

After running the code this is the result message I got:

Failure

Expected :99.40...

Actual   :92.84866041488623

File "C:/Users/PycharmProjects/a1/a1.py", line 60, in a1.compute_fres
Failed example:
    compute_fres(emma) # doctest: +ELLIPSIS

Expected:
    99.40...
Got:
    92.84866041488623

My function is supposed to pass the doctest and result in 99.40... And I'm also not allowed to edit the syllables function since it came with the task:

import re
VC = re.compile('[aeiou]+[^aeiou]+', re.I)
def count_syllables(word):
    return len(VC.findall(word))

This question has being very tricky but at least now it's giving me a result instead of an error message, not sure why it's giving me a different result though.

Any help will be very appreciated. Thank you.

Ovaflow
  • 111
  • 1
  • 14
  • See my answer to your other question at [https://stackoverflow.com/questions/49270817/flesch-kincaid-readability-test-in-python/52412814#52412814](https://stackoverflow.com/questions/49270817/flesch-kincaid-readability-test-in-python/52412814#52412814). – algoguru Sep 19 '18 at 19:23

1 Answers1

0

BTW, there's the textstat library.

from textstat.textstat import textstat
from nltk.corpus import gutenberg

for filename in gutenberg.fileids():
    print(filename, textstat.flesch_reading_ease(filename))

If you're bent on coding up your own, first you've to

  • decide if a punctuation is a word
  • define how to count no. of syllables in the word.

If punctuation is a word and syllables is counted by the regex in your question, then:

import re
from itertools import chain
from nltk.corpus import gutenberg

def num_syllables_per_word(word):
    return len(re.findall('[aeiou]+[^aeiou]+', word))

for filename in gutenberg.fileids():
    sents = gutenberg.sents(filename)
    words = gutenberg.words(filename) # i.e. list(chain(*sents))
    num_sents = len(sents)
    num_words = len(words)
    num_syllables = sum(num_syllables_per_word(w) for w in words)
    score = 206.835 - 1.015 * (num_words / num_sents) - 84.6 * (num_syllables / num_words)
    print(filename, score)
alvas
  • 115,346
  • 109
  • 446
  • 738
  • Well as I mentioned I'm not allowed to edit def count_syllables(word):, I implemented your coded from for filename in gutenberg.fileids(): to the end and got this result Expected: 99.40... Actual: 92.84866041488623 – Ovaflow Mar 14 '18 at 03:07
  • I also tried and the other option and imported textstat and implement the function and got -44 – Ovaflow Mar 14 '18 at 03:09
  • I'm not sure how `textstats` work so I'm not sure what that is. Most probably, the difference between the expected and actual comes from the 2 assumptions, (1) deciding what is a word (e.g. is punctuation a word? and (2) how you count syllables. BTW, where did you get the "expected" value? Is this a homework? – alvas Mar 14 '18 at 03:28
  • This is a homework task given to me by my teacher, I simply need to written a function that passes the doctest and not allowed to change anything else besides importing third party modules – Ovaflow Mar 14 '18 at 03:35
  • I'm sure you can figure out why the expected is different from the output from the code. Hint: Look closely at assumption (1) and (2). I believe in you! – alvas Mar 14 '18 at 04:17
  • 1
    Isn't assumption 2 being established already? through the def count_syllables(word)? Also the from itertools import chain is not being called at all on my end – Ovaflow Mar 14 '18 at 04:55
  • You're getting closer. Have fun! ;P – alvas Mar 14 '18 at 05:06
  • 1
    Well everytime I change something to fit the assumptions I get no result and errors, the code I have right now is the closest I get to passing the doctest – Ovaflow Mar 14 '18 at 05:43