C#中如何表示共轭表 [英] How to Represent Conjugation Tables in C#

查看:163
本文介绍了C#中如何表示共轭表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为法语文本设计语言分析器。我有一个XML格式的字典,如下所示:

 <?xml version =1.0encoding =utf -8? 
<字典>
<! - 这是字典中每个条目的基本结构。给出属性值为
作为属性的说明。虽然这是每个单词的成品的结构,但定义,上下文和上下文示例将被忽略,因为它们在此时对应用程序没有真正的影响。 Defini - >
< Word word =字典中的单词(将被定义的任何单词)。吸气=这个词是否以吸气h开始。在以非吸气h开头的单词之前的一些形容词有一个额外的形式(形容词 - & gt; na [非吸引])。
< GrammaticalForm form =这个词的语法形式是使用它的语法语境,形式可以由名词,形容词,副词,感叹词或其他形式的单词组成,每种形式(一般)它自己的定义,因为单词的含义以其使用的方式改变。>
< definition definition =>< / Definition>
< / GrammaticalForm>
< ConjugationTables>
< NounForms ms =名词的男性风格单数形式。 fs =名词的女性单数形式。 mpl =名词的男性主义复数形式。 fpl =女名词复数形式的名词。 gender =名词的性别,确定>< / NounForms>
< AdjectiveForms ms =形容词的男性风格单数形式。 fs =形容词的女性单数形式。 mpl =形容词的男性主义复数形式。 fpl =女性复数形式的形容词。 na =形容词的非抽象形式,在形容词之后是非抽象词语的情况下。 location =形容词放在名词前面(之前,之后或之后)>< / AdjectiveForms>
< VerbForms group =动词所属的组(第1,第2,第3或异常)。 auxillary =动词采用的辅助动词。介词=这个动词使用的有效介词的CSV列表;用于语法分析。 transitive =动词是否可传递。 pronominal =动词的代词无限形式,如果动词允许代词构造。>
<指示性>
< Present fps =(Je)first person singular。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / Present>
< SimplePast fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / SimplePast>
< PresentPerfect fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / PresentPerfect>
< PastPerfect fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / PastPerfect>
<不完美的fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / Imperfect>
< Pluperfect fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / Pluperfect>
<未来fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / Future>
< PastFuture fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / PastFuture>
< /指示>
< Subjunctive>
< Present fps =(Je)first person singular。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / Present>
<过去fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)third person plural。>< / Past>
<不完美的fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / Imperfect>
< Pluperfect fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / Pluperfect>
< / Subjunctive>
<条件>
< Present fps =(Je)first person singular。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / Present>
< FirstPast fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / FirstPast>
< SecondPast fps =(Je)第一人称单数。 sps =(Tu)第二人称单数。 tps =(Il)第三人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)第二人称复数。 tpp =(Ils)第三人称复数。>< / SecondPast>
< /条件>
<命令式>
< Present sps =(Tu)第二人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)second person plural。>< / Present>
<过去sps =(Tu)第二人称单数。 fpp =(Nous)第一人称复数。 spp =(Vous)second person plural。>< / Past>
< /命令>
< Infinitive present =动词的当前不定式。 past =动词的过去无限形式>< / Infinitive>
< Participle present =动词的现在分词。 past =动词的过去部分。>< / Participle>
< / VerbForms>
< / ConjugationTables>
< / Word>
< / Dictionary>

对不起这么久,但有必要准确地显示数据建模(树结构结构)



目前我正在使用 structs 对共轭表进行建模,嵌套 structs 更具体。以下是我创建的类以在XML文件中为单个条目建立什么:

  class Word 
{
public string word {get;组; }
public bool aspirate {get;组; }
public List&GrammaticalForms>形式{get;组; }

struct GrammaticalForms
{
public string form {get;组; }
public string definition {get;组; }
}

struct NounForms
{
public string gender {get;组; }
public string masculinSingular {get;组; }
public string femininSingular {get;组; }
public string masculinPlural {get;组; }
public string femininPlural {get;组; }
}

struct AdjectiveForms
{
public string masculinSingular {get;组; }
public string femininSingular {get;组; }
public string masculinPlural {get;组; }
public string femininPlural {get;组; }
public string nonAspirate {get;组; }
public string location {get;组; }
}

struct VerbForms
{
public string group {get;组; }
public string辅助{get;组; }
public string []介词{get;组; }
public bool transitive {get;组; }
public string pronominalForm {get;组; }

struct IndicativePresent
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组;
}
struct IndicativeSimplePast
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}
struct IndicativePresentPerfect
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}
struct IndicativePastPerfect
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组;
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}
struct IndicativeImperfect
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组;
}
struct IndicativePluperfect
{
public string firstPersonSingular {get;组;
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}
struct IndicativeFuture
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组;
}
struct IndicativePastFuture
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}

