I have the following program that plots the number of trigram appearances across a corpus of textual information between 1964 and 1989 (apologies for the messy code):

 viewerCount1 = {};
 filenames = FileNames["*trigrams-put.txt"];
  input = Get[file];
  (trigramHash[Sequence @@ #1] = #2) & @@@ input;
  count = trigramHash[word1, word2, word3]; trigramHash[___] = 0; 
  AppendTo[viewerCount1, count];
  , {file, filenames}];
 DateListPlot[viewerCount1, {1964}, Joined -> True, 
  PlotLabel -> "Number of Trigram Appearances"], {{word1, "i", 
   "Word1:"}}, {{word2, "love", "Word2:"}}, {{word3, "you", 

Right now, it looks like so:

enter image description here

What this is doing is initially seeing how frequent the phrase "i love you" is across files like this:

{{{"i", "don", "t"}, 102}, {{"i", "m", "gonna"}, 67}, 
 {{"wa", "wa", "wa"}, 66}, {{"i", "can", "t"}, 66}, {{"i", "ll", "be"}, 66}, 
 {{"i", "love", "you"}, 62}, {{"you", "don", "t"}, 55}, 
 {{"la", "la", "la"}, 50}, {{"don", "t", "you"}, 49}, 
 {{"don", "t", "know"}, 48}, {{"ain", "t", "that"}, 47}, 

The problem is that in this version you have to type word1 as "i", word2 as "love", and word3 as "you".

Would there be a way for Mathematica to detect "i love you" in the Manipulate box, and then return responses as if it was "i" "love" "you"? So it could just be a phrase box, and it would split them in to three parts. (I have separate programs right now for unigrams, bigrams, trigrams, and quadgrams)

  • 2
    $\begingroup$ Can't you use StringSplit? $\endgroup$
    – Heike
    Apr 18, 2012 at 20:35

1 Answer 1


I think you can just use StringSplit at Whitespace characters.

Here is a toy example that might get you started.

 PieChart[MapThread[Labeled[#2, #1] &, 
   Transpose[Tally@StringSplit[ngram, Whitespace]]]], {{ngram, 
   "One fish two fish red fish blue fish", "n-gram"}}]

enter image description here

  • $\begingroup$ This is great. I will play around with this! $\endgroup$ Apr 18, 2012 at 22:04

Your Answer

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

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