Spyral.Categories(config) → {Spyral.Categories}

Class for working with categories and features. Categories are groupings of terms. A term can be present in multiple categories. Category ranking is used to determine which feature value to prioritize. Features are arbitrary properties (font, color) that are associated with each category.

new Categories(config) → {Spyral.Categories}

Construct a new Categories class.

Parameters:
Name Type Description
config Object

The config object

Properties
Name Type Description
categories Object

An object that maps arrays of terms to category names

categoriesRanking Array

An array of category names that determines their ranking, from high to low

features Object

An object that maps categories to feature names

featureDefaults Object

An object that maps default feature value to feature names

Example
new Spyral.Categories({
  categories: {
    positive: ['good', 'happy'],
    negative: ['bad', 'sad']
  },
  categoriesRanking: ['positive','negative'],
  features: {color: {}},
  featureDefaults: {color: '#333333'}
})

Methods

addCategory(name)

Add a new category.

Parameters:
Name Type Description
name String

The category name

addFeature(name, defaultValue)

Add a feature.

Parameters:
Name Type Description
name String

The feature name

defaultValue *

The default value

addTerm(category, term)

Add a term to a category.

Parameters:
Name Type Description
category String

The category name

term String

The term

addTerms(category, terms)

Add multiple terms to a category.

Parameters:
Name Type Description
category String

The category name

terms Array

An array of terms

getCategories() → {Object}

Get the categories.

Returns:
Object

getCategoriesForTerm(term) → {Array}

Get all the categories a term belongs to.

Parameters:
Name Type Description
term String

The term

Returns:
Array

getCategoryExportData() → {Object}

Get a copy of the category and feature data.

Returns:
Object

getCategoryFeature(categoryName, featureName) → {*}

Get the feature for a category.

Parameters:
Name Type Description
categoryName String

The category name

featureName String

The feature name

Returns:
*

getCategoryForTerm(term) → {string}

Get the category that a term belongs to, taking ranking into account.

Parameters:
Name Type Description
term String

The term

Returns:
string

getCategoryNames() → {Array}

Get category names as an array.

Returns:
Array

getCategoryRanking(name) → {number}

Gets the ranking for a category.

Parameters:
Name Type Description
name String

The category name

Returns:
number

getCategoryTerms(name) → {Array}

Get the terms for a category.

Parameters:
Name Type Description
name String

The category name

Returns:
Array

getFeatureForTerm(feature, term) → {*}

Get the feature for a term.

Parameters:
Name Type Description
feature String

The feature

term String

The term

Returns:
*

getFeatures() → {Object}

Get the features.

Returns:
Object

load(config, apiopt) → {Promise.<Object>}

Load the categories (if we're in a recognized environment).

In its simplest form this can be used with a single string ID to load:

new Spyral.Categories().load("categories.en.txt")

Which is equivalent to:

new Spyral.Categories().load({retrieveResourceId: "categories.en.txt"});
Parameters:
Name Type Attributes Description
config Object | String

an object specifying the parameters (see above)

api Object <optional>

an object specifying any parameters for the trombone call

Returns:
Promise.<Object> -

this first returns a promise and when the promise is resolved it returns this categories object (with the loaded data included)

removeCategory(name)

Remove a category.

Parameters:
Name Type Description
name String

The category name

removeFeature(name)

Remove a feature.

Parameters:
Name Type Description
name String

The feature name

removeTerm(category, term)

Remove a term from a category.

Parameters:
Name Type Description
category String

The category name

term String

The term

removeTerms(category, terms)

Remove multiple terms from a category.

Parameters:
Name Type Description
category String

The category name

terms Array

An array of terms

renameCategory(oldName, newName)

Rename a category.

Parameters:
Name Type Description
oldName String

The old category name

newName String

The new category name

save(config, apiopt) → {Promise.<String>}

Save the categories (if we're in a recognized environment).

Parameters:
Name Type Attributes Description
config Object

for the network call (specifying if needed the location of Trombone, etc., see Spyral.Load#trombone

api Object <optional>

an object specifying any parameters for the trombone call

Returns:
Promise.<String> -

this returns a promise which eventually resolves to a string that is the ID reference for the stored categories

setCategoryFeature(categoryName, featureName, featureValue)

Set the feature for a category.

Parameters:
Name Type Description
categoryName String

The category name

featureName String

The feature name

featureValue *

The feature value

setCategoryRanking(name, ranking)

Sets the ranking for a category.

Parameters:
Name Type Description
name String

The category name

ranking number

The category ranking

static load(config, apiopt) → {Promise.<Object>}

Load categories and return a promise that resolves to a new Spyral.Categories instance.

Parameters:
Name Type Attributes Description
config Object | String

an object specifying the parameters (see above)

api Object <optional>

an object specifying any parameters for the trombone call

Returns:
Promise.<Object> -

this first returns a promise and when the promise is resolved it returns this categories object (with the loaded data included)