struct SubjunctivePresent
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}
struct SubjunctivePast
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}
struct SubjunctiveImperfect
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}
struct SubjunctivePluperfect
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}

struct ConditionalPresent
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}
struct ConditionalFirstPast
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}
struct ConditionalSecondPast
{
public string firstPersonSingular {get;组; }
public string secondPersonSingular {get;组; }
public string thirdPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
public string thirdPersonPlural {get;组; }
}

struct ImperativePresent
{
public string secondPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组; }
}
struct ImperativePast
{
public string secondPersonSingular {get;组; }
public string firstPersonPlural {get;组; }
public string secondPersonPlural {get;组;
}

struct Infinitive
{
public string present {get;组; }
public string past {get;组; }
}

struct Participle
{
public string present {get;组; }
public string past {get;组; }
}
}
}






我是C#的新手,我不太熟悉数据结构。基于我对C ++的有限知识,我知道当您正在建模小型,高度相关的数据时, structs 是有用的,这就是为什么我目前在此使用它们时尚。



所有这些结构都可以实际上被构建成一个 ConjugationTables class 并将在很大程度上具有相同的结构。我不确定是将这些变成一个类,还是使用不同的数据结构来更好地适应这个问题。为了给出关于问题规范的更多信息,我将会说出以下几点:


  1. 一旦从XML中加载了这些值

  2. 这些值将经常读取/读取

  3. 表格结构必须维护 - 也就是说 IndicativePresent 必须嵌套在 VerbForms ;这同样适用于作为 VerbForms struct成员的所有其他结构体。这些是共轭毕竟!

  4. 也许是最重要的:我需要组织数据在例如,如果XML文件中的例如 Word 不具有 GrammaticalForm 动词 ,实际上将不会为该条目创建 VerbForms struct。这是为了提高效率 - 为什么实例化 VerbForms 如果这个词实际上不是动词?避免不必要地创建这些表单表(这些表格现在被表示为 struct XXXXXForms )的想法是绝对必要的。






根据(主要)点#4 ,最好使用哪种数据结构建模共轭(而不是数据库表)?为了符合#4,我需要更改我的数据格式吗?如果我实例化一个新的Word ,结构体在当前状态下是否也被实例化并占用很多空间?这是一些数学...在Googling之后,最终找到这个问题 ..



在所有共轭表(名词,形容词,动词)中,总共有(巧合?) 100 string 被分配,并且是空的。对于每个 Word 100 x 18 bytes = 1800字节,至少如果这些数据结构被创建并保持为空(对于实际填充的值,始终至少有一些开销)。所以假设(只是随机地,可能或多或少) 50,000 Word s需要在内存中,这是 9000万字节,或大约 85.8307 兆字节。



这是一个很大的开销只是为了有空表。那么我可以把这些数据放在一起,让我只能根据 实现某些表(名词,形容词,动词) GrammaticalForms Word 实际上有(在XML文件中)。



我想要这些表成为 Word 类的成员,但只能实例化我需要的表。我不能想到它的一种方式,现在我在结构体上做了数学我知道这不是一个很好的解决方案。我的第一个想法是为每种类型的 NounForms AdjectiveForms VerbForms ,如果表单出现在XML文件中,则实例化该类。我不知道这是否正确...



任何建议?

解决方案

建议:


  1. 我会切换到使用课程。

  2. '以单数形式表示该类,并通过属性名称表示基数。

  3. 我将从类定义中删除嵌套。

  4. 您可以介绍一个ConjugationForm抽象类,具有多个ConjugationForm子类(NounForm,AdjectiveForm,abstract VerbForm,然后是所有的VerbForm子类 - IndicativePresent,IndicativeSimplePast等)Word类然后具有ConjugationForm实例的数据结构,也许 List< ConjugationForm>共轭表{get;设置;}

