/// <summary> Split up a string containing options into an array of strings, /// one for each option. /// /// </summary> /// <param name="optionString">the string containing the options /// </param> /// <returns> the array of options /// </returns> public static System.String[] splitOptions(System.String quotedOptionString) { FastVector optionsVec = new FastVector(); System.String str = new System.Text.StringBuilder(quotedOptionString).ToString(); int i; while (true) { //trimLeft i = 0; while ((i < str.Length) && (System.Char.IsWhiteSpace(str[i]))) i++; str = str.Substring(i); //stop when str is empty if (str.Length == 0) break; //if str start with a double quote if (str[0] == '"') { //find the first not anti-slached double quote i = 1; while (i < str.Length) { if (str[i] == str[0]) break; if (str[i] == '\\') { i += 1; if (i >= str.Length) throw new System.Exception("String should not finish with \\"); if (str[i] != '\\' && str[i] != '"') throw new System.Exception("Unknow character \\" + str[i]); } i += 1; } if (i >= str.Length) throw new System.Exception("Quote parse error."); //add the founded string to the option vector (without quotes) System.String optStr = str.Substring(1, (i) - (1)); optStr = unbackQuoteChars(optStr); optionsVec.addElement(optStr); str = str.Substring(i + 1); } else { //find first whiteSpace i = 0; while ((i < str.Length) && (!System.Char.IsWhiteSpace(str[i]))) i++; //add the founded string to the option vector System.String optStr = str.Substring(0, (i) - (0)); optionsVec.addElement(optStr); str = str.Substring(i); } } //convert optionsVec to an array of String System.String[] options = new System.String[optionsVec.size()]; for (i = 0; i < optionsVec.size(); i++) { options[i] = ((System.String) optionsVec.elementAt(i)); } return options; }
/// <summary> Constructor for nominal attributes and string attributes, where /// metadata is supplied. If a null vector of attribute values is passed /// to the method, the attribute is assumed to be a string. /// /// </summary> /// <param name="attributeName">the name for the attribute /// </param> /// <param name="attributeValues">a vector of strings denoting the /// attribute values. Null if the attribute is a string attribute. /// </param> /// <param name="metadata">the attribute's properties /// </param> //@ requires attributeName != null; //@ requires metadata != null; /*@ ensures m_Name == attributeName; ensures m_Index == -1; ensures attributeValues == null && m_Type == STRING || attributeValues != null && m_Type == NOMINAL && m_Values.size() == attributeValues.size(); signals (IllegalArgumentException ex) (* if duplicate strings in attributeValues *); */ public Attribute(System.String attributeName, FastVector attributeValues, ProtectedProperties metadata) { m_Name = attributeName; m_Index = - 1; if (attributeValues == null) { m_Values = new FastVector(); m_Hashtable = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable()); m_Type = STRING; } else { m_Values = new FastVector(attributeValues.size()); //m_Hashtable = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable(attributeValues.size())); m_Hashtable = new System.Collections.Hashtable(attributeValues.size()); for (int i = 0; i < attributeValues.size(); i++) { // No serialization System.Object store = attributeValues.elementAt(i); //if (((System.String) store).Length > STRING_COMPRESS_THRESHOLD) //{ // try // { // store = new SerializedObject(attributeValues.elementAt(i), true); // } // catch (System.Exception ex) // { // System.Console.Error.WriteLine("Couldn't compress nominal attribute value -" + " storing uncompressed."); // } //} if (m_Hashtable.ContainsKey(store)) { //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" throw new System.ArgumentException("A nominal attribute (" + attributeName + ") cannot" + " have duplicate labels (" + store + ")."); } m_Values.addElement(store); m_Hashtable.Add(store, (System.Int32) i); } m_Type = NOMINAL; } setMetadata(metadata); }
private Instance createSingleWhyInstance(FastVector fvWhy, Token candidate) { //first word-n attribute number int wordsBeforeFirstAttributeNumber = 7; //first pos-n attribute number int posBeforeFirstAttributeNumber = wordsBeforeFirstAttributeNumber + whyWordsBefore + whyWordsAfter; //word+1 attribute number int wordsAfterFirstAttributeNumber = wordsBeforeFirstAttributeNumber + whyWordsBefore; //pos+1 attribute number int posAfterFirstAttributeNumber = posBeforeFirstAttributeNumber + whyWordsBefore; int totalAttributeCount = wordsBeforeFirstAttributeNumber + whyWordsBefore * 2 + whyWordsAfter * 2 + 1; Instance whyCandidate = new DenseInstance(totalAttributeCount); whyCandidate.setValue((weka.core.Attribute)fvWhy.elementAt(0), candidate.Value); whyCandidate.setValue((weka.core.Attribute)fvWhy.elementAt(1), candidate.Value.Split(' ').Count()); whyCandidate.setValue((weka.core.Attribute)fvWhy.elementAt(2), candidate.Sentence); whyCandidate.setValue((weka.core.Attribute)fvWhy.elementAt(3), candidate.Score); whyCandidate.setValue((weka.core.Attribute)fvWhy.elementAt(4), candidate.NumWho); whyCandidate.setValue((weka.core.Attribute)fvWhy.elementAt(5), candidate.NumWhen); whyCandidate.setValue((weka.core.Attribute)fvWhy.elementAt(6), candidate.NumWhere); for (int i = whyWordsBefore; i > 0; i--) { if (candidate.Position - i - 1 >= 0) { whyCandidate.setValue((weka.core.Attribute)fvWhy.elementAt(whyWordsBefore - i + wordsBeforeFirstAttributeNumber), articleCurrent[candidate.Position - i - 1].Value); if (articleCurrent[candidate.Position - i - 1].PartOfSpeech != null) { whyCandidate.setValue((weka.core.Attribute)fvWhy.elementAt(whyWordsBefore - i + posBeforeFirstAttributeNumber), articleCurrent[candidate.Position - i - 1].PartOfSpeech); } } } for (int i = 0; i < whyWordsAfter; i++) { if (candidate.Position + i < articleCurrent.Count) { whyCandidate.setValue((weka.core.Attribute)fvWhy.elementAt(wordsAfterFirstAttributeNumber + i), articleCurrent[candidate.Position + i].Value); if (articleCurrent[candidate.Position + i].PartOfSpeech != null) { whyCandidate.setValue((weka.core.Attribute)fvWhy.elementAt(posAfterFirstAttributeNumber + i), articleCurrent[candidate.Position + i].PartOfSpeech); } } } return whyCandidate; }
/// <summary> Sets a value of a nominal attribute or string attribute. /// Creates a fresh list of attribute values before it is set. /// /// </summary> /// <param name="index">the value's index /// </param> /// <param name="string">the value /// </param> /// <exception cref="IllegalArgumentException">if the attribute is not nominal or /// string. /// </exception> //@ requires string != null; //@ requires isNominal() || isString(); //@ requires 0 <= index && index < m_Values.size(); internal void setValue(int index, System.String string_Renamed) { switch (m_Type) { case NOMINAL: case STRING: m_Values = (FastVector) m_Values.copy(); m_Hashtable = (System.Collections.Hashtable) m_Hashtable.Clone(); System.Object store = string_Renamed; if (string_Renamed.Length > STRING_COMPRESS_THRESHOLD) { try { store = string_Renamed; } catch (System.Exception ex) { System.Console.Error.WriteLine("Couldn't compress string attribute value -" + " storing uncompressed." + " " + ex.ToString()); } } m_Hashtable.Remove(m_Values.elementAt(index)); m_Values.setXmlElementAt(store, index); m_Hashtable.Add(store, (System.Int32) index); break; default: throw new System.ArgumentException("Can only set values for nominal" + " or string attributes!"); } }
private Instance createSingleWhoInstance(FastVector fvWho, Token candidate) { //first word-n attribute number int wordsBeforeFirstAttributeNumber = 6; //first pos-n attribute number int posBeforeFirstAttributeNumber = wordsBeforeFirstAttributeNumber + whoWordsBefore + whoWordsAfter; //word+1 attribute number int wordsAfterFirstAttributeNumber = wordsBeforeFirstAttributeNumber + whoWordsBefore; //pos+1 attribute number int posAfterFirstAttributeNumber = posBeforeFirstAttributeNumber + whoWordsBefore; int totalAttributeCount = wordsBeforeFirstAttributeNumber + whoWordsBefore * 2 + whoWordsAfter * 2 + 1; Instance whoCandidate = new DenseInstance(totalAttributeCount); whoCandidate.setValue((weka.core.Attribute)fvWho.elementAt(0), candidate.Value); whoCandidate.setValue((weka.core.Attribute)fvWho.elementAt(1), candidate.Value.Split(' ').Count()); whoCandidate.setValue((weka.core.Attribute)fvWho.elementAt(2), candidate.Sentence); whoCandidate.setValue((weka.core.Attribute)fvWho.elementAt(3), candidate.Position); double sentenceStartProximity = -1; foreach (List<Token> tokenList in segregatedArticleCurrent) { if (tokenList.Count > 0 && tokenList[0].Sentence == candidate.Sentence) { sentenceStartProximity = (double)(candidate.Position - tokenList[0].Position) / (double)tokenList.Count; break; } } if (sentenceStartProximity > -1) { whoCandidate.setValue((weka.core.Attribute)fvWho.elementAt(4), sentenceStartProximity); } whoCandidate.setValue((weka.core.Attribute)fvWho.elementAt(5), candidate.Frequency); for (int i = whoWordsBefore; i > 0; i--) { if (candidate.Position - i - 1 >= 0) { whoCandidate.setValue((weka.core.Attribute)fvWho.elementAt(whoWordsBefore - i + wordsBeforeFirstAttributeNumber), articleCurrent[candidate.Position - i - 1].Value); if (articleCurrent[candidate.Position - i - 1].PartOfSpeech != null) { whoCandidate.setValue((weka.core.Attribute)fvWho.elementAt(whoWordsBefore - i + posBeforeFirstAttributeNumber), articleCurrent[candidate.Position - i - 1].PartOfSpeech); } } } for (int i = 0; i < whoWordsAfter; i++) { if (candidate.Position + i < articleCurrent.Count) { whoCandidate.setValue((weka.core.Attribute)fvWho.elementAt(wordsAfterFirstAttributeNumber + i), articleCurrent[candidate.Position + i].Value); if (articleCurrent[candidate.Position + i].PartOfSpeech != null) { whoCandidate.setValue((weka.core.Attribute)fvWho.elementAt(posAfterFirstAttributeNumber + i), articleCurrent[candidate.Position + i].PartOfSpeech); } } } return whoCandidate; }