However, this results in splitting words like “men’s”, “can’t”, “don’t”, etc into two words. What regular expression can I use to keep words like these intact?
I used your suggestion, with a simple modification. So now we have the original and the enhanced.
ParseText[
text_] := (* text is string, result is lower case list of words within text *)
StringSplit[ToLowerCase[text], RegularExpression["\\W+"]]
ParseText2[
text_] := (* text is string, result is lower case list of words within text *)
StringSplit[ToLowerCase[text], Except[WordCharacter | "'"]]
One would think the enhanced solves all problems. Alas, no such luck.
Merely adding a comma in the sample phrase results in the space after the comma being recognized as a separate word. According to the documentation RegularExpression[“\W+”] is equivalent to Except[WordCharacter], so adding the pattern “[RawQuote]” to the Except pattern should not cause problems. It does. If this result is not a bug in Mathematica, I don’t understand its logic.
Why not try the opposite - split at white space and/or a list of other atomic characters to exclude so we would use:
In[1]:= parseText[text_] :=
StringSplit[
ToLowerCase[text], {RegularExpression["\\s|[\\.,\\,,\\:,\\;]"]}];
sample1 = "These are the times that try men's souls.";
parseText[sample1]
Out[3]= {"these", "are", "the", "times", "that", "try", "men's", \
"souls"}
keep adding after the “|” in the regular expressions as you need to exclude other characters. If you need to exclude single quotes when they are not representing contractions, such as “say ‘hello world’” the single quote will be part of the words. The http://regexlib.com/ is a good place to start.