Esempio n. 1
0
        /// <summary>
        /// Adds a label to this node
        /// </summary>
        /// <param name="label">Label to add</param>
        /// <param name="syncWithRootNodeCollection">Whether or not to add this node to the corresponding node collection on the root. The
        /// root collections are shortcut collections that allow quick searching for particular node types. Thus, the collections need to
        /// remain synchronized with the labels that are applied to the nodes. Passing true here will perform this synchronization. If you
        /// will do the synchronization on your own later, pass false.</param>
        public void AddLabel(NomBankNodeLabel label, bool syncWithRootNodeCollection)
        {
            _labels.Add(label);

            // add node to root's collection if needed
            if (syncWithRootNodeCollection)
            {
                GetLabeledNodeCollection(label, true).AddSingleNode(this);
            }
        }
        /// <summary>
        /// Gets a deep copy of this label
        /// </summary>
        /// <returns>Deep copy of this label</returns>
        public NomBankNodeLabel Copy()
        {
            NomBankNodeLabel copied = new NomBankNodeLabel(_type, _feature, _confidence);

            foreach (HyphenationIndex hyphenIndex in _hyphenIndexes)
            {
                copied.AddHyphenIndex(hyphenIndex);
            }

            return(copied);
        }
        /// <summary>
        /// Gets whether or not this NomBankNodeLabel equals another object
        /// </summary>
        /// <param name="obj">Object to compare this one with</param>
        /// <returns>True if the other object is a NomBankNodeLabel and is equal to this one, false otherwise</returns>
        public override bool Equals(object obj)
        {
            if (!(obj is NomBankNodeLabel))
            {
                return(false);
            }

            NomBankNodeLabel label = obj as NomBankNodeLabel;

            return(_type == label.Type && _feature == label.Feature);
        }
Esempio n. 4
0
        /// <summary>
        /// Removes a label from this node
        /// </summary>
        /// <param name="label">Label to remove</param>
        public void RemoveLabel(NomBankNodeLabel label)
        {
            _labels.Remove(label);

            // remove from root node collection if it is present
            NomBankLabeledNodeCollection nodes = GetLabeledNodeCollection(label, false);

            if (nodes != null && nodes.ContainsSingleNode(this))
            {
                nodes.RemoveSingleNode(this);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Tries to get a label on this node that matches a given label
        /// </summary>
        /// <param name="label">Label to find</param>
        /// <param name="matchingLabel">Matching label, or null if none was found</param>
        /// <returns>Whether or not label was found</returns>
        public bool TryGetLabel(NomBankNodeLabel label, out NomBankNodeLabel matchingLabel)
        {
            matchingLabel = null;

            if (!HasLabel(label))
            {
                return(false);
            }

            matchingLabel = GetLabel(label);

            return(true);
        }
Esempio n. 6
0
        /// <summary>
        /// Gets label on the current node that matches the given label
        /// </summary>
        /// <param name="label">Label to match</param>
        /// <returns>Matching label</returns>
        public NomBankNodeLabel GetLabel(NomBankNodeLabel label)
        {
            // check each label
            foreach (NomBankNodeLabel current in _labels)
            {
                if (current == label)
                {
                    return(current);
                }
            }

            throw new Exception("Given label not found");
        }
Esempio n. 7
0
        /// <summary>
        /// Gets descendant nodes by label
        /// </summary>
        /// <param name="label">Label of nodes to get</param>
        /// <returns>List of nodes matching label</returns>
        public List <NomBankNode> GetDescendants(NomBankNodeLabel label)
        {
            List <NomBankNode> nodes = new List <NomBankNode>();

            foreach (NomBankNode n in Descendants)
            {
                if (n.Labels.Contains(label))
                {
                    nodes.Add(n);
                }
            }

            return(nodes);
        }
        /// <summary>
        /// Applies the current type to a node, or does nothing if the node already has the type
        /// </summary>
        /// <param name="n">Node to apply type to</param>
        private void ApplyType(NomBankNode n)
        {
            // don't reapply type
            if (n.HasLabel(_label))
            {
                return;
            }

            // add label without hyphen indexes, and don't bother synching with root collection (this collection will be added to the root)
            NomBankNodeLabel label = _label.Copy();

            label.HyphenIndexes.Clear();
            n.AddLabel(label, false);

            // only apply hyphen indexes to text with a hyphen or slash
            string nodeText = n.SurfaceText;

            char[] hyphenSlash = new char[] { '-', '/' };
            if (nodeText.IndexOfAny(hyphenSlash) == -1)
            {
                return;
            }

            // try to apply each hyphen index
            foreach (NomBankNodeLabel.HyphenationIndex hyphenIndex in _label.HyphenIndexes)
            {
                int numParts = nodeText.Split(hyphenSlash, StringSplitOptions.RemoveEmptyEntries).Length;

                // get numeric hyphen index
                int index = int.Parse(hyphenIndex.ToString().Substring(1));

                // add index to node's label if applicable
                if (index < numParts)
                {
                    if (_appliedIndexes.Contains(hyphenIndex))
                    {
                        throw new Exception("Hyphen index applied more than once");
                    }

                    label.AddHyphenIndex(hyphenIndex);
                    _appliedIndexes.Add(hyphenIndex);
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Gets labeled node collection
        /// </summary>
        /// <param name="label">Label of node collection to get</param>
        /// <param name="createIfMissing">Whether or not to create and return a new node collection if none exists</param>
        /// <returns>Labeled node collection</returns>
        public NomBankLabeledNodeCollection GetLabeledNodeCollection(NomBankNodeLabel label, bool createIfMissing)
        {
            // look for an existing collection with the given label
            foreach (NomBankLabeledNodeCollection nodeCollection in LabeledNodeCollections)
            {
                if (nodeCollection.Label == label)
                {
                    return(nodeCollection);
                }
            }

            if (!createIfMissing)
            {
                return(null);
            }

            // return new collection
            NomBankLabeledNodeCollection newCollection = new NomBankLabeledNodeCollection(label);

            LabeledNodeCollections.Add(newCollection);

            return(newCollection);
        }
Esempio n. 10
0
 /// <summary>
 /// Gets whether or not this node has a particular label
 /// </summary>
 /// <param name="label">Label to check for</param>
 /// <returns>True if label is present and false otherwise</returns>
 public bool HasLabel(NomBankNodeLabel label)
 {
     return(_labels.Contains(label));
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="label">Label for collection</param>
 public NomBankLabeledNodeCollection(NomBankNodeLabel label)
 {
     _label          = label;
     _appliedIndexes = new List <NomBankNodeLabel.HyphenationIndex>();
 }