Patterns in Mathematica
I struggle some with patterns while trying to extract words out of large texts. Let us say that I have a long text (Strindberg) that we can call dictionary
.
I have implemented the flowing extracting filters.
Search for words containing both
"x"
and"y"
in that orderSelect[dictionary, With[{pat = #}, StringLength[pat] == i && StringMatchQ[#, ___ ~~ "x" ~~ __ ~~ "y" ~~ ___] &]]
Search for words that start with
"x"
Select[dictionary, With[{pat = #}, StringLength[pat] == i && StringMatchQ[#, "x" ~~ ___] &]]
…and a few others. I can post them if there is any interest.
Search for words that start with
"x"
Search for words that end with
"x"
Search for words that contain
"x"
or"y"
in any order
QUESTION. I also would like to have the following filters.
Search for words that contain both
"x"
and"y"
, but in any orderSearch for words that contain
"x"
but not"y"
Then I would like to refine the outputs above but taking into consideration the statistics of the language at hand. Some words from the results above might include unusual letters (z,w) not common in Swedish. Those words should be in the results but in a tail, i e first quite common words (with common letters with high occurrence frequency) and then more exotic words (with uncommon letters).
The issue in 8 is especially important but I cannot find how to implement a solution. How does one include statistics of the language in search algorithms? I have a list of the occurrences of the Swedish letters.
Any pointers in the correct direction would be much appreciated. Thank you very much in advance.
Select[{"xy", "x", "y"}, ! StringFreeQ[#, {"x", "y"}] &]
Select[{"xy", "x", "y"}, Not[StringFreeQ[#, "x"] || StringFreeQ[#, "y"]] &]
, andSelect[{"xy", "x", "y"}, StringFreeQ[#, "y"] && Not@StringFreeQ[#, "x"] &]
$\endgroup$