0
$\begingroup$

I would like to automate the following operation. To be transparent I shall put my code into separate chunks and explain the process. Suppose I have the following:

ELPr = Map[DeleteDuplicates, 
   WordList[Language -> "English"] // Characters // ToLowerCase];

which basically makes English words into characters without duplication of letters in each words. I am going to make a polynomial also which measures the length of words by numbers that is,

ELPrPo = Total[x^Map[Length, ELPr]];

The rest of my code would be as follow:

out = Import["path/out.csv"];
l = Replace[out, 
   s_String :> 
    If[StringMatchQ[s, "[" ~~ ___ ~~ "]"], 
     ToExpression["{" <> StringTake[s, {2, -2}] <> "}"], {s}], {2}];
t1 = l[[All, 1, 1]];
t2 = Table[x^-2 Total[x^l[[i, 2]]], {i, Length[l]}];
LPairPo = AssociationThread[t1, t2];
Map[Expand, ELPrPo + x LPairPo - x^2 LPairPo];
Map[Expand, 
   Table[%[[i]] - 1/(26 + 1) x D[%[[i]], x], {i, Length[l]}]] // N;
LPairQo = AssociationThread[t1, %];
ξ = LPairQo/ELPrPo;
q = TakeLargest[ξ /. x -> 2/(26 + 1) // N, 1];

In my directory I have number of out.csv files named as out, out1, out2 and so on for each I want to import them and find the q value. Once the q value is returned I do the following:

g1 = ELPr //. {OrderlessPatternSequence[
      Flatten[StringSplit[#, ":"] &@Keys[q]][[1]], 
      Flatten[StringSplit[#, ":"] &@Keys[q]][[2]], p___]} :> {Keys[q],
      p};
ELPrPo1 = Total[x^Map[Length, g1]];
Export["Desktop/LIMS/LIMS13/Language/ELPr1.csv",g1]

which essentially makes a new ELPr list now calling it g1 and its corresponding polynomial ELPrPo1 as explained in the beginning of this post. I now do the same calculations again:

out = Import["path/out1.csv"];
l = Replace[out, 
   s_String :> 
    If[StringMatchQ[s, "[" ~~ ___ ~~ "]"], 
     ToExpression["{" <> StringTake[s, {2, -2}] <> "}"], {s}], {2}];
t1 = l[[All, 1, 1]];
t2 = Table[x^-2 Total[x^l[[i, 2]]], {i, Length[l]}];
LPairPo = AssociationThread[t1, t2];
Map[Expand, ELPrPo1 + x LPairPo - x^2 LPairPo];
Map[Expand, 
   Table[%[[i]] - 1/(26 + 2) x D[%[[i]], x], {i, Length[l]}]] // N;
LPairQo = AssociationThread[t1, %];
ξ = LPairQo/ELPrPo;
q = TakeLargest[ξ /. x -> 2/(26 + 2) // N, 1]

But this one by q value is being calculated based on out1.csv, ELPrPo1, and the numerical value which was 2/(26+1) is now 2/(26+2). I wonder how can I make this iterations automated namely to go through out.csv files, find new value for q with appropriate numerics (namely changing 2/(26+1) to 2/(26+2) and then 2/(26+3) and so on on each iteration), make a new list g1,g2,g3 and their corresponding polynomials?

$\endgroup$

1 Answer 1

1
$\begingroup$

Try these changes:

automateStuff[j_] := Block[{out, l, t1, t2, LPairPo, m1, m2, xi, q, ELPrPo1},
out = Import["path/out"<>ToString[j]<>".csv"];
l = Replace[out, 
   s_String :> 
    If[StringMatchQ[s, "[" ~~ ___ ~~ "]"], 
     ToExpression["{" <> StringTake[s, {2, -2}] <> "}"], {s}], {2}];
t1 = l[[All, 1, 1]];
t2 = Table[x^-2 Total[x^l[[i, 2]]], {i, Length[l]}];
LPairPo = AssociationThread[t1, t2];
m1 = Map[Expand, ELPrPo1 + x LPairPo - x^2 LPairPo];
m2 = Map[Expand, 
   Table[m1[[i]] - 1/(26 + 1 + j) x D[m1[[i]], x], {i, Length[l]}]] // N;
LPairQo = AssociationThread[t1, m2];
xi = LPairQo/ELPrPo;
q = TakeLargest[xi /. x -> 2/(26 + 1 + j) // N, 1];
g[j] = ELPr //. {OrderlessPatternSequence[
      Flatten[StringSplit[#, ":"] &@Keys[q]][[1]], 
      Flatten[StringSplit[#, ":"] &@Keys[q]][[2]], p___]} :> {Keys[q],
      p};
ELPrPo1 = Total[x^Map[Length, g1]];
Export["Desktop/LIMS/LIMS13/Language/ELPr"<>ToString[j]<>"1.csv",g[j]]
]

In general, indexed things like your g1, g2 etc. are best represented by g[1], g[2] etc. in Mathematica.

$\endgroup$
4
  • $\begingroup$ Thanks, I've imposed the changes yet this generates wrong results in many levels. I've tried to fix the indexing but does not work properly still. $\endgroup$
    – Wiliam
    Aug 9, 2019 at 13:44
  • $\begingroup$ Why would you not localize your variables if you're gonna put them in a Block...? What you did here is no different than taking all the code and putting it in parentheses. $\endgroup$
    – b3m2a1
    Sep 7, 2019 at 21:57
  • $\begingroup$ @b3m2a1 Of course I should have localized the variables, but that does not address OP's problem. The important part is the use of g[1] instead of g1 and how to use j programmatically in a few places. $\endgroup$ Sep 7, 2019 at 22:12
  • 3
    $\begingroup$ @MariusLadegårdMeyer better to put completely good code on the site rather than mostly bad code. 80% of what's here is bad already, let's not increase the amount. $\endgroup$
    – b3m2a1
    Sep 7, 2019 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.