#OK to post homework #Joseph Koutsoutis, 04-28-2024, Assignment 26 read `ENGLISH.txt`: #1 done #2 #GetLetterPairCounts(): outputs a table T such that for any two letters #L1 and L2, T[L1,L2] is the number of times L1 and L2 showed up as consecutive letters #in all of ENG() GetLetterPairCounts := proc() local DB,ALPH,L,WORD,J,K,T,L1,L2: 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(): for L1 in ALPH do: for L2 in ALPH do: T[L1,L2] := 0: od: od: for J from 1 to nops(DB) do: for WORD in DB[J] do: for K from 1 to J-1 do: T[WORD[K], WORD[K+1]] += 1: od: od: od: op(T): end: #GetCountPairsAboveC(C): outputs how many letters L1 and L2 showed up as consecutive #letters (L1 followed by L2) in all of ENG more than C times GetCountPairsAboveC := proc(C) local T,ALPH,L1,L2,K: 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]: T := GetLetterPairCounts(): K := 0: for L1 in ALPH do: for L2 in ALPH do: if T[L1,L2] > C then K++: fi: od: od: K: end: #GetCountPairsAbove(0) outputs 593 pairs #GetCountPairsAbove(10) outputs 497 pairs #GetLetterTripleCounts(): outputs a table T such that for any three letters #L1,L2, and L3, T[L1,L2,L3] is the number of times L1, L2, and L3 showed up #as consecutive letters in all of ENG() GetLetterTripleCounts := proc() local DB,ALPH,L,WORD,J,K,T,L1,L2,L3: 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(): for L1 in ALPH do: for L2 in ALPH do: for L3 in ALPH do: T[L1,L2,L3] := 0: od: od: od: for J from 1 to nops(DB) do: for WORD in DB[J] do: for K from 1 to J-2 do: T[WORD[K], WORD[K+1], WORD[K+2]] += 1: od: od: od: op(T): end: #GetCountTripleAboveC(C): outputs how many letters L1, L2, and L3 showed up as consecutive #letters (L1 followed by L2 followed by L3) in all of ENG more than C times GetCountTripleAboveC := proc(C) local T,ALPH,L1,L2,L3,K: 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]: T := GetLetterTripleCounts(): K := 0: for L1 in ALPH do: for L2 in ALPH do: for L3 in ALPH do: if T[L1,L2,L3] > C then K++: fi: od: od: od: K: end: #GetCountTripleAboveC(0) outputted 6639 #3 #TM(SetL): Outputs the 28 by 28 transition matrix of all words whose length #belongs to SetL. TM:=proc(SetL) local ALPH1,LEN,WORD,J,C,T,DB,L1,L2: ALPH1 := [ST,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,EN]: DB:=ENG(): for L1 in ALPH1 do: for L2 in ALPH1 do: T[L1,L2]:=0: od: od: for LEN in SetL do: for WORD in DB[LEN] do: for J from 1 to LEN do: if J = 1 then: T[ST,WORD[J]]++: fi: if J < LEN then: T[WORD[J], WORD[J+1]]++: else: T[WORD[J], EN]++: fi: od: od: od: for L1 in ALPH1 do: C := add(T[L1, L2], L2 in ALPH1): if C <> 0 then: for L2 in ALPH1 do: T[L1,L2] := evalf(T[L1,L2] / C): od: fi: od: [seq([seq(T[L1,L2], L2 in ALPH1)], L1 in ALPH1)]: end: