Advertisement
Guest User

Untitled

a guest
Aug 20th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. FRE = 206.835 - 1.015 * (words per sentence) - 84.6 * (syllables per word)
  2.  
  3. I would not, could not, in the rain.
  4. Not in the dark, not on a train.
  5. Not in a car, not in a tree.
  6. I do not like them, Sam, you see.
  7. Not in a house, not in a box.
  8. Not with a mouse, not with a fox.
  9. I will not eat them here or there.
  10. I do not like them anywhere!
  11.  
  12. It was a bright cold day in April, and the clocks were striking thirteen.
  13. Winston Smith, his chin nuzzled into his breast in an effort to escape
  14. the vile wind, slipped quickly through the glass doors of Victory Mansions,
  15. though not quickly enough to prevent a swirl of gritty dust from entering
  16. along with him.
  17.  
  18. When in the Course of human events, it becomes necessary for one people to
  19. dissolve the political bands which have connected them with another, and to
  20. assume among the powers of the earth, the separate and equal station to
  21. which the Laws of Nature and of Nature's God entitle them, a decent respect
  22. to the opinions of mankind requires that they should declare the causes
  23. which impel them to the separation.
  24.  
  25. #!perl -pa0
  26. s@w+|([.!?])@$s+=$#-,lc($&)=~s![aeiou]+B|([aeiouy]$)!$y+=1-$#-/3!ger@ge}
  27. {$_=206.835-1.015*@F/$s-84.6*$y/@F
  28.  
  29. $ perl flesch-kincaid.pl < input1.dat
  30. 110.730040322581
  31.  
  32. $ perl flesch-kincaid.pl < input2.dat
  33. 65.6097727272728
  34.  
  35. $ perl flesch-kincaid.pl < input2.dat
  36. 1.71366197183096
  37.  
  38. d,a,v,s,t,w;float R(char*c){for(;*c;++c){s+=*c=='.';if(isalpha(*c)){
  39. w+=!a++;d=(*c&30)>>1;if(*c&1&(d==7|((!(d&1))&(d<6|d>8)))){t+=!v++;}
  40. else v=0;}else v=a=0;}return 206.835-1.*w/s-82.*t/w;}
  41.  
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. d,a,/*last character was alphabetic */
  45. v,/*lastcharacter was a vowel */
  46. s, /* sentences counted by periods */
  47. t, /* syllables counted by non-consequtive vowels */
  48. w; /* words counted by non-letters after letters */
  49. float R/*eadability*/(char*c){
  50. for(;*c;++c){
  51. s+=*c=='.';
  52. if(isalpha(*c)){ /* a letter might mark the start of a word or a
  53. vowel string */
  54. w+=!a++; /* It is only the start of a word if the last character
  55. wasn't a letter */
  56. /* Extract the four bits of the character that matter in determining
  57. * vowelness because a vowel might mark a syllable */
  58. d=(*c&30)>>1;
  59. if( *c&1 & ( d==7 | ( (!(d&1)) & (d<6|d>8) ) )
  60. ) { /* These bits 7 or even and not 6, 8 make for a
  61. vowel */
  62. printf("Vowel: '%c' (mangled as %d [0x%x]) counts:%dn",*c,d,d,!v);
  63. t+=!v++;
  64. } else v=0; /* Not a vowel so set the vowel flag to zero */
  65. }else v=a=0; /* this input not alphabetic, so set both the
  66. alphabet and vowel flags to zero... */
  67. }
  68. printf("Syllables: %3in",t);
  69. printf("Words: %3i (t/w) = %fn",w,(1.0*t/w));
  70. printf("Sentences: %3i (w/s) = %fn",s,(1.0*w/s));
  71. /* Constants tweaked here due to bad counting behavior ...
  72. * were: 1.015 84.6 */
  73. return 206.835-1. *w/s-82. *t/w;
  74. }
  75. main(c){
  76. int i=0,n=100;
  77. char*buf=malloc(n);
  78. /* Suck in the whole input at once, using a dynamic array for staorage */
  79. while((c=getc(stdin))!=-1){
  80. if(i==n-1){ /* Leave room for the termination */
  81. n*=1.4;
  82. buf=realloc(buf,n);
  83. printf("Reallocated to %dn",n);
  84. }
  85. buf[i++]=c;
  86. printf("%c %cn",c,buf[i-1]);
  87. }
  88. /* Be sure the string is terminated */
  89. buf[i]=0;
  90. printf("'%s'n",buf);
  91. printf("%fn",R/*eadability*/(buf));
  92. }
  93.  
  94. $ gcc readability_golf.c
  95. readability_golf.c:1: warning: data definition has no type or storage class
  96. $ ./a.out < readability1.txt
  97. 'I would not, could not, in the rain.
  98. Not in the dark, not on a train.
  99. Not in a car, not in a tree.
  100. I do not like them, Sam, you see.
  101. Not in a house, not in a box.
  102. Not with a mouse, not with a fox.
  103. I will not eat them here or there.
  104. I do not like them anywhere!
  105. '
  106. 104.074631
  107. $ ./a.out < readability2.txt
  108. 'It was a bright cold day in April, and the clocks were striking thirteen.
  109. Winston Smith, his chin nuzzled into his breast in an effort to escape
  110. the vile wind, slipped quickly through the glass doors of Victory Mansions,
  111. though not quickly enough to prevent a swirl of gritty dust from entering
  112. along with him.
  113. '
  114. 63.044090
  115. $ ./a.out < readability3.txt
  116. 'When in the Course of human events, it becomes necessary for one people to
  117. dissolve the political bands which have connected them with another, and to
  118. assume among the powers of the earth, the separate and equal station to
  119. which the Laws of Nature and of Nature's God entitle them, a decent respect
  120. to the opinions of mankind requires that they should declare the causes
  121. which impel them to the separation.
  122. '
  123. -1.831667
  124.  
  125. import re
  126. 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
  127.  
  128. w=len(r(r'[ n]',i))
  129.  
  130. s=r('\.',i);s=len(s)-s.count('n')
  131.  
  132. y=r('[^aeiou](?i)+',i);y=len(y)-y.count(' ')-2;
  133.  
  134. import re
  135. def t(p):
  136. q=lambda e: e!=''
  137. w=filter(q,re.split('[ ,nt]',p))
  138. s=filter(q,re.split('[.?!]',p))
  139. c=len(w)*1.0
  140. f=c/len(s)
  141. return w,f,c
  142. def s(w):
  143. c= len(re.findall(r'([aeiouyAEIOUY]+)',w))
  144. v='aeiouAEIOU'
  145. if len(w)>2 and w[-1]=='e'and w[-2]not in v and w[-3]in v:c-= 1
  146. return c
  147. def f(p):
  148. w,f,c=t(p)
  149. i=0
  150. for o in w:
  151. i+=s(o)
  152. x=i/c
  153. return 206.835-1.015*f-84.6*x
  154.  
  155. def test():
  156. test_cases=[['I would not, could not, in the rain.
  157. Not in the dark, not on a train.
  158. Not in a car, not in a tree.
  159. I do not like them, Sam, you see.
  160. Not in a house, not in a box.
  161. Not with a mouse, not with a fox.
  162. I will not eat them here or there.
  163. I do not like them anywhere!', 111.38, 103.38, 119.38],
  164. ['It was a bright cold day in April, and the clocks were striking thirteen.
  165. Winston Smith, his chin nuzzled into his breast in an effort to escape
  166. the vile wind, slipped quickly through the glass doors of Victory Mansions,
  167. though not quickly enough to prevent a swirl of gritty dust from entering
  168. along with him.', 65.09, 57.09, 73.09],
  169. ["When in the Course of human events, it becomes necessary for one people to
  170. dissolve the political bands which have connected them with another, and to
  171. assume among the powers of the earth, the separate and equal station to
  172. which the Laws of Nature and of Nature's God entitle them, a decent respect
  173. to the opinions of mankind requires that they should declare the causes
  174. which impel them to the separation.", 3.70, -4.70, 11.70]]
  175. for case in test_cases:
  176. fre= f(case[0])
  177. print fre, case[1], (fre>=case[2] and fre<=case[3])
  178.  
  179. if __name__=='__main__':
  180. test()
  181.  
  182. elssar@elssar-laptop:~/code$ python ./golf/readibility.py
  183. 108.910685484 111.38 True
  184. 63.5588636364 65.09 True
  185. -1.06661971831 3.7 True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement