logo CodeStepByStep logo

vocabTypesOfPOS

Language/Type: JavaScript json objects
Author: Melissa Hovik (on 2018/10/22)

This problem will work with JSON-formatted data representing a "vocabulary set" of terms and definitions in the following JSON schema:

{ 
    "setname" : String,
    "terms" : [
        { "term" : String, "pos" : String, "definition" : String },
        { "term" : String, "pos" : String, "definition" : String },
        ...
    ]
}

Each vocabulary set has a name and a collection of terms, each having a single POS (part-of-speech type) and definition.

Write a function named typesOfPOS that takes as a parameter an object called vocab using the above schema and returns a new object with terms as keys each mapping to an array of POS types based on the vocab parameter. Each array's elements should be in lowercase and in the same order as in the passed vocab (for example, if a term with a "noun" POS occurs earlier in the terms array than an entry for the same term with an "adjective" POS, "noun" should occur earlier than "adjective" in the result array associated by that term). If a term in the passed vocab has more than one entry with the same POS type (e.g. "green" in the following example having two entries with "adjective" as a POS type), the earliest occurrence should be the one added to the result array for that term.

For example, if the following vocabulary set is defined:

{
    "setname" : "Vocabulary Set 1",
    "terms" : [
        { "term" : "alert", 
          "pos" : "adjective", 
          "definition" : "fully aware and attentive" },
        { "term" : "alert", 
          "pos" : "noun", 
          "definition" : "warning or alarm" },
        { "term" : "alert", 
          "pos" : "adjective", 
          "definition" : "to advise or warn" },
        { "term" : "annex", 
          "pos" : "noun", 
          "definition" : "extension to a building" },
        { "term" : "annex", 
          "pos" : "verb",
          "definition" : "to take away from" },
        { "term" : "debris",
          "pos" : "noun",
          "definition" : "the remains of anything destroyed; rubble" },
        { "term" : "green",
          "pos" : "adjective",
          "definition" : "covered with herbage or foliage; verdant"},
        { "term" : "green",
          "pos" : "NOUN",
          "definition" : "an effect of light with a wavelength between 500 and 570nm" },
        { "term" : "green",
          "pos" : "adjective",
          "definition" : "immature in age or judgement; untrained" },
        { "term" : "observe",
          "pos" : "NOUN",
          "definition" : "to see or watch" },
        { "term" : "observe",
          "pos" : "verb",
          "definition" : "to show regard for by some appropriate procedure"
        }
    ]
};

then the call typesOfPOS(vocab) should return the following object:

{ "alert" : ["noun", "adjective", "verb"],
  "annex" : ["noun", "verb"],
  "debris" : ["noun"],
  "green" : ["adective", "noun"],
  "observe" : ["noun"]
}

Note that "adjective" comes before "noun" for the term "green" due to the order of the entries for "green" in the passed vocab. If the terms array is empty, your function should return the empty object, {}.

You may assume that the passed vocab is non-null but if the "terms" key is missing, your function should throw an exception with the message, "Passed object must have 'terms' key".

You may otherwise assume all of the keys of each object in the "terms" array are defined and have the datatype specified in the provided schema.

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.