def _make_transducer(self): transducer, segments, state = super( DepConstraint, self)._base_faithfulness_transducer() for segment in segments: transducer.add_arc( Arc(state, segment, segment, CostVector.get_vector(1, 0), state)) transducer.add_arc( Arc(state, segment, NULL_SEGMENT, CostVector.get_vector(1, 0), state)) if segment.has_feature_bundle(self.feature_bundle): transducer.add_arc( Arc(state, NULL_SEGMENT, segment, CostVector.get_vector(1, 1), state)) else: transducer.add_arc( Arc(state, NULL_SEGMENT, segment, CostVector.get_vector(1, 0), state)) if get_configuration("ALLOW_CANDIDATES_WITH_CHANGED_SEGMENTS"): for first_segment, second_segment in permutations(segments, 2): transducer.add_arc( Arc(state, first_segment, second_segment, CostVector.get_vector(1, 0), state)) return transducer
def _make_transducer(self): segments = self.feature_table.get_segments() transducer = Transducer(segments, length_of_cost_vectors=0) word_segments = self.get_segments() n = len(self.word_string) states = [State("q{}".format(i), i) for i in range(n+1)] for i, state in enumerate(states): transducer.add_state(state) transducer.add_arc(Arc(state, NULL_SEGMENT, JOKER_SEGMENT, CostVector.get_empty_vector(), state)) if i != n: transducer.add_arc(Arc(states[i], word_segments[i], JOKER_SEGMENT, CostVector.get_empty_vector(), states[i+1])) transducer.initial_state = states[0] transducer.add_final_state(states[n]) return transducer
def make_optimal_paths(transducer_input): transducer = pickle.loads(pickle.dumps(transducer_input, -1)) alphabet = transducer.get_alphabet() new_arcs = list() for segment in alphabet: word = Word(segment.get_symbol()) word_transducer = word.get_transducer() #print(word_transducer.dot_representation()) intersected_machine = Transducer.intersection(word_transducer, transducer) states = transducer.get_states() for state1, state2 in itertools.product(states, states): initial_state = word_transducer.initial_state & state1 final_state = word_transducer.get_a_final_state() & state2 temp_transducer = pickle.loads(pickle.dumps(intersected_machine, -1)) temp_transducer.initial_state = initial_state temp_transducer.set_final_state(final_state) temp_transducer.clear_dead_states() if final_state in temp_transducer.get_final_states(): # otherwise no path. try: temp_transducer = remove_suboptimal_paths(temp_transducer) range = temp_transducer.get_range() arc = Arc(state1, segment, range, _get_path_cost(temp_transducer), state2) new_arcs.append(arc) except KeyError: pass #print("****") #print(temp_transducer.dot_representation()) transducer.set_arcs(new_arcs) return transducer
def _make_transducer(self): transducer, segments, state = super( IdentConstraint, self)._base_faithfulness_transducer() for segment in segments: transducer.add_arc( Arc(state, segment, segment, CostVector.get_vector(1, 0), state)) transducer.add_arc( Arc(state, segment, NULL_SEGMENT, CostVector.get_vector(1, 0), state)) transducer.add_arc( Arc(state, NULL_SEGMENT, segment, CostVector.get_vector(1, 0), state)) input_segment = segment if input_segment.has_feature_bundle(self.feature_bundle): for output_segment in segments: if output_segment.has_feature_bundle(self.feature_bundle): transducer.add_arc( Arc(state, input_segment, output_segment, CostVector.get_vector(1, 0), state)) else: transducer.add_arc( Arc(state, input_segment, output_segment, CostVector.get_vector(1, 1), state)) else: for output_segment in segments: transducer.add_arc( Arc(state, input_segment, output_segment, CostVector.get_vector(1, 0), state)) return transducer
def _make_transducer(self): def compute_num_of_max_satisfied_bundle(segment): i = 0 while i < n and symbol_bundle_characteristic_matrix[segment][i]: i += 1 return i def compute_highest_num_of_satisfied_bundle(segment, j): for k in range(j + 1, 0, -1): if symbol_bundle_characteristic_matrix[segment][k - 1]: return k else: return 0 n = len(self.feature_bundles) - 1 segments = self.feature_table.get_segments() transducer = Transducer(segments, name=str(self)) symbol_bundle_characteristic_matrix = { segment: [ segment.has_feature_bundle(self.feature_bundles[i]) for i in range(n + 1) ] for segment in segments } states = {i: {j: 0 for j in range(i)} for i in range(n + 1)} initial_state = State( 'q0|0' ) # here we use a tuple as label. it will change at the end of this function states[0][0] = initial_state transducer.set_as_single_state(initial_state) if not n: for segment in segments: transducer.add_arc( Arc( states[0][0], JOKER_SEGMENT, segment, CostVector([ int(symbol_bundle_characteristic_matrix[segment] [0]) ]), states[0][0])) transducer.add_arc( Arc(states[0][0], JOKER_SEGMENT, NULL_SEGMENT, CostVector([0]), states[0][0])) else: for i in range(0, n + 1): for j in range(i): state = State('q{0}|{1}'.format(i, j)) states[i][j] = state transducer.add_state(state) max_num_of_satisfied_bundle_by_segment = { segment: compute_num_of_max_satisfied_bundle(segment) for segment in segments } for segment in segments: transducer.add_arc( Arc( states[0][0], JOKER_SEGMENT, segment, CostVector([0]), states[symbol_bundle_characteristic_matrix[segment] [0]][0])) for i in range(n + 1): for j in range(i): state = states[i][j] transducer.add_final_state(state) if i != n: for segment in segments: if symbol_bundle_characteristic_matrix[segment][i]: new_state_level = i + 1 new_state_mem = min([ j + 1, max_num_of_satisfied_bundle_by_segment[ segment] ]) else: new_state_level = compute_highest_num_of_satisfied_bundle( segment, j) new_state_mem = min([ max_num_of_satisfied_bundle_by_segment[ segment], abs(new_state_level - 1) ]) new_terminus = states[new_state_level][ new_state_mem] transducer.add_arc( Arc(state, JOKER_SEGMENT, segment, CostVector([0]), new_terminus)) transducer.add_arc( Arc(new_terminus, JOKER_SEGMENT, segment, CostVector([0]), new_terminus)) else: # i = n for segment in segments: new_state_level = compute_highest_num_of_satisfied_bundle( segment, j) new_state_mem = min([ max_num_of_satisfied_bundle_by_segment[ segment], abs(new_state_level - 1) ]) new_terminus = states[new_state_level][ new_state_mem] transducer.add_arc( Arc( state, JOKER_SEGMENT, segment, CostVector([ int(symbol_bundle_characteristic_matrix[ segment][i]) ]), new_terminus)) transducer.clear_dead_states() for state in transducer.states: transducer.add_arc( Arc(state, JOKER_SEGMENT, NULL_SEGMENT, CostVector([0]), state)) return transducer