#Please do not post homework #Lucy Martinez, 04-27-2024, Assignment 26 read `ENGLISH.txt`: with(combinat): # Problem 2: Using the file ENGLISH.txt, where ENG() is a list of length 26 # whose i-th entry is the list of words of length i, find: # Out of the 26^2=676 pairs of letters how many actualy show up as consecutive # two letters? (For example for the word [a,l,e,x] # the pairs [a,l],[l,e],[e,x] showed up # ANSWER: 589 --- only counts words of length 3 or more (See Count1() below) # Out of the 26^3=175676 triples of letters how many actually show up as # consecutive three letters? (For example for the word [v,a,l,e,n,t,i,n,o] # the triples [v,a,l],[a,l,e],[l,e,n],[e,n,t],[n,t,i],[t,i,n],[i,n,o] showed up # ANSWER: 581 --- only counts words of length 4 or more (See Count2() below) #chooseRepeat(L,m,k): Inputs a list L, and duplicates it k times, outputs # a list of lists [xi,yi] where xi, yi is an element of L chooseRepeat:= proc(L, m, k) local i,duplicated: duplicated := [seq(op(L), i=1 .. k)]: return choose(duplicated, m): end proc: Count1:=proc() local allwords,ALPH,allpairs,i,current,w,S,j,curword: allwords:=ENG(): #ALPH:=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]: #allpairs:=chooseRepeat(ALPH, 2, 2): S:={}: for i from 3 to 26 do current:=allwords[i]: #this is the list of all words of length i for w in current do #looks at each word in all words of length i for j from 1 to nops(w)-1 do curword:=[w[j],w[j+1]]: S:=S union {curword}: od: od: od: nops(S): end: Count2:=proc() local allwords,ALPH,alltriples,i,current,w,S,j,curword: allwords:=ENG(): #ALPH:=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]: #alltriples:=chooseRepeat(ALPH, 3, 3): S:={}: for i from 4 to 26 do current:=allwords[i]: #this is the list of all words of length i for w in current do #looks at each word in all words of length i for j from 1 to nops(w)-2 do curword:=[w[j],w[j+1],w[j+1]]: S:=S union {curword}: od: od: od: nops(S): end: ###################################C26.txt #C26.txt, April 22, 2024 Help26:=proc(): print(` FT(SetL), TM(SetL) `): end: #FT(SetL): The frequency table of all words whose length belobgs to SetL #For example FT({3,4}) returns the randked frequency table for words of #length 3 and 4 FT:=proc(SetL) local ALPH,T1,W,DB,C,B,FL,L,A,FL1,S1: ALPH:=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]: DB:=ENG(): C:=0: for L in ALPH do T1[L]:=0: od: for A in SetL do for W in DB[A] do for B from 1 to nops(W) do T1[W[B]]:=T1[W[B]]+1: C:=C+1: od: od: od: FL:=evalf([seq(T1[ALPH[A]]/C,A=1..nops(ALPH))]): FL1:=sort(FL,`>`): for A from 1 to nops(ALPH) do S1[FL[A]]:=ALPH[A]: od: FL1,[seq(S1[FL1[A] ],A=1..nops(ALPH))], [seq([FL1[A],S1[FL1[A]] ],A=1..nops(ALPH))] : end: #TM(SetL): The frequency table of all words whose length belobgs to SetL #For example FT({3,4}) returns the randked frequency table for words of #length 3 and 4 #AND the Transiton matrix 28x28 the first "letter" is ST and the last is EN #NOT YET FINISHED TM:=proc(SetL) local ALPH,T1,W,DB,C,B,FL,L,A,FL1,S1,T2,L1,L2,ST,EN,T3,ALPH1: print(`Not yet finished`): ALPH:=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]: DB:=ENG(): C:=0: for L in ALPH do T1[L]:=0: od: for L1 from 1 to nops(ALPH) do T2[ST,ALPH[L1]]:=0: T2[ALPH[L1],EN]:=0: for L2 from 1 to nops(ALPH) do T2[ALPH[L1],ALPH[L2]]:=0: od: od: for A in SetL do for W in DB[A] do for B from 1 to nops(W) do T1[W[B]]:=T1[W[B]]+1: C:=C+1: if B`): for A from 1 to nops(ALPH) do S1[FL[A]]:=ALPH[A]: od: for L1 from 1 to nops(ALPH) do T3[ST,ALPH[L1]]:=evalf(T2[ST,ALPH[L1]]/C): T3[ALPH[L1],EN]:=evalf(T2[ALPH[L1],EN]/C): od: for L1 from 1 to nops(ALPH) do for L2 from 1 to nops(ALPH) do T3[ALPH[L1],ALPH[L2]]:=evalf(T2[ALPH[L1],ALPH[L2]]/C): od: od: FL1,[seq(S1[FL1[A] ],A=1..nops(ALPH))], [seq([FL1[A],S1[FL1[A]] ],A=1..nops(ALPH))] , op(T3): end: