加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

拆分或不拆分类(用Java)

发布时间:2020-12-15 08:30:17 所属栏目:Java 来源:网络整理
导读:我有一个句子,在不同的阶段进行分析.首先,我得到一些属性(比如X,Y,Z): public class AnalyzedSentence { private String X; private String Y; private String Z; public AnalyzedSentence(String sentence) { extractX(); extractY(); extractZ(); } // ge
我有一个句子,在不同的阶段进行分析.首先,我得到一些属性(比如X,Y,Z):

public class AnalyzedSentence {
    private String X;
    private String Y;
    private String Z;

    public AnalyzedSentence(String sentence) {
        extractX();
        extractY();
        extractZ();
    }

    // getters,setters
}

然后,我使用这些属性来进一步分析句子以获得另一个属性,比如“XYZ”,之后我创建了以下类:

public class FinalSentence {

    private AnalyzedSentence data;

    private String XYZ;

    public FinalSentence(String XYZ,AnalyzedSentence data) {
        this.data = data;
        this.XYZ = XYZ;
    }

    // getters,setters
}

工作流程如下:

public class SentenceAnalyzer {
    /// ...
    public FinalSentence analyze(String sentence) {
        AnalyzedSentence as = new AnalyzedSentence(sentence);  // every attribute of "as" can be calculated beforehand
        String XYZ = SpecialClass.extractXYZ(sentence,as); // extract XYZ (needs a special class),based on as
        return new FinalSentence(XYZ,as);
    }
}

或者,我可以只有一个类保存所有信息,在提取时填充属性,这可能会导致一些空结果.它是这样的:

public class Sentence {

    private String X;
    private String Y;
    private String Z;    
    private String XYZ;

    public Sentence(String sentence) {
        extractX();
        extractY();
        extractZ();
    }

    public String getXYZ() {
        // with this design,this method can be called,even if XYZ was not extracted yet.
        // remember that XYZ cannot be extracted as X,Z
    }

    public void setXYZ(...) {...}

    // getters,setters
}

我的问题是:哪种设计更受欢迎,为什么?如果还有更好的方法来完成我在这里尝试做的事情,我也想听听它.

解决方法

您需要考虑的是,在您的问题域中,AnalyzedSentence和FinalSentence是否足够独特,可以拆分或合并.

很明显,他们正在处理类似的数据并密切合作以实现目标.

对我来说,分析和最终只是说句子可以进入,虽然这是基于我对你正在研究的问题的有限知识,所以我希望以某种方式将它们结合起来.

编辑
根据更多信息,我想我会设计如下:

Sentence类封装了原始句子,标签和提取的类别(或者你正在提取的任何内容,我假设它是基于你的描述的类别),以及设置,获取和提取信息的操作.

Sentence类存储TagList,其中包含所有标记,原始字符串和提取的类别.它还通过创建一个Extractor来封装数据的提取,并在数据需要提取时将它传递给TagList(我把它放在构造函数中,但是它可以放在一个方法中,调用它取决于你需要什么时候提取数据).

因此,通过这种方式,操作原始句子所需的一切都在Sentence类中.当然,你可能知道一些我不这样做的东西,这使得这种方法不合适,但这里有一些代码来说明我的意思:

public class Sentence {

    private TagList tags    
    private String category;
    private String sentence

    public Sentence(String newSentence) {
        sentence = newSentence;
        Extractor<TagList> e = new Extractor<TagList>()
        tags = e.extractTags(sentence);
        category = new Category(tags);
    }

    public String getXYZ() {

    }

    public void setXYZ(...) {...}

    private extractTags(String s){ ...}

    // getters,setters
}


public class TagList{

    private List<String> tags;

    ....
    //rest of class definition

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读