Ejemplo n.º 1
0
        private static IEnumerable <TSource> Equal <TSource>(IIndex <int, TSource> source,
                                                             BinaryExpression binaryExpression)
        {
            if (source is Index <int, TSource> )
            {
                Index <int, TSource> index = (Index <int, TSource>)source;
                int max = index.Max();
                ConstantExpression constantExpression = null;
                if (binaryExpression.Right.NodeType == ExpressionType.Constant)
                {
                    constantExpression = (ConstantExpression)binaryExpression.Right;
                }

                if (source.Contains((int)constantExpression.Value))
                {
                    var item = source[(int)constantExpression.Value];
                    yield return(item);
                }
            }
            else if (source is EqualIndex <int, TSource> )
            {
                EqualIndex <int, TSource> index = (EqualIndex <int, TSource>)source;
                ConstantExpression        constantExpression = null;
                if (binaryExpression.Right.NodeType == ExpressionType.Constant)
                {
                    constantExpression = (ConstantExpression)binaryExpression.Right;
                }
                if (source.Contains((int)constantExpression.Value))
                {
                    var item = source[(int)constantExpression.Value];
                    yield return(item);
                }
            }
        }
        /// <summary>Destructively modifies the input and returns it as a convenience.</summary>
        public virtual Pair <UnaryGrammar, BinaryGrammar> Apply(Pair <UnaryGrammar, BinaryGrammar> bgug)
        {
            Alpha = trainOptions.ruleSmoothingAlpha;
            ICounter <string> symWeights = new ClassicCounter <string>();
            ICounter <string> symCounts  = new ClassicCounter <string>();

            //Tally unary rules
            foreach (UnaryRule rule in bgug.First())
            {
                if (!tagIndex.Contains(rule.parent))
                {
                    UpdateCounters(rule, symWeights, symCounts);
                }
            }
            //Tally binary rules
            foreach (BinaryRule rule_1 in bgug.Second())
            {
                UpdateCounters(rule_1, symWeights, symCounts);
            }
            //Compute smoothed rule scores, unary
            foreach (UnaryRule rule_2 in bgug.First())
            {
                if (!tagIndex.Contains(rule_2.parent))
                {
                    rule_2.score = SmoothRuleWeight(rule_2, symWeights, symCounts);
                }
            }
            //Compute smoothed rule scores, binary
            foreach (BinaryRule rule_3 in bgug.Second())
            {
                rule_3.score = SmoothRuleWeight(rule_3, symWeights, symCounts);
            }
            return(bgug);
        }
Ejemplo n.º 3
0
 public virtual void TestToArray()
 {
     string[] strs = new string[2];
     strs = Sharpen.Collections.ToArray(index.ObjectsList(), strs);
     NUnit.Framework.Assert.AreEqual(2, strs.Length);
     NUnit.Framework.Assert.IsTrue(index.Contains(strs[0]));
     NUnit.Framework.Assert.IsTrue(index.Contains(strs[1]));
 }
