From b4469a78dd923dd49e8cdf71d610310a9dd282cc Mon Sep 17 00:00:00 2001 From: SamyaDaleh Date: Tue, 27 Jun 2023 13:26:32 +0200 Subject: [PATCH] PcfgCykComplete takes the job of CfgCykComplete --- .../chartparsing/cfg/cyk/CfgCykComplete.java | 54 ------------ .../chartparsing/cfg/cyk/PcfgCykComplete.java | 87 +++++++++---------- .../cfg/cyk/astar/PcfgAstarComplete.java | 11 +-- .../converter/cfg/CfgToCykRulesConverter.java | 6 +- .../AbstractDynamicDeductionRule.java | 5 +- 5 files changed, 53 insertions(+), 110 deletions(-) delete mode 100644 src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/CfgCykComplete.java diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/CfgCykComplete.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/CfgCykComplete.java deleted file mode 100644 index 258b8cc6..00000000 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/CfgCykComplete.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.github.samyadaleh.cltoolbox.chartparsing.cfg.cyk; - -import com.github.samyadaleh.cltoolbox.chartparsing.dynamicdeductionrule.AbstractDynamicDecutionRuleTwoAntecedences; -import com.github.samyadaleh.cltoolbox.chartparsing.item.ChartItemInterface; -import com.github.samyadaleh.cltoolbox.chartparsing.item.DeductionChartItem; -import com.github.samyadaleh.cltoolbox.common.cfg.CfgProductionRule; -import com.github.samyadaleh.cltoolbox.common.tag.Tree; - -import java.util.List; - -import static com.github.samyadaleh.cltoolbox.common.Constants.DEDUCTION_RULE_CFG_CYK_COMPLETE; - -/** - * If two items match the rhs of a rule, get a new item that represents the lhs. - */ -public class CfgCykComplete extends AbstractDynamicDecutionRuleTwoAntecedences { - - private final CfgProductionRule rule; - - public CfgCykComplete(CfgProductionRule rule) { - this.rule = rule; - this.name = DEDUCTION_RULE_CFG_CYK_COMPLETE + " " + rule.toString(); - this.antNeeded = 2; - } - - protected void calculateConsequences(String[] itemForm1, String[] itemForm2) { - String nt1 = itemForm1[0]; - String i1 = itemForm1[1]; - int i1int = Integer.parseInt(i1); - String j1 = itemForm1[2]; - int j1int = Integer.parseInt(j1); - String nt2 = itemForm2[0]; - String i2 = itemForm2[1]; - int i2int = Integer.parseInt(i2); - String j2 = itemForm2[2]; - int j2int = Integer.parseInt(j2); - if (nt1.equals(rule.getRhs()[0]) && nt2.equals(rule.getRhs()[1]) - && i1int + j1int == i2int) { - ChartItemInterface consequence = - new DeductionChartItem(rule.getLhs(), String.valueOf(i1int), - String.valueOf(j1int + j2int)); - List derivedTrees = - CfgCykUtils.generateDerivedTrees(i1, antecedences, rule); - consequence.setTrees(derivedTrees); - logItemGeneration(consequence); - consequences.add(consequence); - } - } - - @Override public String toString() { - return "[" + rule.getRhs()[0] + ",i,l1], [" + rule.getRhs()[1] + ",i+l1,l2]" - + "\n______ \n" + "[" + rule.getLhs() + ",i,l1+l2]"; - } -} diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/PcfgCykComplete.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/PcfgCykComplete.java index c7f072a4..05832fb7 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/PcfgCykComplete.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/PcfgCykComplete.java @@ -2,6 +2,7 @@ import com.github.samyadaleh.cltoolbox.chartparsing.dynamicdeductionrule.AbstractDynamicDecutionRuleTwoAntecedences; import com.github.samyadaleh.cltoolbox.chartparsing.item.ChartItemInterface; +import com.github.samyadaleh.cltoolbox.chartparsing.item.DeductionChartItem; import com.github.samyadaleh.cltoolbox.chartparsing.item.ProbabilisticChartItemInterface; import com.github.samyadaleh.cltoolbox.common.cfg.CfgProductionRule; import com.github.samyadaleh.cltoolbox.common.cfg.PcfgProductionRule; @@ -9,67 +10,71 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.util.ArrayList; import java.util.List; import static com.github.samyadaleh.cltoolbox.common.Constants.DEDUCTION_RULE_PCFG_CYK_COMPLETE; /** - * Similar to the complete rule for CYK, but used for a PCFG and with weights - * for probabilistic CYK parsing. + * If two items match the rhs of a rule, get a new item that represents the lhs. + * Decides based on the rule it is instantiated with if it behaves weighted. */ public class PcfgCykComplete extends AbstractDynamicDecutionRuleTwoAntecedences { - protected final PcfgProductionRule pRule; + protected final CfgProductionRule pRule; private static final Logger log = LogManager.getLogger(); - public PcfgCykComplete(PcfgProductionRule pRule) { + public PcfgCykComplete(CfgProductionRule pRule) { this.pRule = pRule; this.name = DEDUCTION_RULE_PCFG_CYK_COMPLETE + " " + pRule.toString(); this.antNeeded = 2; } - @Override public List getAntecedences() { - return new ArrayList<>(this.antecedences); - } - - @Override public void setAntecedences(List antecedences) { - List inAntecedences = new ArrayList<>(antecedences); - this.antecedences = inAntecedences; - } - protected void calculateConsequences(String[] itemForm1, String[] itemForm2) { String nt1 = itemForm1[0]; String i1 = itemForm1[1]; int i1Int = Integer.parseInt(i1); String j1 = itemForm1[2]; int j1Int = Integer.parseInt(j1); - Double x1 = ((ProbabilisticChartItemInterface) antecedences.get(0)) - .getProbability(); String nt2 = itemForm2[0]; String i2 = itemForm2[1]; int i2Int = Integer.parseInt(i2); String j2 = itemForm2[2]; int j2Int = Integer.parseInt(j2); - Double x2 = ((ProbabilisticChartItemInterface) antecedences.get(1)) - .getProbability(); - if (nt1.equals(pRule.getRhs()[0]) && nt2.equals(pRule.getRhs()[1]) - && j1Int == i2Int) { - ProbabilisticChartItemInterface consequence = - new PcfgCykItem(x1 + x2 + -Math.log(pRule.getP()), pRule.getLhs(), - i1Int, j2Int); - addTreesToConsequence(i1, consequence); - logItemGeneration(consequence); - consequences.add(consequence); - } + + ChartItemInterface consequence; + if (pRule instanceof PcfgProductionRule) { + if (nt1.equals(pRule.getRhs()[0]) && nt2.equals(pRule.getRhs()[1]) + && j1Int == i2Int) { + Double x1 = ((ProbabilisticChartItemInterface) antecedences.get(0)) + .getProbability(); + Double x2 = ((ProbabilisticChartItemInterface) antecedences.get(1)) + .getProbability(); + consequence = + new PcfgCykItem(x1 + x2 + + -Math.log(((PcfgProductionRule) pRule).getP()), pRule.getLhs(), + i1Int, j2Int); + addTreesToConsequence(i1, consequence); + logItemGeneration(consequence); + consequences.add(consequence); + } + } else { + if (nt1.equals(pRule.getRhs()[0]) && nt2.equals(pRule.getRhs()[1]) + && i1Int + j1Int == i2Int) { + consequence = + new DeductionChartItem(pRule.getLhs(), String.valueOf(i1Int), + String.valueOf(j1Int + j2Int)); + addTreesToConsequence(i1, consequence); + logItemGeneration(consequence); + consequences.add(consequence); + } + } } - protected void addTreesToConsequence(String i1, - ProbabilisticChartItemInterface consequence) { + protected void addTreesToConsequence(String i1, ChartItemInterface consequence) { CfgProductionRule rule = new CfgProductionRule(pRule.getLhs(), pRule.getRhs()); List derivedTrees = @@ -77,23 +82,15 @@ protected void addTreesToConsequence(String i1, consequence.setTrees(derivedTrees); } - @Override public String getName() { - return this.name; - } - - @Override public int getAntecedencesNeeded() { - return this.antNeeded; - } - @Override public String toString() { - return "x1 : [" + pRule.getRhs()[0] + ", i, j], x2 : [" + pRule.getRhs()[1] - + ", j, k]" + "\n______ \n" + "x1 + x2 + |log(" + pRule.getP() - + ")| : [" + pRule.getLhs() + ", i, k]"; - } - - @Override public void clearItems() { - antecedences = new ArrayList<>(); - consequences = new ArrayList<>(); + if (pRule instanceof PcfgProductionRule) { + return "x1 : [" + pRule.getRhs()[0] + ", i, j], x2 : [" + pRule.getRhs()[1] + + ", j, k]" + "\n______ \n" + "x1 + x2 + |log(" + ((PcfgProductionRule) pRule).getP() + + ")| : [" + pRule.getLhs() + ", i, k]"; + } else { + return "[" + pRule.getRhs()[0] + ",i,l1], [" + pRule.getRhs()[1] + ",i+l1,l2]" + + "\n______ \n" + "[" + pRule.getLhs() + ",i,l1+l2]"; + } } protected void logItemGeneration(ChartItemInterface item) { diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/astar/PcfgAstarComplete.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/astar/PcfgAstarComplete.java index 40748e11..20c73433 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/astar/PcfgAstarComplete.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/cyk/astar/PcfgAstarComplete.java @@ -22,7 +22,7 @@ public PcfgAstarComplete(PcfgProductionRule pRule, super(pRule); this.n = n; this.outsides = outsides; - this.name = DEDUCTION_RULE_PCFG_CYK_COMPLETE + " " + pRule.toString(); + this.name = DEDUCTION_RULE_PCFG_CYK_COMPLETE + " " + pRule; } @Override @@ -39,7 +39,7 @@ protected void calculateConsequences(String[] itemForm1, String[] itemForm2) { if (!outsides.containsKey(outKey1)) { return; } - Double x1 = w1 - outsides + double x1 = w1 - outsides .get(SxCalc.getOutsideKey(nt1, i1Int, j1Int - i1Int, n - j1Int)); String nt2 = itemForm2[0]; @@ -54,7 +54,7 @@ protected void calculateConsequences(String[] itemForm1, String[] itemForm2) { if (!outsides.containsKey(outkey2)) { return; } - Double x2 = w2 - outsides + double x2 = w2 - outsides .get(SxCalc.getOutsideKey(nt2, i2Int, j2Int - i2Int, n - j2Int)); if (nt1.equals(pRule.getRhs()[0]) && nt2.equals(pRule.getRhs()[1]) @@ -66,7 +66,8 @@ protected void calculateConsequences(String[] itemForm1, String[] itemForm2) { } Double newOutP = outsides.get(outKey3); ProbabilisticChartItemInterface consequence = - new PcfgAstarItem(x1 + x2 + -Math.log(pRule.getP()), newOutP, + new PcfgAstarItem(x1 + x2 + + -Math.log(((PcfgProductionRule) pRule).getP()), newOutP, pRule.getLhs(), i1Int, j2Int); addTreesToConsequence(i1, consequence); logItemGeneration(consequence); @@ -78,7 +79,7 @@ protected void calculateConsequences(String[] itemForm1, String[] itemForm2) { return "x1 + out(" + pRule.getRhs()[0] + ", i, j - i, n - j) : [" + pRule .getRhs()[0] + ",i,j], x2 + out(" + pRule.getRhs()[1] + ", j, k - j, n - k) : [" + pRule.getRhs()[1] + ", j, k]" - + "\n______ \n" + "x1 + x2 + |log(" + pRule.getP() + + "\n______ \n" + "x1 + x2 + |log(" + ((PcfgProductionRule)pRule).getP() + ")| + out(" + pRule.getLhs() + ", i, k - i, n - k) : [" + pRule .getLhs() + ", i, k]"; } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/cfg/CfgToCykRulesConverter.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/cfg/CfgToCykRulesConverter.java index 6a545a69..2bcf53f5 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/cfg/CfgToCykRulesConverter.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/cfg/CfgToCykRulesConverter.java @@ -2,9 +2,9 @@ import com.github.samyadaleh.cltoolbox.chartparsing.ParsingSchema; import com.github.samyadaleh.cltoolbox.chartparsing.StaticDeductionRule; -import com.github.samyadaleh.cltoolbox.chartparsing.cfg.cyk.CfgCykComplete; import com.github.samyadaleh.cltoolbox.chartparsing.cfg.cyk.CfgCykCompleteGeneral; import com.github.samyadaleh.cltoolbox.chartparsing.cfg.cyk.CfgCykCompleteUnary; +import com.github.samyadaleh.cltoolbox.chartparsing.cfg.cyk.PcfgCykComplete; import com.github.samyadaleh.cltoolbox.chartparsing.dynamicdeductionrule.DynamicDeductionRuleInterface; import com.github.samyadaleh.cltoolbox.chartparsing.item.ChartItemInterface; import com.github.samyadaleh.cltoolbox.chartparsing.item.DeductionChartItem; @@ -38,7 +38,7 @@ public static ParsingSchema cfgToCykRules(Cfg cfg, String w) if (rule.getRhs().length == 1) { addCykScanRules(wSplit, schema, rule); } else { - DynamicDeductionRuleInterface complete = new CfgCykComplete(rule); + DynamicDeductionRuleInterface complete = new PcfgCykComplete(rule); schema.addRule(complete); } } @@ -70,7 +70,7 @@ public static ParsingSchema cfgToCykExtendedRules(Cfg cfg, String w) schema.addRule(complete); } } else { - DynamicDeductionRuleInterface complete = new CfgCykComplete(rule); + DynamicDeductionRuleInterface complete = new PcfgCykComplete(rule); schema.addRule(complete); } } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/dynamicdeductionrule/AbstractDynamicDeductionRule.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/dynamicdeductionrule/AbstractDynamicDeductionRule.java index 768f04b7..f86c0e3b 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/dynamicdeductionrule/AbstractDynamicDeductionRule.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/dynamicdeductionrule/AbstractDynamicDeductionRule.java @@ -20,13 +20,12 @@ public abstract class AbstractDynamicDeductionRule protected int antNeeded; @Override public List getAntecedences() { - return this.antecedences; + return new ArrayList<>(this.antecedences); } @Override public void setAntecedences(List antecedences) { - this.antecedences = antecedences; + this.antecedences = new ArrayList<>(antecedences); } - @Override public String getName() { return this.name; }