6
$\begingroup$

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)

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

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

$\endgroup$
  • $\begingroup$ This is great. I will play around with this! $\endgroup$ – programming_historian Apr 18 '12 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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