特征向量生成器 - 有哪些可用的设计模式?

3
我的问题看起来很简单,但只是乍一看(对我而言:P)。 我需要创建一个构建特征向量的类。这个特征向量表示文本。特征如:平均字长,整个文本中的句子数等。 有些特征可以在计算其他特征时提取,这就是为什么我稍微修改了Builder设计模式,它看起来像这样: 我正在创建一个builder对象:
FeatureVectorBuilder fvb = new FeatureVectorBuilder(String text/InputStream <- now it doesn't matter) 

然后我会指定一个顺序,它表明我想要包括哪些功能。
fvb.setLenghtWord(True) <- for fixed length features
fvb.setXXXFeature(32) <- for variable length features

接下来我要创建这个向量:

fvb.buildFeatureVector() <- this way computations are optimized;

最后,我有一个FeatureVector需要获取。
fvb.getFeatureVector();

一切看起来都不错,但是...有大约32个不同的特性要设置...
这样,悲观情况需要调用32个函数,同时创建一个有32个参数的函数看起来很愚蠢。

我想知道是否有人遇到过这样的问题,也许有比“32种方法”更好的解决方案 :)


1
创建8个函数,每个函数有4个参数? :) 有时候工作就是工作,无法避免。 - irreputable
2个回答

0

将特性封装的一种可能的干净方式是:

abstract class Feature
{
    String name;
    ...
}

class NumericFeature extends Feature
{
    int value;
}

class OtherFeatureType extends Feature
{
    ....
}

Feature[] features = new Feature[] {
    new NumericFeature("xxxFeature", 32),
    new OtherFeature("feature1", ...),
    ...
};

FeatureVectorBuilder fvb = new FeatureVectorBuilder(text, features);

0

建造者模式的一个要点是通过用多个方法替换具有大量参数的方法来避免这种情况。如果您有32个可能的特性,那么在建造者中拥有32个方法对我来说看起来很正常。

另一种可能性是将每个特性设计为一个类,并在您的建造者中添加这些类的实例:

builder.addFeature(new LengthWordFeature(true))
       .addFeature(new XxxFeature(32));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接