automaton.evaluate(w)

Evaluates the weight of the given word through the automata.

Preconditions:

  • w must be a valid word in the labelset.
  • automaton must not have spontaneous cycles.

Examples

In [1]:
import vcsn
In [2]:
a = vcsn.context('lal(ab), b').de_bruijn(2)
a
Out[2]:
%3 I0 0 0 I0->0 F3 0->0 a, b 1 1 0->1 a 2 2 1->2 a, b 3 3 2->3 a, b 3->F3
In [3]:
a.evaluate('b')
Out[3]:
$\bot$
In [4]:
a.evaluate('bbabb')
Out[4]:
$\top$

You can also write automaton('word') to evaluate a word:

In [5]:
a('bbabb')
Out[5]:
$\top$
In [6]:
a = vcsn.Z.expression('(<2>C+c)los(<3>e(s+<4>d)+ing)').standard()
a.shortest(10)
Out[6]:
$\left\langle 24\right\rangle \mathit{Closed} \oplus \left\langle 6\right\rangle \mathit{Closes} \oplus \left\langle 12\right\rangle \mathit{closed} \oplus \left\langle 3\right\rangle \mathit{closes} \oplus \left\langle 2\right\rangle \mathit{Closing} \oplus \mathit{closing}$
In [7]:
a.evaluate('Closing')
Out[7]:
$2$
In [8]:
a.evaluate('close') # but not enough
Out[8]:
$0$

All automaton types are supported, evaluate is not limited to free labelsets. For instance, with word-labeled automata:

In [9]:
a = vcsn.context('law,q').expression('<2>(ab(<3>cd)*(ef))<5>', "associative").automaton()
a
Out[9]:
%3 I0 0 0 I0->0 F2 1 1 0->1 ⟨2⟩ab 1->1 ⟨3⟩cd 2 2 1->2 ef 2->F2 ⟨5⟩
In [10]:
a.evaluate('abcdef')
Out[10]:
$30$
In [11]:
a.evaluate('abcdcdef')
Out[11]:
$90$

Polynomials are also supported:

In [12]:
p = vcsn.context('law,q').polynomial('<2>abcdef+abcdcdef')
a.evaluate(p)
Out[12]:
$150$

Spontaneous transitions are allowed:

In [13]:
%%automaton -s a
context = "wordset<char_letters(abcdef)>, q"
$ -> 0 <2>
0 -> 1 \e
1 -> 1 <3>cd
1 -> 2 \e
2 -> $ <5>
%3 I0 0 0 I0->0 ⟨2⟩ F2 1 1 0->1 ε 1->1 ⟨3⟩cd 2 2 1->2 ε 2->F2 ⟨5⟩
In [14]:
a.evaluate("cd")
Out[14]:
$30$
In [15]:
a.evaluate("")
Out[15]:
$10$

Tuplesets are fully supported:

In [16]:
ctx = vcsn.context('lat<lan, lan>, zmin')
a = ctx.expression('(<0>(a|a+b|b))* (<1>[^]|\e + <1>\e|[^] + <2>(a|[^a]+b|[^b])){*}').automaton()
a #Compute the LCS distance between two words
Out[16]:
%3 I0 0 0 I0->0 ⟨0⟩ F0 F1 0->F0 ⟨0⟩ 0->0 ⟨0⟩a|a, ⟨0⟩b|b 1 1 0->1 ⟨1⟩[ε|a-a|εb|ε], ⟨2⟩a|b, ⟨2⟩b|a 1->F1 ⟨0⟩ 1->1 ⟨1⟩[ε|a-a|εb|ε], ⟨2⟩a|b, ⟨2⟩b|a
In [17]:
a.evaluate("aaa|ab")
Out[17]:
$3$
In [18]:
a.evaluate("aba|ab")
Out[18]:
$1$