Top

ccg_nlpy.core.predicate_argument_view module

import logging

from .view import *

logger = logging.getLogger(__name__)

class PredicateArgumentView(View):
    def __init__(self, view, tokens):
        super(PredicateArgumentView, self).__init__(view, tokens)

        self.predicates = []

        # The hypothesis is that all nodes with no incoming edges are predicates.
        for constituent in self.cons_list:
            if 'incoming_relations' not in constituent or len(constituent['incoming_relations']) == 0:
                self.predicates.append(constituent)

    def get_predicates(self):
        """
        Function to return a list of predicates which are constituents with no incoming relations

        @return: list of predicates
        """
        return self.predicates

    def get_arguments(self, predicate):
        """
        Function to return a list of outgoing relations given a predicate

        @param: predicate, the predicate to get outgoing relations from 
        @return: a list of relations, None if the predicate is invalid
        """
        if self._check_predicate(predicate):
            res = []
            for index in predicate['outgoing_relations']:
               res.append(self.get_relations(index))
            return res
        return None

    def get_predicate_properties(self, predicate):
        """
        Function to get the properties of given predicate

        @param: predicate, the predicate to get properties from
        @return: a dictionary object contains following keys: predicate, SenseNumber
        """
        if self._check_predicate(predicate):
            return predicate['properties']
        return None

    def _check_predicate(self, predicate):
        """
        Helper function to check if predicate is valid

        @param: predicate, the predicate to evaluate
        @return: True if the predicate is valid. False, otherwise
        """
        if predicate not in self.predicates:
            logger.error("Predicate " + predicate + " not found")
            return False
        else:
            return True

Module variables

var logger

Classes

class PredicateArgumentView

class PredicateArgumentView(View):
    def __init__(self, view, tokens):
        super(PredicateArgumentView, self).__init__(view, tokens)

        self.predicates = []

        # The hypothesis is that all nodes with no incoming edges are predicates.
        for constituent in self.cons_list:
            if 'incoming_relations' not in constituent or len(constituent['incoming_relations']) == 0:
                self.predicates.append(constituent)

    def get_predicates(self):
        """
        Function to return a list of predicates which are constituents with no incoming relations

        @return: list of predicates
        """
        return self.predicates

    def get_arguments(self, predicate):
        """
        Function to return a list of outgoing relations given a predicate

        @param: predicate, the predicate to get outgoing relations from 
        @return: a list of relations, None if the predicate is invalid
        """
        if self._check_predicate(predicate):
            res = []
            for index in predicate['outgoing_relations']:
               res.append(self.get_relations(index))
            return res
        return None

    def get_predicate_properties(self, predicate):
        """
        Function to get the properties of given predicate

        @param: predicate, the predicate to get properties from
        @return: a dictionary object contains following keys: predicate, SenseNumber
        """
        if self._check_predicate(predicate):
            return predicate['properties']
        return None

    def _check_predicate(self, predicate):
        """
        Helper function to check if predicate is valid

        @param: predicate, the predicate to evaluate
        @return: True if the predicate is valid. False, otherwise
        """
        if predicate not in self.predicates:
            logger.error("Predicate " + predicate + " not found")
            return False
        else:
            return True

Ancestors (in MRO)

Static methods

def __init__(

self, view, tokens)

Constructor for the view

@param: view, the decoded JSON object containing information of the view tokens, List of tokens in the view

def __init__(self, view, tokens):
    super(PredicateArgumentView, self).__init__(view, tokens)
    self.predicates = []
    # The hypothesis is that all nodes with no incoming edges are predicates.
    for constituent in self.cons_list:
        if 'incoming_relations' not in constituent or len(constituent['incoming_relations']) == 0:
            self.predicates.append(constituent)

def get_arguments(

self, predicate)

Function to return a list of outgoing relations given a predicate

@param: predicate, the predicate to get outgoing relations from @return: a list of relations, None if the predicate is invalid

def get_arguments(self, predicate):
    """
    Function to return a list of outgoing relations given a predicate
    @param: predicate, the predicate to get outgoing relations from 
    @return: a list of relations, None if the predicate is invalid
    """
    if self._check_predicate(predicate):
        res = []
        for index in predicate['outgoing_relations']:
           res.append(self.get_relations(index))
        return res
    return None

def get_con_label(

self, position=None)

Wrapper function to get a list of labels of constituents in the view

@param: position, the index of the specific constituent that user wants label from @return: list of labels of all constituents if position is not given, otherwise return a list contains the label of the constituent at specified position

def get_con_label(self, position=None):
    """
    Wrapper function to get a list of labels of constituents in the view 
    @param: position, the index of the specific constituent that user wants label from 
    @return: list of labels of all constituents if position is not given, 
             otherwise return a list contains the label of the constituent at specified position 
    """
    return self.get_cons(position, "label")

def get_con_position(

self, position=None)

Wrapper function to get a list of positions of constituents in the view in respect to tokens of the text

@param: position, the index of the specific constituent that user wants token position from @return: list of position tuples (start_pos, end_pos) of all constituents if position is not given, otherwise return a list contains the token position of the constituent at specified position