Ejemplo n.º 4
0
        public void IndexIsEmptyAfterClear()
        {
            _items.Clear();

            Assert.IsFalse(_itemsById.Contains(1));
            Assert.IsFalse(_itemsById.Contains(2));
            Assert.IsFalse(_itemsByName.Contains("one"));
            Assert.IsFalse(_itemsByName.Contains("two"));
        }
        /// <summary>Adds dependencies to list depList.</summary>
        /// <remarks>
        /// Adds dependencies to list depList.  These are in terms of the original
        /// tag set not the reduced (projected) tag set.
        /// </remarks>
        protected internal static MLEDependencyGrammar.EndHead TreeToDependencyHelper(Tree tree, IList <IntDependency> depList, int loc, IIndex <string> wordIndex, IIndex <string> tagIndex)
        {
            //       try {
            //  PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out,"GB18030"),true);
            //  tree.pennPrint(pw);
            //       }
            //       catch (UnsupportedEncodingException e) {}
            if (tree.IsLeaf() || tree.IsPreTerminal())
            {
                MLEDependencyGrammar.EndHead tempEndHead = new MLEDependencyGrammar.EndHead();
                tempEndHead.head = loc;
                tempEndHead.end  = loc + 1;
                return(tempEndHead);
            }
            Tree[] kids = tree.Children();
            if (kids.Length == 1)
            {
                return(TreeToDependencyHelper(kids[0], depList, loc, wordIndex, tagIndex));
            }
            MLEDependencyGrammar.EndHead tempEndHead_1 = TreeToDependencyHelper(kids[0], depList, loc, wordIndex, tagIndex);
            int lHead = tempEndHead_1.head;
            int split = tempEndHead_1.end;

            tempEndHead_1 = TreeToDependencyHelper(kids[1], depList, tempEndHead_1.end, wordIndex, tagIndex);
            int           end        = tempEndHead_1.end;
            int           rHead      = tempEndHead_1.head;
            string        hTag       = ((IHasTag)tree.Label()).Tag();
            string        lTag       = ((IHasTag)kids[0].Label()).Tag();
            string        rTag       = ((IHasTag)kids[1].Label()).Tag();
            string        hWord      = ((IHasWord)tree.Label()).Word();
            string        lWord      = ((IHasWord)kids[0].Label()).Word();
            string        rWord      = ((IHasWord)kids[1].Label()).Word();
            bool          leftHeaded = hWord.Equals(lWord);
            string        aTag       = (leftHeaded ? rTag : lTag);
            string        aWord      = (leftHeaded ? rWord : lWord);
            int           hT         = tagIndex.IndexOf(hTag);
            int           aT         = tagIndex.IndexOf(aTag);
            int           hW         = (wordIndex.Contains(hWord) ? wordIndex.IndexOf(hWord) : wordIndex.IndexOf(LexiconConstants.UnknownWord));
            int           aW         = (wordIndex.Contains(aWord) ? wordIndex.IndexOf(aWord) : wordIndex.IndexOf(LexiconConstants.UnknownWord));
            int           head       = (leftHeaded ? lHead : rHead);
            int           arg        = (leftHeaded ? rHead : lHead);
            IntDependency dependency = new IntDependency(hW, hT, aW, aT, leftHeaded, (leftHeaded ? split - head - 1 : head - split));

            depList.Add(dependency);
            IntDependency stopL = new IntDependency(aW, aT, IntTaggedWord.StopWordInt, IntTaggedWord.StopTagInt, false, (leftHeaded ? arg - split : arg - loc));

            depList.Add(stopL);
            IntDependency stopR = new IntDependency(aW, aT, IntTaggedWord.StopWordInt, IntTaggedWord.StopTagInt, true, (leftHeaded ? end - arg - 1 : split - arg - 1));

            depList.Add(stopR);
            //System.out.println("Adding: "+dependency+" at "+tree.label());
            tempEndHead_1.head = head;
            return(tempEndHead_1);
        }
        public OutsideRuleFilter(BinaryGrammar bg, IIndex <string> stateIndex, IIndex <string> tagIndex)
        {
            this.tagIndex = tagIndex;
            int numStates = stateIndex.Size();

            numTags = tagIndex.Size();
            Allocate(numStates);
            for (int state = 0; state < numStates; state++)
            {
                string         stateStr = stateIndex.Get(state);
                IList <string> left     = new List <string>();
                IList <string> right    = new List <string>();
                if (!bg.IsSynthetic(state))
                {
                    RegisterRule(left, right, state);
                    continue;
                }
                bool           foundSemi = false;
                bool           foundDots = false;
                IList <string> array     = left;
                StringBuilder  sb        = new StringBuilder();
                for (int c = 0; c < stateStr.Length; c++)
                {
                    if (stateStr[c] == ':')
                    {
                        foundSemi = true;
                        continue;
                    }
                    if (!foundSemi)
                    {
                        continue;
                    }
                    if (stateStr[c] == ' ')
                    {
                        if (sb.Length > 0)
                        {
                            string str = sb.ToString();
                            if (!tagIndex.Contains(str))
                            {
                                str = null;
                            }
                            array.Add(str);
                            sb = new StringBuilder();
                        }
                        continue;
                    }
                    if (!foundDots && stateStr[c] == '.')
                    {
                        c        += 3;
                        foundDots = true;
                        array     = right;
                        continue;
                    }
                    sb.Append(stateStr[c]);
                }
                RegisterRule(left, right, state);
            }
        }
