Exemple #1
0
        public UnaryRule Clone()
        {
            var clone = new UnaryRule ();

            clone.ptag = ptag;
            clone.ctag = ctag;
            clone.scores = ArrayHelper.Clone (scores);

            return clone;
        }
Exemple #2
0
        public UnaryRule SplitSymbols(int[] subtagCount, int[] newSubtagCount, Random RNG, double randomness)
        {
            int parentSplitFactor = subtagCount [ptag] == newSubtagCount [ptag] ? 1 : 2;
            int childSplitFactor = subtagCount [ctag] == newSubtagCount [ctag] ? 1 : 2;
            double[][] oldScores = scores;
            double[][] newScores = new double[newSubtagCount [ctag]][];

            for (int csubt = 0; csubt < oldScores.Length; ++csubt) {
                if (oldScores [csubt] == null) {
                    continue;
                }

                for (int offset = 0; offset < childSplitFactor; ++offset) {
                    newScores [childSplitFactor * csubt + offset] = new double[newSubtagCount [ptag]];
                }

                for (int psubt = 0; psubt < subtagCount[ptag]; ++psubt) {
                    double score = oldScores [csubt] [psubt];

                    for (int p = 0; p < parentSplitFactor; ++p) {
                        double div = (double)Math.Log (childSplitFactor);
                        double rd = (double)(randomness / 100 * (RNG.NextDouble () - 0.5));

                        int newPsubt = psubt * parentSplitFactor + p;

                        if (childSplitFactor == 1) {
                            rd = 0;
                        }

                        for (int c = 0; c < childSplitFactor; ++c) {
                            rd *= -1.0f;
                            int newCsubt = csubt * childSplitFactor + c;
                            newScores [newCsubt] [newPsubt] = score - div + (double)Math.Log (1.0f + rd);
                        }

                    }
                }
            }

            var newRule = new UnaryRule (newScores, ptag, ctag);

            return newRule;
        }
Exemple #3
0
        public UnaryRule CreateRule(int[] newSubtagCount)
        {
            var rule = new UnaryRule ();

            rule.scores = ArrayHelper.AllocateArray<double> (newSubtagCount [ctag], newSubtagCount [ptag]);

            ArrayHelper.Fill (rule.scores, double.NegativeInfinity);

            rule.ptag = ptag;
            rule.ctag = ctag;

            return rule;
        }
Exemple #4
0
        public UnaryRule MergeSymbols(int[][] trace, double[][] symbolProbs, double[][] projectedSymbolProbs)
        {
            int ptagcount = trace [ptag] [trace [ptag].Length - 1] + 1;
            int ctagcount = trace [ctag] [trace [ctag].Length - 1] + 1;

            var newScores = ArrayHelper.AllocateArray<double> (ctagcount, ptagcount);

            ArrayHelper.Fill<double> (newScores, double.NegativeInfinity);

            for (int c = 0; c < scores.Length; ++c) {
                if (scores [c] == null) {
                    continue;
                }

                int pc = trace [ctag] [c];

                for (int p = 0; p < scores[c].Length; ++p) {
                    int pp = trace [ptag] [p];

                    newScores [pc] [pp] = MathHelper.LogAdd (newScores [pc] [pp], scores [c] [p] + symbolProbs [ptag] [p]);
                }
            }

            for (int pc = 0; pc < newScores.Length; ++pc) {
                for (int pp = 0; pp < newScores[pc].Length; ++pp) {
                    if (!double.IsNegativeInfinity (newScores [pc] [pp])
                        && !double.IsNegativeInfinity (projectedSymbolProbs [ptag] [pp])) {
                        newScores [pc] [pp] -= projectedSymbolProbs [ptag] [pp];
                    }
                }
            }

            var rule = new UnaryRule ();
            rule.ptag = ptag;
            rule.ctag = ctag;
            rule.scores = newScores;

            return rule;
        }
Exemple #5
0
        public void CopyTo(UnaryRule rule)
        {
            rule.ctag = ctag;
            rule.ptag = ptag;

            ArrayHelper.CopyTo (scores, rule.scores);
        }
