Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FRE = 206.835 - 1.015 * (words per sentence) - 84.6 * (syllables per word)
- I would not, could not, in the rain.
- Not in the dark, not on a train.
- Not in a car, not in a tree.
- I do not like them, Sam, you see.
- Not in a house, not in a box.
- Not with a mouse, not with a fox.
- I will not eat them here or there.
- I do not like them anywhere!
- It was a bright cold day in April, and the clocks were striking thirteen.
- Winston Smith, his chin nuzzled into his breast in an effort to escape
- the vile wind, slipped quickly through the glass doors of Victory Mansions,
- though not quickly enough to prevent a swirl of gritty dust from entering
- along with him.
- When in the Course of human events, it becomes necessary for one people to
- dissolve the political bands which have connected them with another, and to
- assume among the powers of the earth, the separate and equal station to
- which the Laws of Nature and of Nature's God entitle them, a decent respect
- to the opinions of mankind requires that they should declare the causes
- which impel them to the separation.
- #!perl -pa0
- s@w+|([.!?])@$s+=$#-,lc($&)=~s![aeiou]+B|([aeiouy]$)!$y+=1-$#-/3!ger@ge}
- {$_=206.835-1.015*@F/$s-84.6*$y/@F
- $ perl flesch-kincaid.pl < input1.dat
- 110.730040322581
- $ perl flesch-kincaid.pl < input2.dat
- 65.6097727272728
- $ perl flesch-kincaid.pl < input2.dat
- 1.71366197183096
- d,a,v,s,t,w;float R(char*c){for(;*c;++c){s+=*c=='.';if(isalpha(*c)){
- w+=!a++;d=(*c&30)>>1;if(*c&1&(d==7|((!(d&1))&(d<6|d>8)))){t+=!v++;}
- else v=0;}else v=a=0;}return 206.835-1.*w/s-82.*t/w;}
- #include <stdlib.h>
- #include <stdio.h>
- d,a,/*last character was alphabetic */
- v,/*lastcharacter was a vowel */
- s, /* sentences counted by periods */
- t, /* syllables counted by non-consequtive vowels */
- w; /* words counted by non-letters after letters */
- float R/*eadability*/(char*c){
- for(;*c;++c){
- s+=*c=='.';
- if(isalpha(*c)){ /* a letter might mark the start of a word or a
- vowel string */
- w+=!a++; /* It is only the start of a word if the last character
- wasn't a letter */
- /* Extract the four bits of the character that matter in determining
- * vowelness because a vowel might mark a syllable */
- d=(*c&30)>>1;
- if( *c&1 & ( d==7 | ( (!(d&1)) & (d<6|d>8) ) )
- ) { /* These bits 7 or even and not 6, 8 make for a
- vowel */
- printf("Vowel: '%c' (mangled as %d [0x%x]) counts:%dn",*c,d,d,!v);
- t+=!v++;
- } else v=0; /* Not a vowel so set the vowel flag to zero */
- }else v=a=0; /* this input not alphabetic, so set both the
- alphabet and vowel flags to zero... */
- }
- printf("Syllables: %3in",t);
- printf("Words: %3i (t/w) = %fn",w,(1.0*t/w));
- printf("Sentences: %3i (w/s) = %fn",s,(1.0*w/s));
- /* Constants tweaked here due to bad counting behavior ...
- * were: 1.015 84.6 */
- return 206.835-1. *w/s-82. *t/w;
- }
- main(c){
- int i=0,n=100;
- char*buf=malloc(n);
- /* Suck in the whole input at once, using a dynamic array for staorage */
- while((c=getc(stdin))!=-1){
- if(i==n-1){ /* Leave room for the termination */
- n*=1.4;
- buf=realloc(buf,n);
- printf("Reallocated to %dn",n);
- }
- buf[i++]=c;
- printf("%c %cn",c,buf[i-1]);
- }
- /* Be sure the string is terminated */
- buf[i]=0;
- printf("'%s'n",buf);
- printf("%fn",R/*eadability*/(buf));
- }
- $ gcc readability_golf.c
- readability_golf.c:1: warning: data definition has no type or storage class
- $ ./a.out < readability1.txt
- 'I would not, could not, in the rain.
- Not in the dark, not on a train.
- Not in a car, not in a tree.
- I do not like them, Sam, you see.
- Not in a house, not in a box.
- Not with a mouse, not with a fox.
- I will not eat them here or there.
- I do not like them anywhere!
- '
- 104.074631
- $ ./a.out < readability2.txt
- 'It was a bright cold day in April, and the clocks were striking thirteen.
- Winston Smith, his chin nuzzled into his breast in an effort to escape
- the vile wind, slipped quickly through the glass doors of Victory Mansions,
- though not quickly enough to prevent a swirl of gritty dust from entering
- along with him.
- '
- 63.044090
- $ ./a.out < readability3.txt
- 'When in the Course of human events, it becomes necessary for one people to
- dissolve the political bands which have connected them with another, and to
- assume among the powers of the earth, the separate and equal station to
- which the Laws of Nature and of Nature's God entitle them, a decent respect
- to the opinions of mankind requires that they should declare the causes
- which impel them to the separation.
- '
- -1.831667
- import re
- def R(i):r=re.split;w=len(r(r'[ n]',i));s=r('\.',i);y=r('[^aeiou](?i)+',i);return 206.835-1.015*w/(len(s)-s.count('n'))-84.6*(len(y)-y.count(' ')-2)*.98/w
- w=len(r(r'[ n]',i))
- s=r('\.',i);s=len(s)-s.count('n')
- y=r('[^aeiou](?i)+',i);y=len(y)-y.count(' ')-2;
- import re
- def t(p):
- q=lambda e: e!=''
- w=filter(q,re.split('[ ,nt]',p))
- s=filter(q,re.split('[.?!]',p))
- c=len(w)*1.0
- f=c/len(s)
- return w,f,c
- def s(w):
- c= len(re.findall(r'([aeiouyAEIOUY]+)',w))
- v='aeiouAEIOU'
- if len(w)>2 and w[-1]=='e'and w[-2]not in v and w[-3]in v:c-= 1
- return c
- def f(p):
- w,f,c=t(p)
- i=0
- for o in w:
- i+=s(o)
- x=i/c
- return 206.835-1.015*f-84.6*x
- def test():
- test_cases=[['I would not, could not, in the rain.
- Not in the dark, not on a train.
- Not in a car, not in a tree.
- I do not like them, Sam, you see.
- Not in a house, not in a box.
- Not with a mouse, not with a fox.
- I will not eat them here or there.
- I do not like them anywhere!', 111.38, 103.38, 119.38],
- ['It was a bright cold day in April, and the clocks were striking thirteen.
- Winston Smith, his chin nuzzled into his breast in an effort to escape
- the vile wind, slipped quickly through the glass doors of Victory Mansions,
- though not quickly enough to prevent a swirl of gritty dust from entering
- along with him.', 65.09, 57.09, 73.09],
- ["When in the Course of human events, it becomes necessary for one people to
- dissolve the political bands which have connected them with another, and to
- assume among the powers of the earth, the separate and equal station to
- which the Laws of Nature and of Nature's God entitle them, a decent respect
- to the opinions of mankind requires that they should declare the causes
- which impel them to the separation.", 3.70, -4.70, 11.70]]
- for case in test_cases:
- fre= f(case[0])
- print fre, case[1], (fre>=case[2] and fre<=case[3])
- if __name__=='__main__':
- test()
- elssar@elssar-laptop:~/code$ python ./golf/readibility.py
- 108.910685484 111.38 True
- 63.5588636364 65.09 True
- -1.06661971831 3.7 True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement