parameters: Language Model Parameters

View source: R/parameters.R

parametersR Documentation

Language Model Parameters

Description

Get and set parameters of a language model.

Usage

param(object, which)

## S3 method for class 'kgram_freqs'
param(object, which)

param(object, which) <- value

parameters(object)

Arguments

object

a language_model or kgram_freqs class object.

which

a string. Name of the parameter to get or set.

value

new value for the parameter specified by which. Typically a length one numeric.

Details

These functions are used to retrieve or modify the parameters of a language_model or a kgram_freqs object. Any object of, or inheriting from, any of these two classes has at least two parameters:

  • N: higher order of k-grams considered in the model for language_model, or stored in memory for kgram_freqs.

  • V: size of the dictionary (excluding the special tokens BOS(), EOS(), UNK()).

For an object of class kgram_freqs, these are the only parameters, and they are read-only. language_models allow to set N less than or equal to the order of the underlying kgram_freqs object.

In addition to these, language_models can have additional parameters, e.g. discount values or interpolation constants, depending on the particular smoother employed by the model. A list of parameters available for a given smoother can be obtained through info() (see smoothers).

It is important to mention that setting a parameter is an in-place operation. This implies that if, say, object m is a language_model object, the code m1 <- m ; param(m1, which) <- value will set the parameter which to value both for m1 and m. The reason for this is that, behind the scenes, both m and m1 are pointers to the same C++ object. In order to create a true copy, one can use the copy constructor language_model(), see language_model.

Value

a list for parameters(), a single value, typically numeric, for param() and param()<- (the new value, in this last case).

Examples

# Get and set k-gram model parameters

f <- kgram_freqs("a a b a b", 3)
param(f, "N")
parameters(f)

m <- language_model(f, "sbo", lambda = 0.2)
param(m, "V")
param(m, "lambda")
param(m, "N") <- 2
param(m, "lambda") <- 0.4

if (FALSE) {
        param(m, "V") <- 5 # Error: dictionary size cannot be set.  
}

if (FALSE) {
        param(f, "N") <- 4 # Error: parameters of 'kgram_freqs' cannot be set  
}

m1 <- m
param(m1, "lambda") <- 0.5
param(m, "lambda") # 0.5 ; param() modifies 'm' by reference!

m2 <- language_model(m) # This creates a true copy
param(m2, "lambda") <- 0.6
param(m, "lambda") # 0.5


kgrams documentation built on Oct. 6, 2023, 5:06 p.m.