至于内存/ GC压力,你实际测量了?我建议您编写一些东西,并进行实际测试,以验证您是否会遇到GC问题,而不是试图猜测或作出假设。 GC已经很好地优化了分配和取消分配大量小对象,但长期存在和/或大对象可能会导致问题。


I'm designing a linguistic analyzer for French text. I have a dictionary in XML format, that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Dictionary>
  <!--This is the base structure for every entry in the dictionary. Values on attributes are given
    as explanations for the attributes. Though this is the structure of the finished product for each word, definition, context and context examples will be ommitted as they don't have a real effect on the application at this moment. Defini-->
  <Word word="The word in the dictionary (any word that would be defined)." aspirate="Whether or not the word starts with an aspirate h. Some adjectives that come before words that start with a non-aspirate h have an extra form (AdjectiveForms -&gt; na [non-aspirate]).">
      <GrammaticalForm form="The grammatical form of the word is the grammatical context in which it is used. Forms may consist of a word in noun, adjective, adverb, exclamatory or other form. Each form (generally) has its own definition, as the meaning of the word changes in the way it is used.">
        <Definition definition=""></Definition>
      </GrammaticalForm>
    <ConjugationTables>
      <NounForms ms="The masculin singular form of the noun." fs="The feminin singular form of the noun." mpl="The masculin plural form of the noun." fpl="The feminin plural form of the noun." gender="The gender of the noun. Determines"></NounForms>
      <AdjectiveForms ms="The masculin singular form of the adjective." fs="The feminin singular form of the adjective." mpl="The masculin plural form of the adjective." fpl="The feminin plural form of the adjective." na="The non-aspirate form of the adjective, in the case where the adjective is followed by a non-aspirate word." location="Where the adjective is placed around the noun (before, after, or both)."></AdjectiveForms>
      <VerbForms group="What group the verb belongs to (1st, 2nd, 3rd or exception)." auxillary="The auxillary verb taken by the verb." prepositions="A CSV list of valid prepositions this verb uses; for grammatical analysis." transitive="Whether or not the verb is transitive." pronominal="The pronominal infinitive form of the verb, if the verb allows pronominal construction.">
        <Indicative>
          <Present fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Present>
          <SimplePast fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></SimplePast>
          <PresentPerfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></PresentPerfect>
          <PastPerfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></PastPerfect>
          <Imperfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Imperfect>
          <Pluperfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Pluperfect>
          <Future fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Future>
          <PastFuture fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></PastFuture>
        </Indicative>
        <Subjunctive>
          <Present fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Present>
          <Past fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Past>
          <Imperfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Imperfect>
          <Pluperfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Pluperfect>
        </Subjunctive>
        <Conditional>
          <Present fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Present>
          <FirstPast fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></FirstPast>
          <SecondPast fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></SecondPast>
        </Conditional>
        <Imperative>
          <Present sps="(Tu) second person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural."></Present>
          <Past sps="(Tu) second person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural."></Past>
        </Imperative>
        <Infinitive present="The present infinitive form of the verb." past="The past infinitive form of the verb."></Infinitive>
        <Participle present="The present participle of the verb." past="The past partciple of the verb."></Participle>
      </VerbForms>
    </ConjugationTables>
  </Word>
</Dictionary>

Sorry it's so long, but it's necessary to show exactly how the data is modeled (tree-node structure).

Currently I am using structs to model the conjugation tables, nested structs to be more specific. Here is the class I created to model what is a single entry in the XML file:

class Word
{
    public string word { get; set; }
    public bool aspirate { get; set; }
    public List<GrammaticalForms> forms { get; set; }

    struct GrammaticalForms
    {
        public string form { get; set; }
        public string definition { get; set; }
    }

    struct NounForms
    {
        public string gender { get; set; }
        public string masculinSingular { get; set; }
        public string femininSingular { get; set; }
        public string masculinPlural { get; set; }
        public string femininPlural { get; set; }
    }

    struct AdjectiveForms
    {
        public string masculinSingular { get; set; }
        public string femininSingular { get; set; }
        public string masculinPlural { get; set; }
        public string femininPlural { get; set; }
        public string nonAspirate { get; set; }
        public string location { get; set; }
    }

    struct VerbForms
    {
        public string group { get; set; }
        public string auxillary { get; set; }
        public string[] prepositions { get; set; }
        public bool transitive { get; set; }
        public string pronominalForm { get; set; }

