import { State , getActiveCategory , advanceDot , isPassive , isCompleted } from "./chart/state" ; import { Chart } from "./chart/chart" ; import { Grammar } from "../grammar/grammar" ; import { NonTerminal , Category } from "../grammar/category" ; import { Rule , isUnitProduction } from "../grammar/rule" ; import { Expression } from "semiring" ; import { DeferredStateScoreComputations } from "./chart/addable-expressions-container" ; import { AtomicValue } from "semiring" ; import { DeferredValue } from "./expression/value" ; param position param states param addForwardScores param addInnerScores param grammar param stateSets function completeNoViterbi < S , T > ( position , states , addForwardScores , addInnerScores , grammar , stateSets ) { let definitelyNewStates ; states . forEach ( ( completedState ) => { const j = completedState . ruleStartPosition ; const Y = completedState . rule . left ; const probM = grammar . probabilityMapping ; const innerScore = stateSets . getInnerScore ( completedState ) ; const unresolvedCompletedInner = addInnerScores . getOrCreateByState ( completedState , new AtomicValue ( innerScore ) ) ; stateSets . getStatesActiveOnNonTerminalWithNonZeroUnitStarScoreToY ( j , Y ) . forEach ( ( stateToAdvance ) => { if ( j !== stateToAdvance . position ) throw new Error ( "Index failed. This is a bug." ) ; const innerScore2 = stateSets . getInnerScore ( stateToAdvance ) ; const prevInner = addInnerScores . getOrCreateByState ( stateToAdvance , new AtomicValue ( innerScore2 ) ) ; const forwardScore = stateSets . getForwardScore ( stateToAdvance ) ; const prevForward = addForwardScores . getOrCreateByState ( stateToAdvance , new AtomicValue ( forwardScore ) ) ; const Z = getActiveCategory ( stateToAdvance ) ; const unitStarScore = new AtomicValue ( probM . fromProbability ( grammar . getUnitStarScore ( Z , Y ) ) ) ; const sr = grammar . deferrableSemiring ; const fw = sr . times ( unitStarScore , sr . times ( prevForward , unresolvedCompletedInner ) ) ; const inner = sr . times ( unitStarScore , sr . times ( prevInner , unresolvedCompletedInner ) ) ; const newStateRule = stateToAdvance . rule ; const newStateDotPosition = advanceDot ( stateToAdvance ) ; const newStateRuleStart = stateToAdvance . ruleStartPosition ; addForwardScores . plus ( newStateRule , position , newStateRuleStart , newStateDotPosition , fw ) ; if ( isPassive ( newStateRule , newStateDotPosition ) && ! isUnitProduction ( newStateRule ) && ! stateSets . has ( newStateRule , position , newStateRuleStart , newStateDotPosition ) ) { if ( ! definitelyNewStates ) definitelyNewStates = new DeferredStateScoreComputations < S , T > ( sr ) ; definitelyNewStates . plus ( newStateRule , position , newStateRuleStart , newStateDotPosition , fw ) ; } addInnerScores . plus ( newStateRule , position , newStateRuleStart , newStateDotPosition , inner ) ; } ) ; } ) ; if ( ! ! definitelyNewStates ) { const newCompletedStates = new Set < State < S , T > > ( ) ; definitelyNewStates . forEach ( ( index , ruleStart , dot , rule , ignored ) => { if ( stateSets . has ( rule , index , ruleStart , dot ) ) { throw new Error ( "State wasn't new" ) ; } const state = stateSets . getOrCreate ( index , ruleStart , dot , rule ) ; if ( ! isCompleted ( state ) || isUnitProduction ( state . rule ) ) throw new Error ( "Unexpected chart found in possible new states. This is a bug." ) ; newCompletedStates . add ( state ) ; } ) ; if ( ! ! newCompletedStates && newCompletedStates . size > 0 ) { completeNoViterbi ( position , newCompletedStates , addForwardScores , addInnerScores , grammar , stateSets ) ; } } } param i param stateSets param grammar export function complete < S , T > ( i , stateSets , grammar ) { const addForwardScores = new DeferredStateScoreComputations ( grammar . deferrableSemiring ) ; const addInnerScores = new DeferredStateScoreComputations ( grammar . deferrableSemiring ) ; const completeOnStates = stateSets . getCompletedStatesThatAreNotUnitProductions ( i ) ; if ( ! ! completeOnStates ) completeNoViterbi ( i , completeOnStates , addForwardScores , addInnerScores , grammar , stateSets ) ; addForwardScores . forEach ( ( position , ruleStart , dot , rule , score ) => { const state = stateSets . getOrCreate ( position , ruleStart , dot , rule ) ; stateSets . setForwardScore ( state , score . resolve ( ) ) ; } ) ; addInnerScores . forEach ( ( position , ruleStart , dot , rule , score ) => { const state = stateSets . getOrCreate ( position , ruleStart , dot , rule ) ; stateSets . setInnerScore ( state , score . resolve ( ) ) ; } ) ; }