Skip to content

Commit

Permalink
PcfgCykComplete takes the job of CfgCykComplete
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyaDaleh committed Jun 27, 2023
1 parent 7fb43f0 commit b4469a7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 110 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,95 @@

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;
import com.github.samyadaleh.cltoolbox.common.tag.Tree;
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<ChartItemInterface> getAntecedences() {
return new ArrayList<>(this.antecedences);
}

@Override public void setAntecedences(List<ChartItemInterface> antecedences) {
List<ChartItemInterface> 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<Tree> derivedTrees =
CfgCykUtils.generateDerivedTrees(i1, this.getAntecedences(), rule);
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
Expand All @@ -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])
Expand All @@ -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);
Expand All @@ -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]";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ public abstract class AbstractDynamicDeductionRule
protected int antNeeded;

@Override public List<ChartItemInterface> getAntecedences() {
return this.antecedences;
return new ArrayList<>(this.antecedences);
}

@Override public void setAntecedences(List<ChartItemInterface> antecedences) {
this.antecedences = antecedences;
this.antecedences = new ArrayList<>(antecedences);
}

@Override public String getName() {
return this.name;
}
Expand Down

0 comments on commit b4469a7

Please sign in to comment.