Esempio n. 1
0
    def java_parse_sentence(self, sequence: str):
        json_grammar = self.grammar.save_to_json()
        gateway = JavaGateway()
        cyk = gateway.entry_point
        cyk_result = cyk.runCyk(sequence, json_grammar)

        result = json.loads(cyk_result)
        rt = result['rules_table']
        pt = result['probability_table']

        for i in range(len(sequence)):
            for j in range(len(sequence) - i):
                rules = []
                pts = []
                for rule in rt[i][j]['rules']:
                    rules.append(sCellRule.fromCellRuleDict(rule))
                rt[i][j] = rules
                for p in pt[i][j]:
                    if p['item_1'] != -1 and p['item_2'] != -1:
                        pts.append(
                            ProbabilityArrayCell(p['item_1'], p['item_2']))
                    else:
                        pts.append(self.default_value)
                pt[i][j] = pts
        self.rules_table = rt
        self.probability_array = pt

        if self.learning_on:
            rules_to_add = result['rules_to_add']
            for cell_indexes in rules_to_add:
                self.__apply_aggressive_and_final_covering(
                    cell_indexes['i'], cell_indexes['j'])
Esempio n. 2
0
def __init_cell(cyk_values: CykValues, index: int, rule: sRule):
    rule_left_index = rule.left.index
    cyk_values.probability_array[0][index][rule_left_index] = ProbabilityArrayCell()
    if cyk_values.mode == "Viterbi":
        cyk_values.probability_array[0][index][rule_left_index].item_1 = math.log10(rule.probability)
    else:
        cyk_values.probability_array[0][index][rule_left_index].item_1 = rule.probability
        cyk_values.probability_array[0][index][rule_left_index].item_2 = rule.probability
Esempio n. 3
0
def __init_cell(cyk_values: CykValues, index: int, rule: sRule):
    rule_left_index = rule.left.index
    cyk_values.probability_array[0][index][
        rule_left_index] = ProbabilityArrayCell()
    cyk_values.probability_array[0][index][
        rule_left_index].item_1 = rule.probability
    cyk_values.probability_array[0][index][
        rule_left_index].item_2 = rule.probability
Esempio n. 4
0
def __calculate_min_prob_rule_cell_probability(default_value, cell_probability: ProbabilityArrayCell,
                                               parent_cell_1_probability: ProbabilityArrayCell,
                                               parent_cell_2_probability: ProbabilityArrayCell,
                                               rule: sRule):
    if cell_probability == default_value:
        cell_probability = ProbabilityArrayCell()
        cell_probability.item_1 = min(min(rule.probability * parent_cell_1_probability.item_1),
                                      parent_cell_2_probability.item_1)

        cell_probability.item_2 = rule.probability * \
                                  parent_cell_1_probability.item_2 * \
                                  parent_cell_2_probability.item_2
    else:
        cell_probability.item_1 = max(cell_probability.item_1,
                                      min(min(rule.probability), parent_cell_1_probability.item_1,
                                          parent_cell_2_probability.item_1))
        cell_probability.item_2 = cell_probability.item_2 + rule.probability * \
                                                            parent_cell_1_probability.item_2 * \
                                                            parent_cell_2_probability.item_2
    return cell_probability
Esempio n. 5
0
def __calculate_viterbi_rule_cell_probability(default_value, cell_probability: ProbabilityArrayCell,
                                              parent_cell_1_probability: ProbabilityArrayCell,
                                              parent_cell_2_probability: ProbabilityArrayCell,
                                              rule: sRule):
    if cell_probability == default_value:
        cell_probability = ProbabilityArrayCell()
        cell_probability.item_1 = math.log10(rule.probability) + \
                                  parent_cell_1_probability.item_1 * parent_cell_2_probability.item_1

        cell_probability.item_2 = (rule.probability *
                                   parent_cell_1_probability.item_2 *
                                   parent_cell_2_probability.item_2)

    else:
        cell_probability.item_1 = max(cell_probability.item_1, math.log10(parent_cell_1_probability.item_1 +
                                                                          parent_cell_2_probability.item_1))

        cell_probability.item_2 = cell_probability.item_2 + (rule.probability *
                                                             parent_cell_1_probability.item_2 *
                                                             parent_cell_2_probability.item_2)
    return cell_probability
Esempio n. 6
0
 def __init_cell(self, index: int, rule: sRule):
     rule_left_index = rule.left.index
     self.probability_array[0][index][
         rule_left_index] = ProbabilityArrayCell()
     if self.mode == "Viterbi":
         self.probability_array[0][index][
             rule_left_index].item_1 = math.log10(rule.probability)
     else:
         self.probability_array[0][index][
             rule_left_index].item_1 = rule.probability
     self.probability_array[0][index][
         rule_left_index].item_2 = rule.probability
Esempio n. 7
0
def __calculate_baum_welch_rule_cell_probability(default_value, cell_probability: ProbabilityArrayCell,
                                                 parent_cell_1_probability: ProbabilityArrayCell,
                                                 parent_cell_2_probability: ProbabilityArrayCell,
                                                 rule: sRule) -> ProbabilityArrayCell:
    if cell_probability == default_value:
        cell_probability = ProbabilityArrayCell()
        cell_probability.item_1 = rule.probability * \
                                  parent_cell_1_probability.item_1 * \
                                  parent_cell_2_probability.item_1
        cell_probability.item_2 = rule.probability * \
                                  parent_cell_1_probability.item_2 * \
                                  parent_cell_2_probability.item_2
    else:
        cell_probability.item_1 = cell_probability.item_1 + \
                                  (rule.probability *
                                   parent_cell_1_probability.item_1 *
                                   parent_cell_2_probability.item_1)
        cell_probability.item_2 = cell_probability.item_2 + \
                                  (rule.probability *
                                   parent_cell_1_probability.item_2 *
                                   parent_cell_2_probability.item_2)
    return cell_probability