K&R c - 188 196 199 229ตัวอักษร
ด้วยการเปลี่ยนสเป็คเพื่อระบุฟังก์ชั่นฉันจะได้รับจำนวนมากของค่าใช้จ่ายคออกจากการนับ การเปลี่ยนไปใช้การนับจำนวนพยางค์ของ Strigoides ซึ่งดีกว่าการปรับสูตรของฉัน
หลังจากที่ฉันพบวิธีที่สั้นกว่าในการตรวจจับเสียงสระที่อิงกับความเศร้าstdchr
ฉันก็มีแรงจูงใจที่จะบีบเอาสิ่งที่น่าสะอิดสะเอียนนิด ๆ หน่อย ๆ ออกมาอีกสองสามข้อที่ฉันใช้อยู่ดังนั้นฉันจึงไม่ต้องเบื่อ
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;}
ตรรกะที่นี่เป็นเครื่องรัฐอย่างง่าย มันนับประโยคตามจุดเท่านั้นคำโดยสตริงของตัวอักษรและพยางค์เป็นสตริงของสระ (รวมถึง y)
ฉันต้องทำให้ค่าคงที่หายไปเล็กน้อยเพื่อให้มันออกมาพร้อมกับตัวเลขที่ถูกต้อง แต่ฉันได้ยืมเคล็ดลับของสไตรโกเดี้ยนว่าควรจะลดจำนวนพยางค์ลงไปเพียงเล็กน้อย
ยกเลิกการตีกอล์ฟพร้อมความคิดเห็นและเครื่องมือแก้ไขข้อบกพร่องบางอย่าง:
#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:%d\n",*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: %3i\n",t);
printf("Words: %3i (t/w) = %f\n",w,(1.0*t/w));
printf("Sentences: %3i (w/s) = %f\n",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 %d\n",n);
}
buf[i++]=c;
printf("%c %c\n",c,buf[i-1]);
}
/* Be sure the string is terminated */
buf[i]=0;
printf("'%s'\n",buf);
printf("%f\n",R/*eadability*/(buf));
}
เอาท์พุท: (ใช้นั่งร้านจากรุ่นยาว แต่ใช้ฟังก์ชัน golfed)
$ 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
ข้อบกพร่อง:
- ตรรกะประโยคนับเป็นสิ่งที่ผิด แต่ฉันได้รับไปกับมันเพราะเพียงหนึ่งในปัจจัยการผลิตที่มีหรือ
!
?
- ตรรกะการนับคำจะถือว่าการหดตัวเป็นสองคำ
- ตรรกะการนับพยางค์จะถือว่าการหดตัวเช่นเดียวกับพยางค์เดียว แต่อาจเกินจำนวนเฉลี่ย (ตัวอย่างเช่น
there
นับเป็นสองคำและหลายคำที่ลงท้ายด้วยe
จะนับหนึ่งมากเกินไป) ดังนั้นฉันจึงใช้ปัจจัยคงที่ของการแก้ไข 96.9%
- ถือว่าชุดอักขระ ASCII
- ฉันเชื่อว่าการตรวจจับเสียงสระจะยอมรับ
[
และ{
เห็นได้ชัดว่าไม่ถูกต้อง
- การพึ่งพาความหมายของ K&R มากมายทำให้เรื่องนี้น่าเกลียด แต่เฮ้มันเป็นรหัสกอล์ฟ
สิ่งที่ต้องดู:
ฉันอยู่ข้างหน้าของสารละลายไพ ธ อนทั้งคู่ที่นี่ถึงแม้ว่าฉันจะติดตาม perl อยู่ก็ตาม
รับสิ่งที่น่าสยดสยองที่ฉันทำเพื่อตรวจหาสระ มันสมเหตุสมผลถ้าคุณเขียนการแทน ASCII ออกมาเป็นไบนารี่และอ่านความคิดเห็นในเวอร์ชั่นยาว