10
$\begingroup$

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".

$\endgroup$
  • 3
    $\begingroup$ Have you tried wrapping InputForm around? $\endgroup$ – Leonid Shifrin May 15 '12 at 13:57
11
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – programming_historian May 15 '12 at 14:12
  • $\begingroup$ We should be very careful about creating the style sheet because it can affect future results if we forget this setting. This setting is very specific. $\endgroup$ – LCarvalho Jan 4 '18 at 15:33
  • $\begingroup$ @Mr.Wizard Your answer was very good, but an alert on that would be convenient. $\endgroup$ – LCarvalho Jan 4 '18 at 15:36
  • $\begingroup$ @LCarvalho What kind of alert do you have in mind? The procedure given will create a style sheet local to the Notebook in use so it should not affect other Notebooks unexpectedly. If one wants to affect only a single output InputForm is a better choice as already acknowledged. If you have a warning about style sheets in general in mind I could see that as being useful, perhaps something for the pitfalls omnibus, but I wouldn't want to make this answer overly long with a general guide to style sheets. $\endgroup$ – Mr.Wizard Jan 4 '18 at 23:41
  • $\begingroup$ I do not know what wrong procedure I took that ended up affecting new stylesheets. My old stylesheets were not affected. Even more that I was careful not to choose the stylesheet with modified formatting. $\endgroup$ – LCarvalho Jan 5 '18 at 8:22
4
$\begingroup$

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;
$\endgroup$
  • $\begingroup$ +1 thanks for catching that, very appreciated. $\endgroup$ – programming_historian May 15 '12 at 16:15
  • $\begingroup$ @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. $\endgroup$ – Ajasja May 15 '12 at 18:08

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.