The methods required by the assignment all work with Strings. These notes don't change that requirement. However, in many cases it's more convenient to work with "words," as in an Array or List of Strings. Fortunately, Scala provides methods that make it easy to convert between these two representations.
Another useful method is "contains."
scala> "This is a string.".split(" ") res0: Array[String] = Array(This, is, a, string.) scala> val s = "This string has lots of spaces, \t\t tabs, and \nnewlines" s: String = This string has lots of spaces, tabs, and newlines scala> s.split("\\W+") res1: Array[String] = Array(This, string, has, lots, of, spaces, tabs, and, newlines) scala> List("These", "are", "some", "words").mkString(" ") res3: String = These are some words scala> List("These", "are", "some", "words").mkString("[before] ", " ", ".") res4: String = [before] These are some words.
scala> List("one", "two", "three").contains("two") res5: Boolean = true
Whenever you want to generate an arbitrary number of things (for example, a Predstring consists of one or more PREDs), the following approach works well:
It turns out that, if p = 1/2, then the average number of things generated is two. This procedure could generate a huge number of things--and you could win the lottery--but it's really not worth considering.
Note: When you generate a sentence to display to the user, put one space between words. When you are trying to recognize what the user types in (over which you have little control!), you should allow any whitespace--that is, not reject a sentence just because they started with a space, or used a tab, or something like that.
There are a lot of ways you might recognize Lojban sentences, but here are two that should work.
la sruda ba pakde rodmuas a Sentence:
lais a LA; skip past it.
srudais a PRED; skip past it. We have a Predname.
Replace the "words" in the sentence with their "parts of speech". Repeat with higher-level concepts. For example,
la sruda e pakde →
You do have to be careful with the ordering in this approach. If you replace
(for example), you can get stuck and fail to recognize a valid sentence.