//line is a jsonstring of map of label to array of strings; ex: {"name":["Bush","Carter","Obama"]} /// <exception cref="System.Exception"/> public virtual string DoNewPhrases(string line) { System.Console.Out.WriteLine("adding new phrases"); ConstantsAndVariables constVars = new ConstantsAndVariables(props, humanLabelClasses.Keys, humanLabelClasses); IJsonReader jsonReader = Javax.Json.Json.CreateReader(new StringReader(line)); IJsonObject objarr = jsonReader.ReadObject(); foreach (KeyValuePair <string, IJsonValue> o in objarr) { string label = o.Key; ICollection <CandidatePhrase> seed = new HashSet <CandidatePhrase>(); IJsonArray arr = objarr.GetJsonArray(o.Key); for (int i = 0; i < arr.Count; i++) { string seedw = arr.GetString(i); System.Console.Out.WriteLine("adding " + seedw + " to seed "); seed.Add(CandidatePhrase.CreateOrGet(seedw)); } Sharpen.Collections.AddAll(seedWords[label], seed); constVars.AddSeedWords(label, seed); GetPatternsFromDataMultiClass.RunLabelSeedWords(Data.sents, humanLabelClasses[label], label, seed, constVars, false); } //model.labelWords(label, labelclass, Data.sents, seed); return("SUCCESS added new phrases"); }
public JsonObject BuildObject(JsonObject jsonObject = null) { JsonObject returnValue = jsonObject ?? new JsonObject(); string key = null; Reader.ReadObject(name => { key = name; return(true); }, () => returnValue.Add(JsonApi.Naming(key, Config.NamingType), BuildValue(returnValue.Get(key)))); //todo 待测试复用是否正常 return(returnValue); }
public void BuildObject() { TextWriter.BeginObject(); Reader.ReadObject( name => { TextWriter.Write(JsonApi.Naming(name, Config.NamingType), JsonValueType.String); return(true); }, () => BuildValue() ); TextWriter.EndObject(); }
private int ChangeAnnotation(string line, bool remove) { int tokensNum = 0; IJsonReader jsonReader = Javax.Json.Json.CreateReader(new StringReader(line)); IJsonObject objarr = jsonReader.ReadObject(); foreach (string label in objarr.Keys) { IJsonObject obj4label = objarr.GetJsonObject(label); foreach (string sentid in obj4label.Keys) { IJsonArray tokenArry = obj4label.GetJsonArray(sentid); foreach (IJsonValue tokenid in tokenArry) { tokensNum++; Data.sents[sentid].GetTokens()[System.Convert.ToInt32(tokenid.ToString())].Set(humanLabelClasses[label], remove ? backgroundSymbol : label); } } } return(tokensNum); }
/// <exception cref="System.IO.IOException"/> /// <exception cref="System.TypeLoadException"/> internal virtual void SetUpProperties(string line, bool readFile, bool writeOutputToFile, string additionalSeedWordsFiles) { IJsonReader jsonReader = Javax.Json.Json.CreateReader(new StringReader(line)); IJsonObject objarr = jsonReader.ReadObject(); jsonReader.Close(); Properties props = new Properties(); foreach (string o in objarr.Keys) { if (o.Equals("seedWords")) { IJsonObject obj = objarr.GetJsonObject(o); foreach (string st in obj.Keys) { seedWords[st] = new HashSet <CandidatePhrase>(); IJsonArray arr = obj.GetJsonArray(st); for (int i = 0; i < arr.Count; i++) { string val = arr.GetString(i); seedWords[st].Add(CandidatePhrase.CreateOrGet(val)); System.Console.Out.WriteLine("adding " + val + " for label " + st); } } } else { props.SetProperty(o, objarr.GetString(o)); } } System.Console.Out.WriteLine("seedwords are " + seedWords); if (additionalSeedWordsFiles != null && !additionalSeedWordsFiles.IsEmpty()) { IDictionary <string, ICollection <CandidatePhrase> > additionalSeedWords = GetPatternsFromDataMultiClass.ReadSeedWords(additionalSeedWordsFiles); logger.Info("additional seed words are " + additionalSeedWords); foreach (string label in seedWords.Keys) { if (additionalSeedWords.Contains(label)) { Sharpen.Collections.AddAll(seedWords[label], additionalSeedWords[label]); } } } outputFile = null; if (readFile) { System.Console.Out.WriteLine("input value is " + objarr.GetString("input")); outputFile = props.GetProperty("input") + "_processed"; props.SetProperty("file", objarr.GetString("input")); if (writeOutputToFile && !props.Contains("columnOutputFile")) { props.SetProperty("columnOutputFile", outputFile); } } else { string systemdir = Runtime.GetProperty("java.io.tmpdir"); File tempFile = File.CreateTempFile("sents", ".tmp", new File(systemdir)); tempFile.DeleteOnExit(); IOUtils.WriteStringToFile(props.GetProperty("input"), tempFile.GetPath(), "utf8"); props.SetProperty("file", tempFile.GetAbsolutePath()); } SetProperties(props); this.props = props; int i_1 = 1; foreach (string label_1 in seedWords.Keys) { string ansclstr = "edu.stanford.nlp.patterns.PatternsAnnotations$PatternLabel" + i_1; Type mcCl = (Type)Sharpen.Runtime.GetType(ansclstr); machineAnswerClasses[label_1] = mcCl; string humanansclstr = "edu.stanford.nlp.patterns.PatternsAnnotations$PatternHumanLabel" + i_1; humanLabelClasses[label_1] = (Type)Sharpen.Runtime.GetType(humanansclstr); i_1++; } }
public object BuildArray(Type type, object instance = null) { if (type is null) { throw new ArgumentNullException(nameof(type)); } if (!JsonApi.TryGetArrayType(type, out JsonArrayType arrayType)) { throw new JsonNotSupportException(type); } switch (arrayType) { default: throw new JsonNotSupportException(arrayType); case JsonArrayType.Array: { JsonArray jsonArray = new JsonValueBuilder(Reader, Config).BuildArray(); List <int> bounds = new List <int> (); MeasureArray(jsonArray, ref bounds); if (bounds.Count != type.GetArrayRank()) { throw new JsonException("数组维数不匹配"); } Type elementType = type.GetElementType(); Array array = null; bool create = false; if (instance?.GetType() != type) { create = true; } else { array = (Array)instance; for (int i = 0; i < array.Rank; i++) { if (array.GetLength(i) != bounds[i]) { create = true; break; } } } if (create) { if (bounds.Count == 1) { array = Array.CreateInstance(elementType, bounds[0]); } else { array = Array.CreateInstance(elementType, bounds.ToArray()); } } if (array.Rank == 1) { for (int i = 0; i < jsonArray.Count; i++) { JsonDeserializer deserializer = new JsonDeserializer(new JsonValueReader(jsonArray[i], Config), Config); array.SetValue(deserializer.BuildValue(elementType, array.GetValue(i)), i); } return(array); } int[] indices = new int[bounds.Count]; int dimension = 0; void ForEachArray(JsonArray currentArray) { int length = bounds[dimension]; for (int i = 0; i < length; i++) { indices[dimension] = i; if (dimension == indices.Length - 1) { JsonDeserializer deserializer = new JsonDeserializer(new JsonValueReader(currentArray[i], Config), Config); array.SetValue(deserializer.BuildValue(elementType, array.GetValue(indices)), indices); continue; } dimension++; ForEachArray(currentArray[i]); } dimension--; } ForEachArray(jsonArray); return(array); } case JsonArrayType.GenericList: case JsonArrayType.GenericIList: case JsonArrayType.GenericObservableCollection: { Type elementType = type.GetGenericArguments()[0]; if (instance?.GetType() != type) { if (type.IsInterface) { switch (arrayType) { default: throw new JsonNotSupportException(arrayType); case JsonArrayType.GenericIList: instance = JsonApi.CreateInstance(typeof(List <>).MakeGenericType(elementType)); break; } } else { instance = JsonApi.CreateInstance(type); } } IList list = (IList)instance; int oldCount = list.Count; int newCount = 0; Reader.ReadArray(i => { newCount = i + 1; if (i < oldCount) { list[i] = BuildValue(elementType, list[i]); return; } list.Add(BuildValue(elementType)); }); while (list.Count > newCount) { list.RemoveAt(list.Count - 1); } return(list); } case JsonArrayType.DataTable: { if (instance?.GetType() != type) { instance = JsonApi.CreateInstance(type); } DataTable dataTable = (DataTable)instance; int columnNumber = dataTable.Columns.Count; int columnIndex; ArrayList arrayList = null; object[] values = null; Reader.ReadArray(i => { bool hasRow = dataTable.Rows.Count > i; if (!hasRow) { if (i == 0) { arrayList = new ArrayList(); } else if (values is null) { values = new object[columnNumber]; } } columnIndex = 0; Reader.ReadObject(columnName => { if (!hasRow && i == 0) { columnNumber++; dataTable.Columns.Add(columnName); } return(true); }, () => { object value = BuildValue(); if (hasRow) { dataTable.Rows[i].ItemArray[columnIndex] = value; return; } if (i == 0) { arrayList.Add(value); return; } values[columnIndex] = value; columnIndex++; }); if (!hasRow) { dataTable.Rows.Add(i == 0 ? arrayList.ToArray() : values); } }); return(dataTable); } } }