private static int CountMentions(CorefChain cluster)
        {
            int count = 0;

            foreach (IntPair mid in cluster.GetMentionMap().Keys)
            {
                count += cluster.GetMentionMap()[mid].Count;
            }
            return(count);
        }
        /// <summary>Serializes one coref cluster (i.e., one entity).</summary>
        /// <param name="pw">the buffer</param>
        /// <param name="cid">id of cluster to save</param>
        /// <param name="cluster">the cluster</param>
        public static void SaveCorefChain(PrintWriter pw, int cid, CorefChain cluster)
        {
            pw.Println(cid + " " + CountMentions(cluster));
            // each mention saved on one line
            IDictionary <IntPair, ICollection <CorefChain.CorefMention> > mentionMap = cluster.GetMentionMap();

            foreach (KeyValuePair <IntPair, ICollection <CorefChain.CorefMention> > intPairSetEntry in mentionMap)
            {
                // all mentions with the same head
                IntPair mentionIndices = intPairSetEntry.Key;
                ICollection <CorefChain.CorefMention> mentions = intPairSetEntry.Value;
                foreach (CorefChain.CorefMention mention in mentions)
                {
                    // one mention per line
                    pw.Print(mentionIndices.GetSource() + " " + mentionIndices.GetTarget());
                    if (mention == cluster.GetRepresentativeMention())
                    {
                        pw.Print(" " + 1);
                    }
                    else
                    {
                        pw.Print(" " + 0);
                    }
                    pw.Print(" " + mention.mentionType);
                    pw.Print(" " + mention.number);
                    pw.Print(" " + mention.gender);
                    pw.Print(" " + mention.animacy);
                    pw.Print(" " + mention.startIndex);
                    pw.Print(" " + mention.endIndex);
                    pw.Print(" " + mention.headIndex);
                    pw.Print(" " + mention.corefClusterID);
                    pw.Print(" " + mention.mentionID);
                    pw.Print(" " + mention.sentNum);
                    pw.Print(" " + mention.position.Length());
                    for (int i = 0; i < mention.position.Length(); i++)
                    {
                        pw.Print(" " + mention.position.Get(i));
                    }
                    pw.Print(" " + EscapeSpace(mention.mentionSpan));
                    pw.Println();
                }
            }
        }