Get and set parameters of a language model.
param(object, which)
# S3 method for class 'kgram_freqs'
param(object, which)
param(object, which) <- value
parameters(object)
a list for parameters()
, a single value, typically numeric,
for param()
and param()<-
(the new value, in this last case).
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_model
s allow to set N
less
than or equal to the order of the underlying kgram_freqs
object.
In addition to these, language_model
s 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.
# Get and set k-gram model parameters
f <- kgram_freqs("a a b a b", 3)
param(f, "N")
#> [1] 3
parameters(f)
#> $N
#> [1] 3
#>
#> $V
#> [1] 2
#>
m <- language_model(f, "sbo", lambda = 0.2)
param(m, "V")
#> [1] 2
param(m, "lambda")
#> [1] 0.2
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!
#> [1] 0.5
m2 <- language_model(m) # This creates a true copy
param(m2, "lambda") <- 0.6
param(m, "lambda") # 0.5
#> [1] 0.5