Exemple #6
0
        public LAPCFGrammar(TagSet set, double[][][] brawScores, double[][] urawScores, double[][] trawScores)
        {
            NTCount = set.NTCount;
            PTCount = set.PTCount;
            ROOTID = set.ROOTID;

            var tagCount = TotalTagCount;

            brules = new BinaryRule[tagCount][][];

            for (int l = 0; l < tagCount; ++l)
            {
                if (brawScores [l] == null)
                {
                    continue;
                }

                brules [l] = new BinaryRule[tagCount][];

                for (int r = 0; r < tagCount; ++r)
                {
                    if (brawScores [l] [r] == null)
                    {
                        continue;
                    }

                    brules [l] [r] = new BinaryRule[tagCount];
                    for (int p = 0; p < tagCount; ++p)
                    {
                        if (!double.IsInfinity(brawScores [l] [r] [p]) && !double.IsNaN(brawScores [l] [r] [p]))
                        {
                            double[][][] s = new double[1][][];
                            s [0] = new double[1][];
                            s [0] [0] = new double[1];
                            s [0] [0] [0] = brawScores [l] [r] [p];
                            brules [l] [r] [p] = new BinaryRule(s, p, l, r);
                        }
                    }
                }
            }

            urules = new UnaryRule[tagCount][];

            for (int c = 0; c < tagCount; ++c)
            {
                if (urawScores [c] == null)
                {
                    continue;
                }

                urules [c] = new UnaryRule[tagCount];

                for (int p = 0; p < tagCount; ++p)
                {
                    if (!double.IsNaN(urawScores [c] [p]) && !double.IsInfinity(urawScores [c] [p]))
                    {
                        double[][] s = new double[1][];
                        s [0] = new double[1];
                        s [0] [0] = urawScores [c] [p];
                        urules [c] [p] = new UnaryRule(s, p, c);
                    }
                }
            }

            trules = new TerminalRule[trawScores.Length][];

            for (int w = 0; w < trawScores.Length; ++w)
            {
                if (trawScores [w] == null)
                {
                    continue;
                }

                trules [w] = new TerminalRule[trawScores [w].Length];

                for (int t = 0; t < trules[w].Length; ++t)
                {
                    if (!double.IsNaN(trawScores [w] [t]) && !double.IsInfinity(trawScores [w] [t]))
                    {
                        double[] s = new double[1];
                        s [0] = trawScores [w] [t];
                        trules [w] [t] = new TerminalRule(s, t, w);
                    }
                }
            }

            subTagCounts = new int[tagCount];
            for (int i = 0; i < subTagCounts.Length; ++i)
            {
                subTagCounts [i] = 1;
            }
        }
Exemple #7
0
        public static UnaryRule[][] CloneRules(UnaryRule[][] rules)
        {
            if (rules == null)
            {
                return null;
            }

            return rules.Select(
                x => x == null ? null : x.Select(
                    y => y == null ? null : y.Clone()
            ).ToArray()
            ).ToArray();
        }
Exemple #8
0
        private void BuildUnaryRule(string ruleString, TagSet tagSet)
        {
            string[] parts = ruleString.Split(new string[] { "\t", "_" }, StringSplitOptions.RemoveEmptyEntries);

            int ptag = tagSet.GetID(parts [0]);
            int subptag = int.Parse(parts [1]);
            int ctag = tagSet.GetID(parts [2]);
            int subctag = int.Parse(parts [3]);
            double s = double.Parse(parts [4]);

            if (urules == null)
            {
                urules = new UnaryRule[TotalTagCount][];
            }

            if (urules [ctag] == null)
            {
                urules [ctag] = new UnaryRule[TotalTagCount];
            }

            if (urules [ctag] [ptag] == null)
            {
                urules [ctag] [ptag] = new UnaryRule(ArrayHelper.AllocateArray<double>(subTagCounts [ctag], subTagCounts [ptag]), ptag, ctag);
                urules [ctag] [ptag].ClearScore();
            }

            urules [ctag] [ptag].scores [subctag] [subptag] = s;
        }
Exemple #9
0
        public void PropMaxUnaryPath()
        {
            UnaryRule[][] xrules = ArrayHelper.AllocateArray<UnaryRule>(TotalTagCount, TotalTagCount);

            for (int c = 0; c < xrules.Length; ++c)
            {
                for (int p = 0; p < xrules[c].Length; ++p)
                {
                    double[][] score = ArrayHelper.AllocateArray<double>(subTagCounts[c], subTagCounts[p]);
                    ArrayHelper.Fill(score, double.NegativeInfinity);
                    xrules[c][p] = new UnaryRule(score, p, c);
                }
            }

            CopyRules(urules, xrules);

            var prules = CloneRules(xrules);

            for (int tag = 0; tag < TotalTagCount; ++tag)
            {
                for (int subtag = 0; subtag < subTagCounts[tag]; ++subtag)
                {
                    for (int ctag = 0; ctag < TotalTagCount; ++ctag)
                    {
                        for (int ptag = 0; ptag < TotalTagCount; ++ptag)
                        {
                            var rule = xrules[ctag][ptag];
                            var prule = prules[tag][ptag];
                            var crule = prules[ctag][tag];

                            for (int subctag = 0; subctag < subTagCounts[ctag]; ++subctag)
                            {
                                for (int subptag = 0; subptag < subTagCounts[ptag]; ++subptag)
                                {
                                    double pscore = prule.scores[subtag][subptag];
                                    double cscore = crule.scores[subctag][subtag];

                                    if (double.IsNegativeInfinity(pscore) || double.IsNegativeInfinity(cscore))
                                    {
                                        continue;
                                    }

                                    rule.scores[subctag][subptag] = Math.Max(rule.scores[subctag][subptag], pscore + cscore);
                                }
                            }
                        }
                    }

                    CopyRules(xrules, prules);
                }
            }

            for (int c = 0; c < TotalTagCount; ++c)
            {
                int survive = 0;
                for (int p = 0; p < TotalTagCount; ++p)
                {
                    xrules[c][p].ComputeMaxScore();

                    if (double.IsNegativeInfinity(xrules[c][p].maxScore))
                    {
                        xrules[c][p] = null;
                    }
                    else
                    {
                        survive += 1;
                    }
                }

                if (survive == 0)
                {
                    xrules[c] = null;
                }
            }

            urules = xrules;
        }