Consider using a dedicated search pattern making function.
Here is an example.
molekule = "Sulfuric Acid";
Clear[MakeSearchPattern]
MakeSearchPattern[s_String] :=
StringExpression @@
Join[{BlankNullSequence[]},
Riffle[StringSplit[ToLowerCase[s]],
BlankNullSequence[]], {BlankNullSequence[]}];
MakeSearchPattern[molekule]
(* ___ ~~ "sulfuric" ~~ ___ ~~ "acid" ~~ ___ *)
Select[ChemicalData[#, "StandardName"] & /@ ChemicalData[],
StringMatchQ[#, MakeSearchPattern[molekule], IgnoreCase -> True] &]
(* {"SulfuricAcid", "SulfuricAcidSolution", \
"DeuteratedSulfuricAcidDeuteriumOxide", "PeroxysulfuricAcid", \
"NitrosylsulfuricAcid", "SodiumSaltSulfuricAcid,MonohexylEster", \
"TitaniumIVOxysulfateSulfuricAcidHydrate", \
"CeriumIVSulfateHydrate,ComplexWithSulfuricAcid"} *)
Update : more complicated search
is there any way to fix the exmpty list that is returned when exchanging Acid with Sulfuric ("Acid Sulfuric" instead of "Sulfuric Acid")
The following function
does the search with the straight string and the reverse string first,
if the result is empty, searches for each search word are made and the most frequent results are returned first.
The second, fall-back step can be slow since multiple searches are made.
Note that the result of the first step is extended to be pairs of {_String,_Integer}
in order to make the return values of MoleculeSearch
consistent.
Clear[MoleculeSearch]
MoleculeSearch[s_String] :=
Block[{swords, pats, res},
swords = StringSplit[ToLowerCase[s]];
pats = Map[MakeSearchPattern, swords];
res = Select[ChemicalData[#, "StandardName"] & /@ ChemicalData[],
StringMatchQ[#,
MakeSearchPattern[swords] | MakeSearchPattern[Reverse[swords]],
IgnoreCase -> True] &];
If[Length[res] > 0, Transpose[{res, ConstantArray[1, Length[res]]}],
res =
Flatten@Map[
Function[{sp},
Select[ChemicalData[#, "StandardName"] & /@ ChemicalData[],
StringMatchQ[#, sp, IgnoreCase -> True] &]], pats];
SortBy[Tally[res], -#[[2]] &]
]
];
MoleculeSearch["acid sulfuric"]
(* {{"SulfuricAcid", 1}, {"SulfuricAcidSolution",
1}, {"DeuteratedSulfuricAcidDeuteriumOxide",
1}, {"PeroxysulfuricAcid", 1}, {"NitrosylsulfuricAcid",
1}, {"SodiumSaltSulfuricAcid,MonohexylEster",
1}, {"TitaniumIVOxysulfateSulfuricAcidHydrate",
1}, {"CeriumIVSulfateHydrate,ComplexWithSulfuricAcid", 1}} *)
Take[MoleculeSearch["acid sulfuric ester"], UpTo[20]]
(* {{"SodiumSaltSulfuricAcid,MonohexylEster", 3}, {"1,1,4,4Tetramethyl1,4ButanediylEsterHexaneperoxoicAcid,\
2Ethyl", 2}, \
{"1,1Benzyl3Hydroxy2OxoPropylcarbamoyl2PhenylEthylCarbamicAcidBenzylEs\
ter", 2}, \
{"1,1DimethylethylEster1H,5H,11H1Benzopyrano6,7,\
8IjQuinolizine10CarboxylicAcid,2,3,6,7Tetrahydro11Oxo",
2}, {"1,1DimethylethylEster2ButeneperoxoicAcid",
2}, {"1,1DimethylethylEsterHexaneperoxoicAcid,2Ethyl",
2}, {"1,1FerrocenediboronicAcidBisPinacolEster",
2}, {"1,1Methyl4,\
5DioxoPent2Enylcarbamoyl2PhenylEthylCarbamicAcidBenzylEster",
2}, {"124CarbamimidoylPhenylaminoMethyl1Methyl1hBenzoimidazol5YlCycl\
opropylPyridin2YlMethyleneaminooxyAceticAcidEthylEster",
2}, {"1,2Chlorophenyl3,\
5Dimethyl1hPyrazole4CarboxylicAcidEthylEster",
2}, {"12HydroxyoctadecanoicAcidMethylEster",
2}, {"1,3Hydroxy2Oxo1PhenethylPropylcarbamoyl2PhenylEthylCarbamicAci\
dPyridin4YlmethylEster",
2}, {"1,4Aminophenyl3,5Dimethyl1hPyrazole4CarboxylicAcidEthylEster",
2}, {"1,4BenzenediboronicAcidBisPinacolEster",
2}, {"1,4Methoxyphenyl3,\
5Dimethyl1hPyrazole4CarboxylicAcidEthylEster",
2}, {"1,4PhenylenebisMethyleneEsterSelenocyanicAcid",
2}, {"16DoxylStearicAcidMethylEster",
2}, {"1Benzylpyrazole4BoronicAcidPinacolEster",
2}, {"1BocIndole5BoronicAcidPinacolEster",
2}, {"1Cyclohexen1YlBoronicAcidPinacolEster", 2}} *)
StringTrim
does not eliminate whitespaces between words $\endgroup$