        struct IndicativePresent
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct IndicativeSimplePast
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct IndicativePresentPerfect
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct IndicativePastPerfect
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct IndicativeImperfect
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct IndicativePluperfect
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct IndicativeFuture
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct IndicativePastFuture
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }

        struct SubjunctivePresent
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct SubjunctivePast
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct SubjunctiveImperfect
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct SubjunctivePluperfect
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }

        struct ConditionalPresent
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct ConditionalFirstPast
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }
        struct ConditionalSecondPast
        {
            public string firstPersonSingular { get; set; }
            public string secondPersonSingular { get; set; }
            public string thirdPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
            public string thirdPersonPlural { get; set; }
        }

        struct ImperativePresent
        {
            public string secondPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
        }
        struct ImperativePast
        {
            public string secondPersonSingular { get; set; }
            public string firstPersonPlural { get; set; }
            public string secondPersonPlural { get; set; }
        }

        struct Infinitive
        {
            public string present { get; set; }
            public string past { get; set; }
        }

        struct Participle
        {
            public string present { get; set; }
            public string past { get; set; }
        }
    }
}


I'm new to C#, and I'm not too familiar with the data structures. Based on my limited knowledge of C++, I know that structs are useful when you are modeling small, highly-related pieces of data, which is why I am currently using them in this fashion.

All of these structs could realistically be made into a ConjugationTables class, and would have, to a high degree, the same structure. I'm unsure of whether to make these into a class, or use a different data structure that would be better suited to the problem. In order to give some more information about the problem specifications, I'll say the following:

  1. Once these values have been loaded from the XML file, they will not be changed.
  2. These values will be read/fetched very often.
  3. The table-like structure must be maintained - that is to say that IndicativePresent must be nested under VerbForms; the same applies to all other structs that are members of the VerbForms struct. These are conjugation tables after all!
  4. Perhaps the most important: I need the organization of the data to be set up in a way that, if for example a Word in the XML file does not have a GrammaticalForm of verb, that no VerbForms struct will actually be created for that entry. This is in an effort to improve efficiency - why instantiate VerbForms if the word is not actually a verb? This idea of avoiding unnecessary creation of these "forms" tables (which are currently represented as struct XXXXXForms) is absolutely imperative.


In accordance with (primarily) point #4 above, what kinds of data structures would be best used in modeling conjugation tables (not database tables)? Do I need to change the format of my data in order to be compliant with #4? If I instantiate a new Word, will the structs, in their current state, be instantiated as well and take up a lot of space? Here's some math... after Googling around and eventually finding this question...

In all the conjugation tables (nouns, adjectives, verbs), there are a total of (coincidence?) 100 strings allocated, and that are empty. So 100 x 18 bytes = 1800 bytes for each Word, at minimum, if these data structures are created and remain empty (there will always be at least some overhead for the values that would actually be filled in). So assuming (just randomly, could be more or less) 50,000 Words that would need to be in memory, that's 90 million bytes, or approximately 85.8307 megabytes.

That's a lot of overhead just to have empty tables. So what is a way that I can put this data together to allow me to instantiate only certain tables (noun, adjective, verb) depending on what GrammaticalForms the Word actually has (in the XML file).

I want these tables to be a member of the Word class, but only instantiate the tables that I need. I can't think of a way around it, and now that I did the math on the structs I know that it's not a good solution. My first thought is to make a class for each type of NounForms, AdjectiveForms, and VerbForms, and instantiate the class if the form appears in the XML file. I'm not sure if that is correct though...

Any suggestions?

解决方案

Suggestions:

  1. I'd switch to using class.
  2. I'd name the class in the singular and express cardinality through the property name.
  3. I'd remove the nesting from the class definitions.
  4. It looks like you can introduce a ConjugationForm abstract class, have multiple ConjugationForm subclasses (NounForm, AdjectiveForm, abstract VerbForm, then all the VerbForm subclasses - IndicativePresent, IndicativeSimplePast, etc.) The Word class then has a data structure for the ConjugationForm instances, maybe List<ConjugationForm> conjugationTable {get; set;}

As for the memory / GC pressure, have you actually measured how bad the situation is? I'd recommend coding up something and actually testing to verify whether you're going to experience GC problems, rather than trying to guess or make an assumption. The GC is pretty well optimized to allocate and de-allocate a lot of small objects, but long lived and/or large objects might cause you problems.

这篇关于C#中如何表示共轭表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