TCPを使用してネットワーク経由で送信する必要があるオブジェクトがあります。これはうまくいきましたが、実装を拡張して、FlatBufferスキーマを定義する方法を検討するのに問題があります。FlatBuffers KeyValuePairデータ型とスキーマの定義
送信するオブジェクトは、このです:
public class Prediction
{
public PredictionMethod PredictionMethod { get; set; }
public NGramPrediction NGramPrediction { get; set; }
public DistancePrediction DistancePrediction { get; set; }
public int NGramOrder { get; set; }
}
PredictionMethodは列挙型です:
public enum PredictionMethod
{
Distance = 1,
NGram = 2,
}
NGramPredictionは次のようになります。
public class NGramPrediction
{
public KeyValuePair<char, int> Gram { get; set; }
public double Probability { get; set; }
public string Pattern { get; set; }
private int Total { get; set; }
private int Order { get; set; }
public NGramPrediction(Gram gram)
{
Total = gram.Total();
var orderedKeyPosibilities = gram.Posibilities.OrderByDescending(x => x.Value);
Gram = orderedKeyPosibilities.First();
Pattern = gram.Pattern;
Probability = (double)Gram.Value/Total;
Order = Pattern.Length;
}
}
public class Gram
{
public string Pattern { get; set; }
public Dictionary<char, int> Posibilities { get; set; }
public Gram(List<char> posibilities, int initialValue = 0)
{
Posibilities = new Dictionary<char, int>();
foreach (var posibility in posibilities)
{
Posibilities.Add(posibility, initialValue);
}
}
public int Total()
{
var keys = Posibilities.Keys;
var total = 0;
foreach (var key in keys)
{
var value = Posibilities[key];
total += value;
}
return total;
// return keys.Sum(key => Posibilities[key]);
}
}
DistancePredictionは次のようになります:
public class DistancePrediction
{
public IIRVector3 Velocity { get; set; }
public float DeltaTime { get; set; }
public IIRVector3 Position { get; set; }
}
、最終的には、IIRVector3は次のようになります。
public class IIRVector3
{
public IIRVector3()
{
X = 0;
Y = 0;
Z = 0;
}
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
}
私は定義しようとしているグラムは次のようになりますFlatBuffersの私のスキーマ:
enum FBPredictionMethod
{
Distance = 1,
NGram = 2
}
struct FBIIRVector3
{
X:float;
Y:float;
Z:float;
}
table FBDistancePrediction
{
Velocity:FBIIRVector3;
DeltaTime:float;
Position:FBIIRVector3;
}
table FBGram
{
Pattern:string;
Possibilities: ????
}
table FBNGramPrediction
{
Gram: ????
Probability:float;
Pattern:string;
Total:short;
Order:short;
}
table FBPrediction
{
PredictionMethod:FBPredictionMethod;
NGramPrediction:FBNGramPrediction;
DistancePrediction:FBDistancePrediction;
NGramOrder:short;
}
root_type FlatServerToClientMessage;
がDictionary<char, int>
とFBGramでなければなりません
FBGram.PossibilitiesはKeyValuePair<char, int>
する必要があります...私はすべてが正しいようだと思うが、私は何の辞書のために行うには見当がつかない。
注:グラムのコンストラクタNGramPrediction
のコンストラクタは、パラメータとしてGram
をとりながら、パラメータとしてList<char>
とint
を取ります。
誰かが私のスキーマを助けてくれますか?
KeyValuePair
@Aardapple私の友人、私たちは再び会います!あなたは以前のprotobuffの問題で私を助けた。 これは素晴らしいアイデアです!私はそれを行こう。グラムに問題はありますか? charのリストに[byte]を使用できますか? – pookie
実際、 'char'はC#ではUnicodeなので、' byte'の代わりに 'ushort'が必要な場合があります。 – Aardappel
@Aardappleどのようにコンストラクタを処理するか分かりません。表示されているように、GramはPossibleを生成するcharのリスト(生成するのは簡単ですが、コンストラクタに渡す方法は?)を取ります。NGramPredictionは、KeyValuePairではなく、コンストラクタとしてグラムを取ります。これに対処する方法はありますか? – pookie