Ejemplo n.º 7
0
 // naClass.init(naWord, ttags);
 private int Add(AmbiguityClass a)
 {
     if (classes.Contains(a))
     {
         return(classes.IndexOf(a));
     }
     classes.Add(a);
     return(classes.IndexOf(a));
 }
Ejemplo n.º 8
0
        /// <summary>Checks whether a word is in the lexicon.</summary>
        /// <remarks>
        /// Checks whether a word is in the lexicon. This version works even while
        /// compiling lexicon with current counters (rather than using the compiled
        /// rulesWithWord array).
        /// TODO: The previous version would insert rules into the
        /// wordNumberer.  Is that the desired behavior?  Why not test in
        /// some way that doesn't affect the index?  For example, start by
        /// testing wordIndex.contains(word).
        /// </remarks>
        /// <param name="word">The word as a String</param>
        /// <returns>Whether the word is in the lexicon</returns>
        public virtual bool IsKnown(string word)
        {
            if (!wordIndex.Contains(word))
            {
                return(false);
            }
            IntTaggedWord iW = new IntTaggedWord(wordIndex.IndexOf(word), nullTag);

            return(seenCounter.GetCount(iW) > 0.0);
        }
Ejemplo n.º 9
0
        public void Add(string key, Stream data)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            CheckDisposed();
            if (_index.Contains(key))
            {
                throw new DuplicateException($"An entry with the same key ({key}) already exists.");
            }

            var indexData = _storage.Append(data);

            _index.Add(key, indexData);
        }
Ejemplo n.º 10
0
        public bool Contains(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            CheckDisposed();

            return(_index.Contains(key));
        }
Ejemplo n.º 11
0
        public virtual void TestUnmodifiableViewEtc()
        {
            IList <string> list = new List <string>();

            list.Add("A");
            list.Add("B");
            list.Add("A");
            list.Add("C");
            HashIndex <string> index4 = new HashIndex <string>(list);
            HashIndex <string> index5 = new HashIndex <string>();

            Sharpen.Collections.AddAll(index5, list);
            NUnit.Framework.Assert.AreEqual("Equality failure", index4, index5);
            index5.AddToIndex("D");
            index5.AddToIndex("E");
            index5.IndexOf("F");
            Sharpen.Collections.AddAll(index5, list);
            NUnit.Framework.Assert.AreEqual(5, index5.Count);
            NUnit.Framework.Assert.AreEqual(3, index4.Count);
            NUnit.Framework.Assert.IsTrue(index4.Contains("A"));
            NUnit.Framework.Assert.AreEqual(0, index4.IndexOf("A"));
            NUnit.Framework.Assert.AreEqual(1, index4.IndexOf("B"));
            NUnit.Framework.Assert.AreEqual(2, index4.IndexOf("C"));
            NUnit.Framework.Assert.AreEqual("A", index4.Get(0));
            IIndex <string> index4u = index4.UnmodifiableView();

            NUnit.Framework.Assert.AreEqual(3, index4u.Size());
            NUnit.Framework.Assert.IsTrue(index4u.Contains("A"));
            NUnit.Framework.Assert.AreEqual(0, index4u.IndexOf("A"));
            NUnit.Framework.Assert.AreEqual(1, index4u.IndexOf("B"));
            NUnit.Framework.Assert.AreEqual(2, index4u.IndexOf("C"));
            NUnit.Framework.Assert.AreEqual("A", index4u.Get(0));
            NUnit.Framework.Assert.AreEqual(-1, index4u.AddToIndex("D"));
            bool okay = false;

            try
            {
                index4u.Unlock();
            }
            catch (NotSupportedException)
            {
                okay = true;
            }
            finally
            {
                NUnit.Framework.Assert.IsTrue(okay);
            }
        }
Ejemplo n.º 12
0
 public bool Contains(string key)
 {
     return(ReadLock(() => _index.Contains(key), $"Timeout {_timeout} expired to read index by key {key}"));
 }
Ejemplo n.º 13
0
 public override bool Contains(object o)
 {
     return(backingIndex.Contains(o) || spilloverIndex.Contains(o));
 }