(* Selecting ID for each name *) maintainSelectID := Module[{}, Print["Selecting a proper ID for mushroom names ..."]; Do[ data = iddata[name]; data = Map[removetags, data, {2}]; noPrint["Selecting a proper ID for the name: ", name]; (* Assigning rank *) mdata = Length[data]; n = 0; ranks = Table[ n++; {id, found, item, type, kingdom, ref} = dat; basename = idcomponents["name", id]; r = 0; If[StringMatchQ[type, "accepted" ~~ __], r = r + 10]; If[StringMatchQ[type, "synonym for" ~~ __], r = r + 0.1]; If[StringMatchQ[type, "ambiguous synonym for" ~~ __], r = r + 0.05]; If[StringMatchQ[type, "misapplied name" ~~ __], r = r + 0.02]; If[StringMatchQ[found, name <> " " ~~ __], r = 4 r]; If[item === "Species", r = 2 r]; If[kingdom === "Fungi", r = 2 r]; If[name === basename, r = 5 r]; r = r + 0.01 (mdata - n); {r, id, basename, item, found, type, kingdom}, {dat, data}]; {rs, ids, basenames, items, founds, types, kingdoms} = Transpose[ranks]; rmax = Max[rs]; likes = If[# == rmax, "*", " "] & /@ rs; rs = printf /@ rs; founds = shrink[#, 40, 20, " ... "] & /@ founds; ranks = {likes, rs, ids, basenames, founds, items, types, kingdoms} // Transpose; pranks = Prepend[ranks, {" ", "Rank", "ID", "Accepted name", "Item found", "Type of the item", "Relation", "Kingdom"}]; nPrint[pranks // TableForm]; select = Select[ranks, #[[1]] === "*" &][[1]]; {id, basename} = select[[{3, 4}]]; prolog = DateString[] <> " Items found for the search string: " <> name <> "\n"; epilog = If[basename === name, "", "\n" <> name <> " -> " <> basename]; dirout = ToFileName[{dir0, "search"}]; checkdir1[dirout]; outfile = ToFileName[dirout, ToLowerCase[StringReplace[name, " " -> "_"]] <> ".gif"]; If[FileType[outfile] =!= File, Export[outfile, SequenceForm[prolog, pranks // TableForm, epilog]]; ]; If[basename =!= name, Print[name <> " -> " <> basename]]; selectid[name] = id, {name, namespic}]; (* Printing replacements *) repls = Table[ id = selectid[name]; basename = idcomponents["name", id]; If[basename === name, "", "\"" <> name <> "\" -> \"" <> basename <> "\","], {name, namespic}]; repls = Select[repls, # =!= "" &]; filerepl = ToFileName[dirwork, "replnames.m"]; filesave = ToFileName[dirwork, "repls.txt"]; Export[filesave, repls, "Text"]; If[repls =!= {}, Print["Following lines are saved to the file ", filesave, " and should be added to the file ", filerepl, ": \n"]; Print[repls // TableForm]; ]; ];