Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 减少子类的Pythonic方法_Python 3.x_Design Patterns_Nlp_Pipeline_Composition - Fatal编程技术网

Python 3.x 减少子类的Pythonic方法

Python 3.x 减少子类的Pythonic方法,python-3.x,design-patterns,nlp,pipeline,composition,Python 3.x,Design Patterns,Nlp,Pipeline,Composition,背景:所以,我正在研究一个NLP问题。我需要根据不同类型的文本文档提取不同类型的特征。我目前有一个设置,其中有一个FeatureExtractor基类,它根据不同类型的文档被多次子类化,所有这些文档都计算不同的功能集,并返回一个数据帧作为输出 所有这些子类由一个名为FeatureExtractionRunner的包装类型类进一步调用,该类调用所有子类并计算所有文档上的功能,并返回所有类型文档的输出 问题:这种计算特征的模式会导致很多子类。目前,我有14个子类,因为我有14种类型的文档。它可能会进

背景:所以,我正在研究一个NLP问题。我需要根据不同类型的文本文档提取不同类型的特征。我目前有一个设置,其中有一个FeatureExtractor基类,它根据不同类型的文档被多次子类化,所有这些文档都计算不同的功能集,并返回一个数据帧作为输出

所有这些子类由一个名为FeatureExtractionRunner的包装类型类进一步调用,该类调用所有子类并计算所有文档上的功能,并返回所有类型文档的输出

问题:这种计算特征的模式会导致很多子类。目前,我有14个子类,因为我有14种类型的文档。它可能会进一步扩展。这类太多,无法维护。有没有其他方法可以做到这一点?少子类化

下面是我解释的一些示例代表代码:

from abc import ABCMeta, abstractmethod

class FeatureExtractor(metaclass=ABCMeta):
    #base feature extractor class
    def __init__(self, document):
        self.document = document
        
        
    @abstractmethod
    def doc_to_features(self):
        return NotImplemented
    
    
class ExtractorTypeA(FeatureExtractor):
    #do some feature calculations.....
    
    def _calculate_shape_features(self):
        return None
    
    def _calculate_size_features(self):
        return None
    
    def doc_to_features(self):
        #calls all the fancy feature calculation methods like 
        f1 = self._calculate_shape_features(self.document)
        f2 = self._calculate_size_features(self.document)
        #do some calculations on the document and return a pandas dataframe by merging them  (merge f1, f2....etc)
        data = "dataframe-1"
        return data
    
    
class ExtractorTypeB(FeatureExtractor):
    #do some feature calculations.....
    
    def _calculate_some_fancy_features(self):
        return None
    
    def _calculate_some_more_fancy_features(self):
        return None
    
    def doc_to_features(self):
        #calls all the fancy feature calculation methods
        f1 = self._calculate_some_fancy_features(self.document)
        f2 = self._calculate_some_more_fancy_features(self.document)
        #do some calculations on the document and return a pandas dataframe (merge f1, f2 etc)
        data = "dataframe-2"
        return data
    
class ExtractorTypeC(FeatureExtractor):
    #do some feature calculations.....
    
    def doc_to_features(self):
        #do some calculations on the document and return a pandas dataframe
        data = "dataframe-3"
        return data

class FeatureExtractionRunner:
    #a class to call all types of feature extractors 
    def __init__(self, document, *args, **kwargs):
        self.document = document
        self.type_a = ExtractorTypeA(self.document)
        self.type_b = ExtractorTypeB(self.document)
        self.type_c = ExtractorTypeC(self.document)
        #more of these extractors would be there
        
    def call_all_type_of_extractors(self):
        type_a_features = self.type_a.doc_to_features()
        type_b_features = self.type_b.doc_to_features()
        type_c_features = self.type_c.doc_to_features()
        #more such extractors would be there....
        
        return [type_a_features, type_b_features, type_c_features]
        
        
all_type_of_features = FeatureExtractionRunner("some document").call_all_type_of_extractors()

首先回答这个问题,您可以避免完全以每次编写
\uuu init\uu
方法为代价进行子类化。或者您可以完全摆脱类,将它们转换为一组函数。甚至你也可以在一个班级里加入所有的班级。请注意,这些方法都不会使代码更简单或更易于维护,实际上它们只是在某种程度上改变了它的形状

这种情况是一个完美的例子,我的意思是领域(NLP)和特定用例(文档特征提取)本身是复杂的

例如,featureXfeatureY可能是完全不同的东西,无法完全计算,因此每个方法都有一个结果。类似地,在数据帧中合并这些功能的过程可能不同于合并奇特功能的过程。在这种情况下拥有很多函数/类对我来说是完全合理的,而且将它们分开是合乎逻辑的,也是可维护的

也就是说,如果你能将一些
特性计算方法
组合成一个更通用的函数,那么真正的代码缩减可能是可能的,但我不能肯定这是否可能