09 December 2008 - java_dev (original) (raw)

| | 08:26 pm - node - nouns and plurals I'm trying to convert some elisp to Java. It's pretty simple, actually. There's an array of nouns, and a two functions to return either a random noun, or a random "plural". My problem is with encoding the data for Java efficiently (I'm writing a toy app for Android). In the elisp, the array of nouns has elements that are either strings, or arrays, like so: (defconst nouns ["beef" "perspiration" ["cow"] ["dwarf" "dwarves"] ["party" ies -1] ...]) The function to return a noun gets a random element from the array, and if it's a string, returns it. If it's an array, it returns the first element of the array. The function to return a pluralized noun has to do more work. It gets a random element from the array. If it's a string, it returns it (strings are words that have no plurals, or their plural is the same as their singular). If it's an array of length 1, it appends an "s" to it, and returns that. If it's an array of length 2, it returns the second element. If it's an array of length 3, it substring's the first element by the number of third element letters, and then appends the second element to the result, and returns it. In other words, (plural "beef") => beef (plural ["cow"]) => cows (plural ["dwarf" "dwarves"]) => dwarves (plural ["party" ies -1]) => parties It may seem odd, but it's actually very space-efficient. I don't know the source of the noun wordlist (something has to explain why words like party have a special case and words like dwarf don't). It's not a list of true plurals; for example, it has ["Budweiser" "buds"], and ["frivolity" "cheater-hawks"]. My problem is with replicating this data structure in Java. I can (and have!) massage the array and output a class field like String[] singulars = ["beef", "cow", "dwarf", "party", ...]; and a similar one for plurals, but that takes up a lot of space (beef and perspiration end up in both lists). Is there a better way to encode the data structure? I haven't really written any Java since the 1.x days... | | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |