Implement a Lattice structure and its associated methods to manage nodes corresponding to segments of a sentence, enabling operations like insertion and the Viterbi algorithm for determining the most likely sequence of nodes. The implementation should fulfill the following requirements:
Build a Lattice Structure:
Implement the Lattice struct with fields to store:
The input sentence as a string.
The length of the sentence.
A vector of node references, where each node corresponds to a segment of the sentence.
A vector to keep track of beginning nodes at each position in the sentence.
A vector to keep track of ending nodes at each position.
IDs for the beginning-of-sentence and end-of-sentence nodes.
Insert Method:
Implement the insert method that:
Takes parameters pos (position in the sentence), length (length of the node), score (score assigned to this node), and id (unique identifier for the node).
Creates a new node, links it to the appropriate position in begin_nodes and end_nodes, and appends it to the nodes vector.
Viterbi Algorithm:
Implement the viterbi method that determines the best path through the lattice based on the scores of the nodes, returning a vector of node references that represent the most likely sequence of segments.
Tokenization:
Implement the method tokens, which utilizes the viterbi method to return a Vec<String> containing the segments of the input sentence identified by the Viterbi algorithm.
Testing:
Ensure that the implemented functionality passes the provided unit tests located in the tests module.
The solution should be placed in the /app/ directory. Follow Rust conventions for struct implementations, method definitions, and testing formats. Ensure code correctness and efficiency in the implementation of the Viterbi algorithm and node management.