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 am running into the following seemingly simple problem. I have this code:

testphrase="i love you";
input=ToLowerCase[StringSplit[testphrase, Whitespace]]

It outputs, input={i,love,you}

However, I want to use input elsewhere in my program as "i","love","you". So the behaviour I am looking for is as such:

input={"i","love","you"}

I know if I paste input, the quotation marks show up around it. I know the issue resides somewhere in ShowStringCharacters, as I've read that documentation, but I'm getting mixed up in the different behaviour between pasting it, manually inputting it, and calling on it in Mathematica. My program will only work if the output is formatted in three words with quotation marks, and won't work if the quotation marks aren't there.

--- edited to add ---

The solution below looks like it should work, but I am still getting odd behaviour. Here is my whole code.

The follow example outputs to count the number 111, as it should.

trigrams={{{"i", "don", "t"}, 211}, {{"i", "can", "t"}, 
  186}, {{"you", "don", "t"}, 175}, {{"don", "t", "know"}, 
  137}, {{"oh", "oh", "oh"}, 111}};
Clear[trigramHash];
(trigramHash[Sequence @@ #1] = #2) & @@@ trigrams;
count = trigramHash["oh","oh","oh"]; trigramHash[___] = 0; 

Yet this still outputs zero.

test = InputForm[ToLowerCase[StringSplit["oh oh oh", Whitespace]]];
trigrams = {{{"i", "don", "t"}, 211}, {{"i", "can", "t"}, 
    186}, {{"you", "don", "t"}, 175}, {{"don", "t", "know"}, 
    137}, {{"oh", "oh", "oh"}, 111}};
Clear[trigramHash];
(trigramHash[Sequence @@ #1] = #2) & @@@ trigrams;
count = trigramHash[test]; trigramHash[___] = 0;

Even though, on the face of it, test should work - it is outputting as "oh","oh","oh".

share|improve this question
2  
Have you tried wrapping InputForm around? –  Leonid Shifrin May 15 '12 at 13:57
add comment

2 Answers 2

up vote 11 down vote accepted

Format > Edit Style Sheet...

Enter style name: Output

Select Output cell, open Option Inpector, and change ShowStringCharacters to True.

Done.

(For a one-time-use InputForm works, as Leonid suggested in a comment.)

share|improve this answer
    
This works great, but when I plug it into my code it is still giving odd behaviour. I might just edit the question to show since it won't fit in the comment. –  programming_historian May 15 '12 at 14:12
add comment

The problem is that you forgot to apply trigramHash to test. This code works correctly

test = ToLowerCase[StringSplit["oh oh oh", Whitespace]]
trigrams = {{{"i", "don", "t"}, 211}, {{"i", "can", "t"}, 
    186}, {{"you", "don", "t"}, 175}, {{"don", "t", "know"}, 
    137}, {{"oh", "oh", "oh"}, 111}};
Clear[trigramHash];
(trigramHash[Sequence @@ #1] = #2) & @@@ trigrams;
count = trigramHash @@ test
trigramHash[___] = 0;
share|improve this answer
    
+1 thanks for catching that, very appreciated. –  programming_historian May 15 '12 at 16:15
    
@ian.milligan No problem. I also hope it's clear now, that the elements of the list are strings even if no quotation marks are displayed. –  Ajasja May 15 '12 at 18:08
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.