Esempio n. 1
0
class ExpressionTreeEmbedder:
    def __init__(self):

        self.exprTreeProvider = ExpressionTreeProvider()
        self.trackingInfoProvider = TrackingInfoProvider()
        self.treeToExprConverter = TreeToExpressionConverter()

    def embed(self, context, symbol):
        return self._termDocumentMatrixFromContext(context, symbol)

    def _termDocumentMatrixFromContext(self, context, symbol):

        x = self._termDictsFromContext(context, symbol)
        if x == None: return None
        (vecs, allNgrams) = x

        self.nameDictMapToMatrix = NameDictMapToMatrix()
        self.nameDictMapToMatrix.convertFromDicts(vecs, allNgrams)
        termDocMatrix = self.nameDictMapToMatrix.termDocumentMatrix
        return termDocMatrix

    def _termDictsFromContext(self, context, symbol):

        vecs = NameToDictMap()
        allNgrams = OccurrenceCounter()

        context.neighbours.append(context.origin)

        for neighbour in context.neighbours:
            nOcc = neighbour.nOccurrences
            location = neighbour.location
            expressions = self.treeToExprConverter.getExpressionsForSymbol(
                location, symbol)
            # expressions.append('@+$_+@')
            # expressions.append('@+EXPR@+$_+@+@')

            # print 'FOO %s: %s: %s' % (symbol, location, expressions)

            neighbour.setExpressions(expressions)

            # add null-vector for function if it does not contain expressions
            if len(expressions) == 0:
                vecs.add(None, location)

            for expr in expressions:
                # vecs.add(expr, location, 1.0/nOcc)
                # vecs.add(expr, location, 1.0)
                vecs.setItem(expr, location, 1.0)
                allNgrams.add(expr)

        context.neighbours.pop()

        if len(vecs.d) == 0 or len(allNgrams.d) == 0:
            return None

        return (vecs, allNgrams)

    def getAllConditionNodes(self):
        return self.tree.conditionalNodes
class ExpressionTreeEmbedder:
    def __init__(self):
    
        self.exprTreeProvider = ExpressionTreeProvider()
        self.trackingInfoProvider = TrackingInfoProvider()
        self.treeToExprConverter = TreeToExpressionConverter()
    
    def embed(self, context, symbol):
        return self._termDocumentMatrixFromContext(context, symbol)
    
    def _termDocumentMatrixFromContext(self, context, symbol):
        
        x = self._termDictsFromContext(context, symbol)
        if x == None: return None
        (vecs, allNgrams)= x
                
        self.nameDictMapToMatrix = NameDictMapToMatrix()
        self.nameDictMapToMatrix.convertFromDicts(vecs, allNgrams)
        termDocMatrix = self.nameDictMapToMatrix.termDocumentMatrix
        return termDocMatrix

    def _termDictsFromContext(self, context, symbol):
        
        vecs = NameToDictMap()
        allNgrams = OccurrenceCounter()
        
        context.neighbours.append(context.origin)
        
        for neighbour in context.neighbours:
            nOcc = neighbour.nOccurrences
            location = neighbour.location
            expressions = self.treeToExprConverter.getExpressionsForSymbol(location, symbol)
            # expressions.append('@+$_+@')
            # expressions.append('@+EXPR@+$_+@+@')
            
            # print 'FOO %s: %s: %s' % (symbol, location, expressions)
            
            neighbour.setExpressions(expressions)

            # add null-vector for function if it does not contain expressions
            if len(expressions) == 0:
                vecs.add(None, location)
            
            for expr in expressions:
                # vecs.add(expr, location, 1.0/nOcc)
                # vecs.add(expr, location, 1.0)
                vecs.setItem(expr, location, 1.0)
                allNgrams.add(expr)            
        
        context.neighbours.pop()
        
        if len(vecs.d) == 0 or len(allNgrams.d) == 0:
            return None
    
        return (vecs, allNgrams)
                   
    def getAllConditionNodes(self):
        return self.tree.conditionalNodes
 def __init__(self):
 
     self.exprTreeProvider = ExpressionTreeProvider()
     self.trackingInfoProvider = TrackingInfoProvider()
     self.treeToExprConverter = TreeToExpressionConverter()
Esempio n. 4
0
    def __init__(self):

        self.exprTreeProvider = ExpressionTreeProvider()
        self.trackingInfoProvider = TrackingInfoProvider()
        self.treeToExprConverter = TreeToExpressionConverter()