Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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):

Manipulate[
 viewerCount1 = {};
 SetDirectory["/users/ME/desktop/DB/Put/"];
 filenames = FileNames["*trigrams-put.txt"];
 Do[
  input = Get[file];
  Clear[trigramHash];
  (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", 
   "Word3:"}}]

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)

share|improve this question
2  
Can't you use StringSplit? –  Heike Apr 18 '12 at 20:35
add comment

1 Answer 1

up vote 9 down vote accepted

I think you can just use StringSplit at Whitespace characters.

Here is a toy example that might get you started.

Manipulate[
 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

share|improve this answer
    
This is great. I will play around with this! –  programming_historian Apr 18 '12 at 22:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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