Help:=proc():print(`Grep(T,p),RandTree(A,k)`): print(`TreeToKW(T)`): end: with(combinat): #Inputs a text T and a shorter word p #finds the set of all locations where you #can find p staring from that location #For example Grep([1,2,3,4,2,3],[2,3]); #returns {2,5}; Grep:=proc(T,p) local j,S,k: k:=nops(p): S:={}: for j from 1 to nops(T)-k+1 do if [op(j..j+k-1,T)]=p then S:=S union {j}: fi: od: S: end: #RandTree(A,k,s): Given an alphabet A and #an integer k, outputs a random tree of depth #k with each non-leaf with s children RandTree:=proc(A,k,s) local S,T,i,A1,name1,T1: if k=0 then RETURN([]): fi: T:=[]: A1:=A: for i from 1 to s do name1:=A1[rand(1..nops(A1))()]: A1:=A1 minus {name1}: T1:=RandTree(A,k-1,s): T:=[op(T),[name1,T1]]: od: T: end: #TreeToKW(T): Given a tree outputs the set of keywords #(all the paths from the root to a leaf) TreeToKW:=proc(T) local i, T1,name1,S1,S: if T=[] then RETURN({[]}): fi: S:={}: for i from 1 to nops(T) do T1:=T[i]: name1:=T1[1]: T1:=T1[2]: S1:=TreeToKW(T1): S:=S union {seq([name1,op(S1[j])],j=1..nops(S1))}: od: S: end: