3
$\begingroup$

I'm unable to come up with a solution to the following question in the "Introduction" online book, Chapter 42:

"42.6 Make any 2-letter word uppercase in the first sentence from the Wikipedia article on computers."

I can do this with multiple procedural statements, but I'm unable to do it in a functional style using StringReplace. I would be grateful to hear any ideas.

$\endgroup$
1
  • $\begingroup$ Welcome to the Mathematica Stack Exchange. Answers are provided at the end of the book. $\endgroup$
    – Syed
    May 17 at 15:54

2 Answers 2

2
$\begingroup$
StringReplace[
  StringSplit[WikipediaData["Computers"], ". "][[1]], 
  WordBoundary ~~ w : Repeated[LetterCharacter, {2}] ~~ WordBoundary :> ToUpperCase[w]
]

(* Out: "A computer IS a digital electronic machine that can BE programmed TO 
carry out sequences OF arithmetic OR logical operations (computation) automatically" *)
$\endgroup$
2
  • $\begingroup$ Nice solution! I kept banging away at it before I saw your solution, and came up with a similar (but inferior) solution. I'm thinking it might be better to use TextSentences[WikipediaData["computers"]][[1]] because Split[] deletes the period at the end of the sentence. $\endgroup$
    – Jim Marks
    May 17 at 17:54
  • $\begingroup$ @JimMarks I agree: TextSentences would be even better than my quick and dirty StringSplit. $\endgroup$
    – MarcoB
    May 17 at 17:56
6
$\begingroup$

Decompose the problem. For example, "Make any 2-letter word uppercase" is a problem that you can probably solve (hint, there is a built in function for that).

Now move on to "Make any 2-letter word uppercase in the first sentence...". So, if you were given a sentence, how would you apply your uppercase function to the two-letter words? A sentence is just text, and two-letter words are just text, so given a string of text, we need to solve two sub-problems:

  • Finding two-letter words
  • Apply a function to a substring that we've found

A two-letter words is just two adjacent letters surrounded by whitespace (whether words can include non-letters might be something to think about, but I think you're fine if you ignore that complexity). So, you'll need to create some sort of search pattern (hint, look at RegularExpression and/or StringExpression).

Applying a function to a substring found in a larger string is what StringReplace does, which you already know, so these two subproblems should be tractable for you.

Moving on again to "the Wikipedia article on computers". At first this might seem intimidating, but it wouldn't make sense for them to give you a problem about StringReplace if there weren't some easy way to deal with this sub-problem. So, you would first search for "wikipedia" in the Mathematica documentation, and indeed, there is a built-in function that'll do the trick.

Next: "the first sentence from the Wikipedia article on computers". We need to find a pattern that will extract the first sentence. You could jump in and try it directly using the fact that sentences are delimited by periods. Or you might be lazy and hope that yet another built-in function will help. Search the Mathematica documetation for "sentence" and you might find a perfect built-in for finding sentences. Extracting the first one is presumably something you already know how to do.

Now, put it all together.

$\endgroup$
1
  • $\begingroup$ Very nice; this is a very pedagogic approach! (+1) $\endgroup$
    – MarcoB
    May 17 at 17:55

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.