def get_con_position(self, position=None):
    """
    Wrapper function to get a list of positions of constituents in the view in respect to tokens of the text
    @param: position, the index of the specific constituent that user wants token position from
    @return: list of position tuples (start_pos, end_pos) of all constituents if position is not given, 
             otherwise return a list contains the token position of the constituent at specified position 
    """
    return self.get_cons(position, "position")

def get_con_score(

self, position=None)

Wrapper function to get a list of scores of constituents in the view

@param: position, the index of the specific constituent that user wants score from @return: list of scores of all constituents if position is not given, otherwise return a list contains the score of the constituent at specified position

def get_con_score(self, position=None):
    """
    Wrapper function to get a list of scores of constituents in the view
    @param: position, the index of the specific constituent that user wants score from
    @return: list of scores of all constituents if position is not given,
             otherwise return a list contains the score of the constituent at specified position
    """
    return self.get_cons(position, "score")

def get_cons(

self, position=None, key=None)

Function to get a list of constituents in the view

@param: position, the index of the specific constituent that user wants key, the specific key in constituents that user wants ("score", "label", "position","tokens") @return: if key is not given, a list of all constituents if position is not given, or a list contains the constituent at specified position if position is given otherwise, a list of specific key in respect to constituents

def get_cons(self, position=None, key=None):
    """
    Function to get a list of constituents in the view
    @param: position, the index of the specific constituent that user wants
            key, the specific key in constituents that user wants ("score", "label", "position","tokens")
    @return: if key is not given, a list of all constituents if position is not given,
             or a list contains the constituent at specified position if position is given
             otherwise, a list of specific key in respect to constituents
    """
    if self.cons_list is None:
        logger.warn("This view does not have constituents in your input text")
        return None
    if key is None:
        if position is not None and 0 <= position < len(self.cons_list):
            return [self.cons_list[position]]
        else:
            return self.cons_list
    elif key == "score" or key == "label" or key == "position" or key == "tokens":
        result_list = []
        if position is not None and 0 <= position < len(
                self.cons_list):
            if key == "position":
                result_list.append((self.cons_list[position]["start"],
                                    self.cons_list[position]["end"]))
            else:
                result_list.append(self.cons_list[position][key])
        else:
            for constituent in self.cons_list:
                if key == "position":
                    result_list.append(
                        (constituent["start"], constituent["end"]))
                else:
                    result_list.append(constituent[key])
        return result_list
    logger.warn("Invalid key in constituent")
    return None

def get_overlapping_constituents(

self, start_token_index, end_token_index)

Function to get a list of constituents in the view that overlap with the indices provided

@param: start_token_index, the starting index of the range for overlapping end_token_index, the ending index of the range for overlapping @return: List of overlapping constituents if the indice are valid, None otherwise

def get_overlapping_constituents(self, start_token_index, end_token_index):
    """
    Function to get a list of constituents in the view that overlap with the indices provided
    @param: start_token_index, the starting index of the range for overlapping
            end_token_index, the ending index of the range for overlapping
    @return: List of overlapping constituents if the indice are valid, None otherwise
    """
    if start_token_index > end_token_index:
        logger.warn("Invalid token indices given, please provide proper indices.")
        return None
    view_overlapping_span = []
    for cons in self.cons_list:
        if((cons['start'] <= start_token_index and cons['end'] >= start_token_index) or
                (cons['start'] <= end_token_index and cons['end'] >= end_token_index) or
                (cons['start'] >= start_token_index and cons['end'] <= end_token_index) or
                (cons['start'] <= start_token_index and cons['end'] >= end_token_index)):
            view_overlapping_span.append(cons)
    return view_overlapping_span

def get_predicate_properties(

self, predicate)

Function to get the properties of given predicate

@param: predicate, the predicate to get properties from @return: a dictionary object contains following keys: predicate, SenseNumber

def get_predicate_properties(self, predicate):
    """
    Function to get the properties of given predicate
    @param: predicate, the predicate to get properties from
    @return: a dictionary object contains following keys: predicate, SenseNumber
    """
    if self._check_predicate(predicate):
        return predicate['properties']
    return None

def get_predicates(

self)

Function to return a list of predicates which are constituents with no incoming relations

@return: list of predicates

def get_predicates(self):
    """
    Function to return a list of predicates which are constituents with no incoming relations
    @return: list of predicates
    """
    return self.predicates

def get_relations(

self, position=None)

Funtion to get the relation array if the view supports relations

@param: position, the index of the specific relation that user wants @return: list of relations if position is not given, otherwise return a list contains the relation at specified position

def get_relations(self, position=None):
    """
    Funtion to get the relation array if the view supports relations
    @param: position, the index of the specific relation that user wants
    @return: list of relations if position is not given,
             otherwise return a list contains the relation at specified position
    """
    if self.relation_array is None:
        logger.warn("This view does not support relations")
        return None
    else:
        if position is not None and 0 <= position < len(
                self.relation_array):
            return [self.relation_array[position]]
        else:
            return self.relation_array

def get_view_type(

self)

Function to get type of the view

@return view type of the view

def get_view_type(self):
    """
    Function to get type of the view
    @return view type of the view
    """
    return self.view_type

Instance variables

var predicates