public override bool Match(object check, Context context, IContinuation succ, IFailure fail) { if (!(check is IParsedPhrase)) { fail.Fail("Cannot match a " + check.GetType(), succ); return(true); } IParsedPhrase phrase = (IParsedPhrase)check; if (phrase.Part != "=P") { fail.Fail("Can only match a paragraph", succ); return(true); } context.Map["$sentences.check"] = check; TwoTuple <List <IContent>, IContinuation> parts = SplitArguments(context, succ); List <IContent> chunk = parts.one; // Match against each element int sentenceStart = 0; foreach (IParsedPhrase constituent in phrase.Branches) { Context first = new Context(context, chunk); first.Map["$sentences.index"] = sentenceStart + 1; Matcher.MatchAgainst(salience, first, constituent, new List <IParsedPhrase>(), parts.two, fail); } return(true); }
public override void SetResult(TwoTuple <Context, IFailure> result, double weight, string location) { context = result.one; fail = result.two; coderack = context.Coderack; salience *= weight; base.SetResult(result, weight, location); }
private Interval AdjustInterval(Interval interval, TwoTuple aggregatedLowerTuple, TwoTuple aggregatedUpperTuple) { var lowerTupleFactor = this.decimalConverter.ConvertToDecimal(aggregatedLowerTuple); var upperTupleFactor = this.decimalConverter.ConvertToDecimal(aggregatedUpperTuple); Interval adjustedInterval = lowerTupleFactor < upperTupleFactor ? new Interval(lowerTupleFactor, upperTupleFactor) : new Interval(upperTupleFactor, lowerTupleFactor); var result = interval.LowerBound + (interval.Width * adjustedInterval); return result; }
public static Tuple <string, string, string> RegularGetSplit(StringDFA dfa, string word, int n) { TwoTuple <int, int> midIndex = detectLoopFromState(dfa, dfa.q_0, word, n); if (midIndex != null) { string start = word.Substring(0, midIndex.first); string mid = word.Substring(midIndex.first, midIndex.second - midIndex.first + 1); string end = word.Substring(midIndex.second + 1); return(Tuple.Create(start, mid, end)); } return(null); }
public void NextSentence(Context context, IContinuation succ, IFailure fail, params object[] args) { int?sentenceStart = context.LookupDefaulted <int?>("$sentences.index", null); TwoTuple <List <IContent>, IContinuation> parts = SplitArguments(context, succ); List <IContent> chunk = parts.one; Context first = new Context(context, chunk); first.Map["$sentences.index"] = sentenceStart + 1; GroupPhrase groupPhrase = new GroupPhrase((IParsedPhrase)context.Lookup("$sentences.check")); Matcher.MatchAgainst(salience, first, groupPhrase.GetBranch(sentenceStart.Value), new List <IParsedPhrase>(), parts.two, fail); }
public static void Main(string[] args) { ToolArguments parsedArgs = new ToolArguments(args, "None", new MainClass()); PorterStemmer stemmer = new PorterStemmer(); if (parsedArgs["stem"] != null) { Console.WriteLine(parsedArgs["stem"] + " => " + stemmer.stemTerm(parsedArgs["stem"])); } if (parsedArgs["freqrows"] != null) { DataReader reader = new DataReader(parsedArgs["f"]); for (string[] row = reader.ReadRow(); row != null; row = reader.ReadRow()) { TwoTuple <int, int> counts = FrequencyTools.WordCount(parsedArgs["freqrows"], row[1]); Console.WriteLine(counts.one + "," + counts.two + ",\"" + row[2] + "\""); } } if (parsedArgs["emotion"] != null) { ANEWEmotionSensor sensor = new ANEWEmotionSensor("/Users/jrising/projects/virsona/github/data"); double[] emotions = sensor.EstimateEmotions(parsedArgs["emotion"]); for (int ii = 0; ii < (int)ANEWEmotionSensor.Emotions.COUNT; ii++) { Console.WriteLine(((ANEWEmotionSensor.Emotions)ii).ToString() + ": " + emotions[ii]); } } if (parsedArgs["emorows"] != null) { int rows = 0, valids = 0; ANEWEmotionSensor sensor = new ANEWEmotionSensor("/Users/jrising/projects/virsona/github/data"); DataReader reader = new DataReader(parsedArgs["f"]); for (string[] row = reader.ReadRow(); row != null; row = reader.ReadRow()) { rows++; double[] emotions = sensor.EstimateEmotions(row[1]); Console.WriteLine("\"" + row[0] + "\"," + emotions[0] + "," + emotions[1] + "," + emotions[2] + "," + emotions[3] + "," + emotions[4] + "," + emotions[5] + "," + emotions[6] + "," + emotions[7] + ",\"" + row[2] + "\""); if (!double.IsNaN(emotions[0])) { valids++; } } } }
public decimal ConvertToDecimal(TwoTuple tuple) { var set = tuple.Label.LabelSet; var beta = set.DeltaInv(tuple); decimal k = 1m; if (beta < set.G) { int h = (int)Math.Truncate(beta); var sigma = beta - h; var membershipTuple1 = new TwoTuple(set[h], 1 - sigma); var membershipTuple2 = new TwoTuple(set[h + 1], sigma); k = (membershipTuple1.Label.M * membershipTuple1.Alpha) + (membershipTuple2.Label.M * membershipTuple2.Alpha); } return k; }
public BarDataPoint(TwoTuple <float> Coords, float Value) { this.Coords = Coords; this.Value = Value; }
protected static TTwo TwoTupleTwor <TOne, TTwo>(TwoTuple <TOne, TTwo> elt, object shared) { return(elt.two); }
protected static TOne TwoTupleOner <TOne, TTwo>(TwoTuple <TOne, TTwo> elt, object shared) { return(elt.one); }
//returns a TwoTuple of items that make up the near straight //and a List of the poistions which need to be removed public TwoTuple<int, List<int>> straightToTuple() { TwoTuple<int, List<int>> result = new TwoTuple<int, List<int>>(0, new List<int>()); //loop through all the cards and see which one has the highest straight //result is the first one we come by that is the greatest for (int i = 0; i < 5; ++i) { int straightCheck = hand[i].value; int numStraight = 0; List<int> positions = new List<int>(); for (int j = 0; j < 5; ++j) { //if the card is within four, than it could be apart of a straight. if (hand[j].value <= straightCheck && hand[j].value >= straightCheck - 4) ++numStraight; else positions.Add(j); } if (numStraight > result.getFirst()) { result.setFirst(numStraight); result.setSecond(positions); } } return result; }
/// <summary> /// Constructor for the bar plot class /// </summary> /// <param name="data">Pre-binned data array with data points that each represent one bar</param> /// <param name="plotModel">Prefab that is used for the bars. A simple cube is recommended</param> /// <param name="barWidth">Width of the bar model as a scaling factor.Default is 1 </param> /// <param name="barHeight">Height of the bar model as a scaling factor. Default is 1 </param> /// <param name="plotAnchor">Position in space. Defaults to (0f, 0f, 0f)</param> /// <param name="binCount">PLACEHOLDER PARAMETER</param> public BarPlot(BarDataPoint[] data, GameObject plotModel, float barWidth = 1, float barHeight = 1, Vector3 plotAnchor = default, TwoTuple <int> binCount = default) : base(data, plotModel, plotAnchor) { this.barWidth = barWidth; this.barHeight = barHeight; this.binCount = binCount; }
protected static double MatchComparison(TwoTuple <T, T> onetwo, object objshr) { MatchShared shared = (MatchShared)objshr; return(shared.scorer(onetwo.one, onetwo.two, shared.objshr)); }
public static void Main(string[] args) { ToolArguments parsedArgs = new ToolArguments(args, "None", new MainClass()); PorterStemmer stemmer = new PorterStemmer(); if (parsedArgs["stem"] != null) { Console.WriteLine(parsedArgs["stem"] + " => " + stemmer.stemTerm(parsedArgs["stem"])); } /*ANEWEmotionSensor sensor2 = new ANEWEmotionSensor("/Users/jrising/projects/virsona/github/data"); * for (int rr = 0; rr < sensor2.positiveMatrix.GetLength(0); rr++) { * for (int cc = 0; cc < sensor2.positiveMatrix.GetLength(1); cc++) * Console.Write(sensor2.positiveMatrix[rr, cc] + ", "); * Console.WriteLine(" - "); * } * for (int rr = 0; rr < sensor2.negativeMatrix.GetLength(0); rr++) { * for (int cc = 0; cc < sensor2.negativeMatrix.GetLength(1); cc++) * Console.Write(sensor2.negativeMatrix[rr, cc] + ", "); * Console.WriteLine(" - "); * } * return;*/ if (parsedArgs["freqrows"] != null) { DataReader reader = new DataReader(parsedArgs["f"]); for (string[] row = reader.ReadRow(); row != null; row = reader.ReadRow()) { TwoTuple <int, int> counts = FrequencyTools.WordCount(parsedArgs["freqrows"], row[1]); Console.WriteLine(counts.one + "," + counts.two + ",\"" + row[2] + "\""); } } if (parsedArgs["emotion"] != null) { ANEWEmotionSensor sensor = new ANEWEmotionSensor("/Users/jrising/projects/virsona/github/data"); double[] emotions = sensor.EstimateEmotions(parsedArgs["emotion"]); for (int ii = 0; ii < (int)ANEWEmotionSensor.Emotions.COUNT; ii++) { Console.WriteLine(((ANEWEmotionSensor.Emotions)ii).ToString() + ": " + emotions[ii]); } } if (parsedArgs["emorows"] != null) { int rows = 0, valids = 0; ANEWEmotionSensor sensor = new ANEWEmotionSensor("/Users/jrising/projects/virsona/github/data"); DataReader reader = new DataReader(parsedArgs["f"]); for (string[] row = reader.ReadRow(); row != null; row = reader.ReadRow()) { rows++; double[] emotions = sensor.EstimateEmotions(row[1]); Console.WriteLine("\"" + row[0] + "\"," + emotions[0] + "," + emotions[1] + "," + emotions[2] + "," + emotions[3] + "," + emotions[4] + "," + emotions[5] + "," + emotions[6] + "," + emotions[7] + ",\"" + row[2] + "\""); if (!double.IsNaN(emotions[0])) { valids++; } } } if (parsedArgs["eimpute"] != null) { ANEWEmotionSensor sensor = new ANEWEmotionSensor("/Users/jrising/projects/virsona/github/data"); // DIAGNOSTIC /*List<List<string>> rows = new List<List<string>>(); * rows.Add(TwitterUtilities.SplitWords("happy aaaa cccc")); * rows.Add(TwitterUtilities.SplitWords("sad bbbb cccc")); * * IDataSource<string, ThreeTuple<ContinuousDistribution, ContinuousDistribution, ContinuousDistribution>> inputed = sensor.ImputeEmotionalContent(rows, 1000); * foreach (KeyValuePair<string, ThreeTuple<ContinuousDistribution, ContinuousDistribution, ContinuousDistribution>> kvp in inputed) * Console.WriteLine(kvp.Key + ": " + kvp.Value.one.Mean + ", " + kvp.Value.two.Mean + ", " + kvp.Value.three.Mean);*/ bool smallFile = false; if (smallFile) { DataReader reader = new DataReader(parsedArgs["f"]); List <List <string> > rows = new List <List <string> >(); for (string[] row = reader.ReadRow(); row != null; row = reader.ReadRow()) { Console.WriteLine(row); rows.Add(TwitterUtilities.SplitWords(row[10].ToLower())); } reader.Close(); /*IDataSource<string, ThreeTuple<ContinuousDistribution, ContinuousDistribution, ContinuousDistribution>> inputed = sensor.ImputeEmotionalContent(rows, 10); * double minv = 1, maxv = 0; * foreach (KeyValuePair<string, ThreeTuple<ContinuousDistribution, ContinuousDistribution, ContinuousDistribution>> kvp in inputed) { * minv = Math.Min(minv, kvp.Value.one.Mean); * maxv = Math.Max(maxv, kvp.Value.one.Mean); * Console.WriteLine(kvp.Key + ": " + kvp.Value.one.Mean + " x " + kvp.Value.one.Variance + ", " + kvp.Value.two.Mean + ", " + kvp.Value.three.Mean); * } * * Console.WriteLine("Min: " + minv + ", Max: " + maxv);*/ sensor.ImputeEmotionalContent(rows, 10, parsedArgs["f"] + "imputed"); } else { sensor.ImputeEmotionalContentFromFile(parsedArgs["f"], 11, 0, parsedArgs["f"].Substring(0, parsedArgs["f"].Length - 4) + "imputed.csv"); } uint jj = 0; using (var stream = File.CreateText(parsedArgs["f"] + "result")) { jj++; if (jj % 1000 == 0) { Console.WriteLine("#" + jj); } DataReader reader = new DataReader(parsedArgs["f"]); for (string[] row = reader.ReadRow(); row != null; row = reader.ReadRow()) { double[] emotions = sensor.EstimateEmotions(row[11]); for (int ii = 0; ii < 11; ii++) { stream.Write(row[ii] + ","); } stream.WriteLine(emotions[0] + "," + emotions[1] + "," + emotions[2] + "," + emotions[3] + "," + emotions[4] + "," + emotions[5] + "," + emotions[6] + "," + emotions[7]); } } } }
public Opinion(TwoTuple lowerOpinion, TwoTuple upperOpinion) { this.LowerOpinion = lowerOpinion; this.UpperOpinion = upperOpinion; }
private static Tuple <NFA <string, string>, HashSet <TwoTuple <State <string>, State <string> > > > epsNFA(SymbolicString symbolicString, HashSet <string> alphabet, Dictionary <VariableType, UnaryComparison> comparisons) { var states = new HashSet <State <string> >(); var delta = new Dictionary <TwoTuple <State <string>, string>, HashSet <State <string> > >(); var Q_0 = new HashSet <State <string> >(); var F = new HashSet <State <string> >(); var epsilonTransitions = new HashSet <TwoTuple <State <string>, State <string> > >(); var nfa = new NFA <string, string>(states, alphabet, delta, Q_0, F); switch (symbolicString.expression_type) { case SymbolicString.SymbolicStringType.Symbol: var symbol = symbolicString.atomic_symbol; var q_0 = createState(); Q_0.Add(q_0); states.Add(q_0); var q_1 = createState(); states.Add(q_1); F.Add(q_1); var twoTuple = new TwoTuple <State <string>, string>(q_0, symbol); var to = new HashSet <State <string> >(); to.Add(q_1); delta.Add(twoTuple, to); nfa = new NFA <string, string>(states, alphabet, delta, Q_0, F); return(Tuple.Create(nfa, epsilonTransitions)); case SymbolicString.SymbolicStringType.Concat: var listOfNFAs = new LinkedList <Tuple <NFA <string, string>, HashSet <TwoTuple <State <string>, State <string> > > > >(); foreach (SymbolicString str in symbolicString.sub_strings) { listOfNFAs.AddLast(epsNFA(str, alphabet, comparisons)); } return(concatAutomata(listOfNFAs)); case SymbolicString.SymbolicStringType.Repeat: LinearIntegerExpression lie = symbolicString.repeat; var lisNFAs = new LinkedList <Tuple <NFA <string, string>, HashSet <TwoTuple <State <string>, State <string> > > > >(); for (int i = 0; i < lie.constant; i++) { lisNFAs.AddLast(epsNFA(symbolicString.root, alphabet, comparisons)); } foreach (var coeff in lie.coefficients) { var compar = comparisons[coeff.Key]; switch (compar.comparisonType) { case UnaryComparison.ComparisonType.EQUAL: for (int i = 0; i < coeff.Value * compar.constant; i++) { lisNFAs.AddLast(epsNFA(symbolicString.root, alphabet, comparisons)); } break; case UnaryComparison.ComparisonType.GREATER: for (int i = 0; i < coeff.Value * (compar.min + 1) - 1; i++) { lisNFAs.AddLast(epsNFA(symbolicString.root, alphabet, comparisons)); } var autom = epsNFA(symbolicString.root, alphabet, comparisons); var autom_start = autom.Item1.Q_0.First(); var newEpsTrans = new HashSet <TwoTuple <State <string>, State <string> > >(); newEpsTrans.UnionWith(autom.Item2); foreach (var acceptingState in autom.Item1.F) { newEpsTrans.Add(new TwoTuple <State <string>, State <string> >(acceptingState, autom_start)); } lisNFAs.AddLast(Tuple.Create(autom.Item1, newEpsTrans)); break; case UnaryComparison.ComparisonType.BETWEEN: for (int i = 0; i < coeff.Value * compar.min; i++) { lisNFAs.AddLast(epsNFA(symbolicString.root, alphabet, comparisons)); } var rememberedStates = new HashSet <State <string> >(); int upperbound = 0; if (compar.min == -1 || compar.min == 0) { upperbound = compar.max - 1; } else { upperbound = compar.max - compar.min - 1; } for (int i = 0; i < coeff.Value * upperbound; i++) { var aut = epsNFA(symbolicString.root, alphabet, comparisons); foreach (var q_f in aut.Item1.F) { rememberedStates.Add(q_f); } lisNFAs.AddLast(aut); } if (compar.min == -1) { rememberedStates.Add(lisNFAs.First.Value.Item1.Q_0.First()); } var epsTrans = new HashSet <TwoTuple <State <string>, State <string> > >(); var lastAut = lisNFAs.Last.Value; foreach (var state in rememberedStates) { foreach (var q_f in lastAut.Item1.F) { epsTrans.Add(new TwoTuple <State <string>, State <string> >(state, q_f)); } } lisNFAs.RemoveLast(); lisNFAs.AddLast(Tuple.Create(lastAut.Item1, new HashSet <TwoTuple <State <string>, State <string> > >(epsTrans.Union(lastAut.Item2)))); break; } } return(concatAutomata(lisNFAs)); default: throw new ArgumentException(); } }
public StringDFA(XElement automatonXML, bool deterministic) { AutomatonXML aXML; var xmlSerializer = new XmlSerializer(typeof(AutomatonXML)); var doc = new XDocument(); doc.Add(automatonXML); using (var reader = doc.CreateReader()) { aXML = (AutomatonXML)xmlSerializer.Deserialize(reader); } if (deterministic) { this.q_0 = aXML.initState[0].sid; this.F = new HashSet <string>(); foreach (var state in aXML.acceptingStates) { this.F.Add(state.sid); } this.delta = new Dictionary <TwoTuple <string, string>, string>(); foreach (var transition in aXML.transitions) { delta.Add(new TwoTuple <string, string>(transition.from + "", transition.read), transition.to + ""); } this.states = new HashSet <string>(); foreach (var state in aXML.states) { this.states.Add(state.sid); } this.alphabet = new HashSet <string>(); foreach (string letter in aXML.alphabet) { alphabet.Add(letter); } } else { //NFA HashSet <State <string> > states = new HashSet <State <string> >(); foreach (var state in aXML.states) { if (Int32.TryParse(state.sid, out int parsed)) { states.Add(new State <string>(parsed, state.sid)); } else { states.Add(new State <string>(0, state.sid)); } } HashSet <string> alphabet = new HashSet <string>(); foreach (var letter in aXML.alphabet) { alphabet.Add(letter); } HashSet <TwoTuple <State <string>, State <string> > > epsilonTransitions = new HashSet <TwoTuple <State <string>, State <string> > >(); Dictionary <TwoTuple <State <string>, string>, HashSet <State <string> > > delta = new Dictionary <TwoTuple <State <string>, string>, HashSet <State <string> > >(); foreach (var transition in aXML.transitions) { if (transition.read.Equals("epsilon")) { epsilonTransitions.Add(new TwoTuple <State <string>, State <string> >(new State <string>(transition.from + ""), new State <string>(transition.to + ""))); } else { TwoTuple <State <string>, string> twoTuple = new TwoTuple <State <string>, string>(new State <string>(transition.from + ""), transition.read); HashSet <State <string> > set; if (delta.TryGetValue(twoTuple, out set)) { set.Add(new State <string>(transition.to + "")); } else { set = new HashSet <State <string> >(); set.Add(new State <string>(transition.to + "")); delta.Add(new TwoTuple <State <string>, string>(new State <string>(transition.from + ""), transition.read), set); } } } HashSet <State <string> > Q_0 = new HashSet <State <string> >(); foreach (var state in aXML.initState) { Q_0.Add(new State <string>(state.sid)); } HashSet <State <string> > F = new HashSet <State <string> >(); foreach (var state in aXML.acceptingStates) { F.Add(new State <string>(state.sid)); } var nfa = collectEpsilonTransitions(states, alphabet, delta, Q_0, F, epsilonTransitions); var dfa = nfa.NFAtoDFA(); var dfa_min = dfa.MinimizeHopcroft(); var strDFA = new StringDFA(dfa_min); //rename error set foreach (var state in strDFA.states) { if (!strDFA.F.Contains(state)) { bool errorStateFound = true; foreach (var letter in strDFA.alphabet) { if (!strDFA.delta[new TwoTuple <string, string>(state, letter)].Equals(state)) { errorStateFound = false; break; } } if (errorStateFound) { strDFA.states.Remove(state); strDFA.states.Add("-1"); if (strDFA.q_0.Equals(state)) { strDFA.q_0 = "-1"; } HashSet <KeyValuePair <TwoTuple <string, string>, string> > entriesToChange = new HashSet <KeyValuePair <TwoTuple <string, string>, string> >(); foreach (var entry in strDFA.delta) { if (entry.Value.Equals(state)) { entriesToChange.Add(entry); } } foreach (var entry in entriesToChange) { strDFA.delta[entry.Key] = "-1"; } foreach (var letter in strDFA.alphabet) { strDFA.delta.Remove(new TwoTuple <string, string>(state, letter)); strDFA.delta.Add(new TwoTuple <string, string>("-1", letter), "-1"); } break; } } } this.alphabet = strDFA.alphabet; this.delta = strDFA.delta; this.F = strDFA.F; this.q_0 = strDFA.q_0; this.states = strDFA.states; } }