Created
December 11, 2013 06:26
-
-
Save michaelglass/7905878 to your computer and use it in GitHub Desktop.
A vs An ported to ruby from: http://home.nerbonne.org/A-vs-An/ ported by my coworker, @jleven
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
# by Eamon Nerbonne (from http://home.nerbonne.org/A-vs-An), Apache 2.0 license | |
class IndefiniteArticle | |
# Usage example: IndefiniteArticle.for("example ") | |
# example returns: "an" | |
# Note that the terminal space indicates this is a complete word - this is sometimes significant, particularly for acronyms! | |
DICT = "]08[09[0-11[0-4[0-4 ]0-6-[0-8[11[110]111]112]113]114]115]116]117]118]119]11.4]18[180]1800[1801[1802[1803[1804[1805[1806[1807[1808[1809[181-]181 ]182-]182 ]183-]183 ]184-]184 ]185-]185 ]186-]186 ]187-]187 ]188-]188 ]189-]189 ]8[800x]890]8,1]8,2]–i[#i[$11 [$11.[$18 [$18,[$18.[$8[&a[&o[*a[*e[*i[.av[.mp[.og[/a[/e[/h[/i[/l[/s/[@[`a[£8[∞[a[a ]abou]about-[agai]al-I]algu]alth]amon]an ]and]and\"[anda[ande[andr[anot]anyw]apart ]appears]apre]are ]are:]artí]A[A$]AAA]Akiz]Amar\"]Andaluc]Armat]Asturias]Athl]Athleti[Athlo[AU$]AUD]AUSC]Á[á[à[Ä[ā[Å[æ[Æ[Æn]Bhai[Bhá[Buddhism[contains[Chais[County,[das [dem [der [describes[Dinas[Diver-[Dún]e[e.g]each ]either ]either.]el-]ella]empez]enoug]eu]eup [ew]exists]E[Empez]Enam]Esp]Espad[Espe[Espo[Eu]Eul[EUR]é[ég]ét]éta[étu[É[f-[f [f\"[f/[fM[fp[ft[F0[F1[F2[F3[F4[F5[F6[F9[F'[F-[F [F\"[F#[F,[F.[F/[F”[FA[FAC]FAD]FAIR]FAL]FAM]FAN]FAP]FAQ]FAR]FAS]FAT]FB[FC[Fc[FD[FEC[FEI[FF[FF ]Ff[Fh[FH[FIA[FIAT]FID [FIR [FIS [FK[FLC[FLN[FLP[FM[FMR]FO [FOI [FP[FP.]FP?]FPC?]FRC[FRS[FS[FT[FTS]FTT]FU [FU,[FU.[FV[FW[FWD]FX[FY[Fσ[Ghae[Ghai[h'[h-[h-U]h [h\"[h,[hC[heir[heira]hims[historic\"[historic\" ]homa[homm[hon[honey]honk]honv]hors [hour[ht[htt]http [H1[H2[H3[H4[H5[H'[H-[H [H\"[H&[H,[H.[H.A]H+[Habilitations[HB[HC[HD[HDB]Heir[HF[HG[HH[HI[HID]HIG]HIM]HIP]HL[HLA-D]HM[HN[Hon[Hond]Hone]Hones[Hong]Honk]Honol]Hour[HO [HOV[HP[HQ[HR[HRT]HS[HS ]HSR]HST]HT[HTP]HV[HWT[i[i.e]ibn]if ]ii]includi]indicates]instea]instead?[is ]is.]it ]iu]I[I-A]I-I]III]Ilb]IMH]Imams]IR£]Islam ]Islam,]Islam.]Islands]İ[Jia[Jian]ku [l [l\"[lp[L1[L2[L3[L5[L'[L'A]L-[L-a]L [L\"[L&[L,[L.[L/[Lae[Laoig[LA [LAL[LAP[LB[LC[LD[LE[LEA]LEE]LEG]LEO]LEP]LET]LF[LG[LH[LIR[LL[LM[LMX]LN[Locha[LOE[LP[LR[LS[LT[LU [LV[LX[LZ[m-[m [m\"[m&[m×[makes [mb[mein[mentions[mf[mp[mR[mt[M1[M19]M190[M2[M3[M4[M5[M6[M7[M8[M9[M'[M-[M-t]M [M\"[M&[M,[M.[M.A.S]M/[MA[MAC]MAD]MAF]MAG]MAJ]MAL]MAM]MAN]MAP]MAR]MAS]MAT]MAX]MAY]MB[MC[MD[Me-[MEd[MEn[MEP[MF[Mf[MG[MH[Mh[Mie[MI5[MI6[MI [MIA[MIT[MK[ML[MM[MMT]MN[MoU[MO [MOT [MOU[MP[MR[MS[Msc[MT[MTR]MUV[MV[MX[N4[N6[N'[N-[N-a]N-S]N [N\"[N,[N.[N.Y]N=[N²[Nao[NA [NAA[NAAF]NAI[NASL[NB[NC[ND[NEA[NEH[NES [NF[NG[NH[NI[NIC]NIL]NIM]NIMH[NIN]NIS]NJC[NK[NL[NLS]NM[NNR[NNT[NP[NPO]NPOV-[NR[NRJ]NRT]NS[NSW]NT[NT$]NUS[NV[Nv[NWA[NX[NYP[NYU[n-[n−[n [n\"[n&[n,[n+[n×[nda[npa[nt[nV[nW[o[obr]occurs]ocho]of ]on]on-[on/[onb[onco[ond[oner[ong[oni[onl[onm[ono[onr[ons[ont[onu[onw[ony[or ]or,]oui]O[Obers]Oberst [Oberstl[Olv]One]Onei[ONE]Oop]Oui]Ó[Ö[ö[Ō[ō[Phob[Phoi[r'[r-[r [r\"[r&[r.[refers[rf[rm[rs[R1[R10]R2[R3[R4[R5[R6[R'[R-[R [R\"[R&[R,[R.[R.C]R/[RA [RAF[RB[RC[RD[RE [RER[RF[Rf[RG[RHS[RIA[RIC [RJ[RK[RL[RL ]RM[RM1]RN[RNG]ROT[RP[RQ[RR[RS[RS ]RS)]RS,]RS.]RS?]RST]RT[RU[RV[RX[s-[s\"[s)[s,[s.[says[sich[sp3[sprot[ssh[states [states:[sv[sva]sve]S1[S2[S3[S4[S5[S6[S'[S-[S [S\"[S&[S&W]S,[S.B[S.M[S.O[S”[SA-[SA-1]SA [SACD[SAE[SAS[SASE]SAT [SATB[SB[SCA [SCC[SCM[SCO [SCR[SCRA]SCT[SD[SE [SEC[SECO]SECR]SEI[SEO[SF[SG[SH2[SH3[SH-[SI [SJ[SK[SL[SLA]SLI]SLO]SM[SMA]SME]SME [SMI]SN[SNA]SNE]SNO]SO([SOA [SOAI[SOE[SOI[SOS[SOV[SP[SPAC]SPAD]SPAM]SPAN]SPAR]SPE]SPE [SPIC]SPO]SPU]SR[SS[ST-[STA [STB[STC[STD[STF[STL[STM[STS[STV[Sura [SU[SUB]SUL]SUN]SUP]SUS]SV[SWF[SWP[SWR[SX[SXS]t-S[than [tS[Taves[Tà[u[u-]u ]u\"]u.]ub]ube[uf]uk]uka[ulu]um ]un ]una ]unan]unana[unann[unans[unant[unary]une ]uni]unicorp[unid[unidi]unim[unimo]unin[unintentionall]unintentionally [univo[unles]until ]upo]ura]ure]uri]url]uro]us]us-[us [ush[ut]utm[utt[uv]uw]U1[U-21[U-23 [U-B[U-Bo]Ua[Ub[Ubi]UDP-[Ud[Ugl[Uh[Ui[Ul[Uli]Um[UMN[Un-[Una[Unan]Unb[Unc[Und[Une[Unes]Unf[Ung[Unh[Unid[Unin[Unk[Unl[Unm[Unn[Uno[Unp[Unr[Uns[Unt[Unters]Unu[Unw[Up[Ur[Ura]Uri]Uru]Uruguayan-[Uruk[Ush[Ust[Utn[Uto-[Utr[Utt[Ux[Uz[ü[Ü[verses[Valley,[VII[WikiEl[x[xa]xe]xi]xo]xx]xy]X[Xa]XA]Xe]Xh]Xi]Xiany[XIV]XIX]Xo]Xu]XU]XV]XX]XX [Xy]Yp[Zen)[α[ε[ω[西[長[" | |
@root = {} | |
DICT.gsub /([^\[\]]*)([\[\]])/ do |match| | |
m = /([^\[\]]*)([\[\]])/.match match | |
prefix = m[1] | |
aVsAn = m[2] # a is closing bracket, an is opening. | |
node = @root | |
prefix.each_char do |letter| | |
node[letter] ||= {} | |
node = node[letter] | |
end | |
node[:article] = aVsAn == "]" ? "a" : "an" | |
end | |
def self.for(word) | |
node = @root | |
article = node[:article] | |
sI = 0 | |
while true | |
if sI >= word.size | |
return article | |
elsif word[sI] === "(" || word[sI] === "'" || word[sI] === '"' | |
sI += 1 | |
else | |
break | |
end | |
end | |
while true | |
node = node[word[sI]]; | |
break unless node | |
article = node[:article] if node[:article] | |
sI += 1 | |
if sI >= word.length | |
node = node[" "] | |
article = node[:article] if node && node[:article] | |
break | |
end | |
end | |
article.dup | |
end | |
def self.correct_sentence(initial_sentence) | |
initial_sentence.gsub /\b(a|an|A|An)\b(\s)+\b([\w']+)\b/ do |s| | |
article, space, word = s.split /\b/ | |
new_article = self.for word | |
new_article.capitalize! if article[0] == 'A' | |
"#{new_article}#{space}#{word}" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe IndefiniteArticle do | |
shared_examples_for "#for" do |word, expected_article| | |
it "knows that it #{expected_article} #{word}" do | |
expect( IndefiniteArticle.for word ).to eq expected_article[:is] | |
end | |
end | |
alias_it_behaves_like_to "it_knows_the_correct_article" | |
it_knows_the_correct_article "#for", "unanticipated", is: "an" | |
it_knows_the_correct_article "#for", "unanimous", is: "a" | |
it_knows_the_correct_article "#for", "honest", is: "an" | |
it_knows_the_correct_article "#for", "honeysuckle", is: "a" | |
it_knows_the_correct_article "#for", "0800", is: "an" | |
it_knows_the_correct_article "#for", "NASA", is: "a" | |
it_knows_the_correct_article "#for", "NSA", is: "an" | |
it_knows_the_correct_article "#for", "FIAT", is: "a" | |
it_knows_the_correct_article "#for", "FAA", is: "an" | |
it_knows_the_correct_article "#for", "man", is: "a" | |
it_knows_the_correct_article "#for", "plan", is: "a" | |
it_knows_the_correct_article "#for", "canal", is: "a" | |
it_knows_the_correct_article "#for", "extraordinary", is: "an" | |
shared_examples_for "#correct_sentence" do |initial_sentence, expected_sentence| | |
it "converts \"#{initial_sentence}\" to \"#{expected_sentence}\"" do | |
expect( IndefiniteArticle.correct_sentence initial_sentence ).to eq expected_sentence | |
end | |
end | |
it_behaves_like "#correct_sentence", | |
"What is a elephant to do?", | |
"What is an elephant to do?" | |
it_behaves_like "#correct_sentence", | |
"An man, an plan, an canal, and a extraordinary Panama.", | |
"A man, a plan, a canal, and an extraordinary Panama." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment