Python nltk.compat.xrange() Examples

The following are code examples for showing how to use nltk.compat.xrange(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: razzy-spinner   Author: rafasashi   File: segmentation.py    GNU General Public License v3.0 5 votes vote down vote up
def pk(ref, hyp, k=None, boundary='1'):
    """
    Compute the Pk metric for a pair of segmentations A segmentation
    is any sequence over a vocabulary of two items (e.g. "0", "1"),
    where the specified boundary value is used to mark the edge of a
    segmentation.

    >>> '%.2f' % pk('0100'*100, '1'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0100'*100, 2)
    '0.00'

    :param ref: the reference segmentation
    :type ref: str or list
    :param hyp: the segmentation to evaluate
    :type hyp: str or list
    :param k: window size, if None, set to half of the average reference segment length
    :type boundary: str or int or bool
    :param boundary: boundary value
    :type boundary: str or int or bool
    :rtype: float
    """

    if k is None:
        k = int(round(len(ref) / (ref.count(boundary) * 2.)))

    err = 0
    for i in xrange(len(ref)-k +1):
        r = ref[i:i+k].count(boundary) > 0
        h = hyp[i:i+k].count(boundary) > 0
        if r != h:
           err += 1
    return err / (len(ref)-k +1.)


# skip doctests if numpy is not installed 
Example 2
Project: razzy-spinner   Author: rafasashi   File: earleychart.py    GNU General Public License v3.0 5 votes vote down vote up
def _positions(self):
        return xrange(self.num_leaves() + 1) 
Example 3
Project: razzy-spinner   Author: rafasashi   File: featurechart.py    GNU General Public License v3.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in xrange(chart.num_leaves() + 1):
                new_edge = FeatureTreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


#////////////////////////////////////////////////////////////
# Feature Chart Parser
#//////////////////////////////////////////////////////////// 
Example 4
Project: razzy-spinner   Author: rafasashi   File: chart.py    GNU General Public License v3.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in compat.xrange(chart.num_leaves() + 1):
                new_edge = TreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


########################################################################
##  Filtered Bottom Up
######################################################################## 
Example 5
Project: OpenBottle   Author: xiaozhuchacha   File: segmentation.py    MIT License 5 votes vote down vote up
def pk(ref, hyp, k=None, boundary='1'):
    """
    Compute the Pk metric for a pair of segmentations A segmentation
    is any sequence over a vocabulary of two items (e.g. "0", "1"),
    where the specified boundary value is used to mark the edge of a
    segmentation.

    >>> '%.2f' % pk('0100'*100, '1'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0100'*100, 2)
    '0.00'

    :param ref: the reference segmentation
    :type ref: str or list
    :param hyp: the segmentation to evaluate
    :type hyp: str or list
    :param k: window size, if None, set to half of the average reference segment length
    :type boundary: str or int or bool
    :param boundary: boundary value
    :type boundary: str or int or bool
    :rtype: float
    """

    if k is None:
        k = int(round(len(ref) / (ref.count(boundary) * 2.)))

    err = 0
    for i in xrange(len(ref)-k +1):
        r = ref[i:i+k].count(boundary) > 0
        h = hyp[i:i+k].count(boundary) > 0
        if r != h:
           err += 1
    return err / (len(ref)-k +1.)


# skip doctests if numpy is not installed 
Example 6
Project: OpenBottle   Author: xiaozhuchacha   File: earleychart.py    MIT License 5 votes vote down vote up
def _positions(self):
        return xrange(self.num_leaves() + 1) 
Example 7
Project: OpenBottle   Author: xiaozhuchacha   File: featurechart.py    MIT License 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in xrange(chart.num_leaves() + 1):
                new_edge = FeatureTreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


#////////////////////////////////////////////////////////////
# Feature Chart Parser
#//////////////////////////////////////////////////////////// 
Example 8
Project: OpenBottle   Author: xiaozhuchacha   File: chart.py    MIT License 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in compat.xrange(chart.num_leaves() + 1):
                new_edge = TreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


########################################################################
##  Filtered Bottom Up
######################################################################## 
Example 9
Project: OpenBottle   Author: xiaozhuchacha   File: segmentation.py    MIT License 5 votes vote down vote up
def pk(ref, hyp, k=None, boundary='1'):
    """
    Compute the Pk metric for a pair of segmentations A segmentation
    is any sequence over a vocabulary of two items (e.g. "0", "1"),
    where the specified boundary value is used to mark the edge of a
    segmentation.

    >>> '%.2f' % pk('0100'*100, '1'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0100'*100, 2)
    '0.00'

    :param ref: the reference segmentation
    :type ref: str or list
    :param hyp: the segmentation to evaluate
    :type hyp: str or list
    :param k: window size, if None, set to half of the average reference segment length
    :type boundary: str or int or bool
    :param boundary: boundary value
    :type boundary: str or int or bool
    :rtype: float
    """

    if k is None:
        k = int(round(len(ref) / (ref.count(boundary) * 2.)))

    err = 0
    for i in xrange(len(ref)-k +1):
        r = ref[i:i+k].count(boundary) > 0
        h = hyp[i:i+k].count(boundary) > 0
        if r != h:
           err += 1
    return err / (len(ref)-k +1.)


# skip doctests if numpy is not installed 
Example 10
Project: OpenBottle   Author: xiaozhuchacha   File: featurechart.py    MIT License 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in xrange(chart.num_leaves() + 1):
                new_edge = FeatureTreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


#////////////////////////////////////////////////////////////
# Feature Chart Parser
#//////////////////////////////////////////////////////////// 
Example 11
Project: OpenBottle   Author: xiaozhuchacha   File: chart.py    MIT License 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in compat.xrange(chart.num_leaves() + 1):
                new_edge = TreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


########################################################################
##  Filtered Bottom Up
######################################################################## 
Example 12
Project: FancyWord   Author: EastonLee   File: segmentation.py    GNU General Public License v3.0 5 votes vote down vote up
def pk(ref, hyp, k=None, boundary='1'):
    """
    Compute the Pk metric for a pair of segmentations A segmentation
    is any sequence over a vocabulary of two items (e.g. "0", "1"),
    where the specified boundary value is used to mark the edge of a
    segmentation.

    >>> '%.2f' % pk('0100'*100, '1'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0100'*100, 2)
    '0.00'

    :param ref: the reference segmentation
    :type ref: str or list
    :param hyp: the segmentation to evaluate
    :type hyp: str or list
    :param k: window size, if None, set to half of the average reference segment length
    :type boundary: str or int or bool
    :param boundary: boundary value
    :type boundary: str or int or bool
    :rtype: float
    """

    if k is None:
        k = int(round(len(ref) / (ref.count(boundary) * 2.)))

    err = 0
    for i in xrange(len(ref)-k +1):
        r = ref[i:i+k].count(boundary) > 0
        h = hyp[i:i+k].count(boundary) > 0
        if r != h:
           err += 1
    return err / (len(ref)-k +1.)


# skip doctests if numpy is not installed 
Example 13
Project: FancyWord   Author: EastonLee   File: earleychart.py    GNU General Public License v3.0 5 votes vote down vote up
def _positions(self):
        return xrange(self.num_leaves() + 1) 
Example 14
Project: FancyWord   Author: EastonLee   File: featurechart.py    GNU General Public License v3.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in xrange(chart.num_leaves() + 1):
                new_edge = FeatureTreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


#////////////////////////////////////////////////////////////
# Feature Chart Parser
#//////////////////////////////////////////////////////////// 
Example 15
Project: FancyWord   Author: EastonLee   File: chart.py    GNU General Public License v3.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in compat.xrange(chart.num_leaves() + 1):
                new_edge = TreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


########################################################################
##  Filtered Bottom Up
######################################################################## 
Example 16
Project: honours_project   Author: JFriel   File: segmentation.py    GNU General Public License v3.0 5 votes vote down vote up
def pk(ref, hyp, k=None, boundary='1'):
    """
    Compute the Pk metric for a pair of segmentations A segmentation
    is any sequence over a vocabulary of two items (e.g. "0", "1"),
    where the specified boundary value is used to mark the edge of a
    segmentation.

    >>> '%.2f' % pk('0100'*100, '1'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0100'*100, 2)
    '0.00'

    :param ref: the reference segmentation
    :type ref: str or list
    :param hyp: the segmentation to evaluate
    :type hyp: str or list
    :param k: window size, if None, set to half of the average reference segment length
    :type boundary: str or int or bool
    :param boundary: boundary value
    :type boundary: str or int or bool
    :rtype: float
    """

    if k is None:
        k = int(round(len(ref) / (ref.count(boundary) * 2.)))

    err = 0
    for i in xrange(len(ref)-k +1):
        r = ref[i:i+k].count(boundary) > 0
        h = hyp[i:i+k].count(boundary) > 0
        if r != h:
           err += 1
    return err / (len(ref)-k +1.)


# skip doctests if numpy is not installed 
Example 17
Project: honours_project   Author: JFriel   File: earleychart.py    GNU General Public License v3.0 5 votes vote down vote up
def _positions(self):
        return xrange(self.num_leaves() + 1) 
Example 18
Project: honours_project   Author: JFriel   File: featurechart.py    GNU General Public License v3.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in xrange(chart.num_leaves() + 1):
                new_edge = FeatureTreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


#////////////////////////////////////////////////////////////
# Feature Chart Parser
#//////////////////////////////////////////////////////////// 
Example 19
Project: honours_project   Author: JFriel   File: chart.py    GNU General Public License v3.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in compat.xrange(chart.num_leaves() + 1):
                new_edge = TreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


########################################################################
##  Filtered Bottom Up
######################################################################## 
Example 20
Project: honours_project   Author: JFriel   File: segmentation.py    GNU General Public License v3.0 5 votes vote down vote up
def pk(ref, hyp, k=None, boundary='1'):
    """
    Compute the Pk metric for a pair of segmentations A segmentation
    is any sequence over a vocabulary of two items (e.g. "0", "1"),
    where the specified boundary value is used to mark the edge of a
    segmentation.

    >>> '%.2f' % pk('0100'*100, '1'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0100'*100, 2)
    '0.00'

    :param ref: the reference segmentation
    :type ref: str or list
    :param hyp: the segmentation to evaluate
    :type hyp: str or list
    :param k: window size, if None, set to half of the average reference segment length
    :type boundary: str or int or bool
    :param boundary: boundary value
    :type boundary: str or int or bool
    :rtype: float
    """

    if k is None:
        k = int(round(len(ref) / (ref.count(boundary) * 2.)))

    err = 0
    for i in xrange(len(ref)-k +1):
        r = ref[i:i+k].count(boundary) > 0
        h = hyp[i:i+k].count(boundary) > 0
        if r != h:
           err += 1
    return err / (len(ref)-k +1.)


# skip doctests if numpy is not installed 
Example 21
Project: honours_project   Author: JFriel   File: featurechart.py    GNU General Public License v3.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in xrange(chart.num_leaves() + 1):
                new_edge = FeatureTreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


#////////////////////////////////////////////////////////////
# Feature Chart Parser
#//////////////////////////////////////////////////////////// 
Example 22
Project: honours_project   Author: JFriel   File: chart.py    GNU General Public License v3.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in compat.xrange(chart.num_leaves() + 1):
                new_edge = TreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


########################################################################
##  Filtered Bottom Up
######################################################################## 
Example 23
Project: serverless-chatbots-workshop   Author: datteswararao   File: segmentation.py    Apache License 2.0 5 votes vote down vote up
def pk(ref, hyp, k=None, boundary='1'):
    """
    Compute the Pk metric for a pair of segmentations A segmentation
    is any sequence over a vocabulary of two items (e.g. "0", "1"),
    where the specified boundary value is used to mark the edge of a
    segmentation.

    >>> '%.2f' % pk('0100'*100, '1'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0100'*100, 2)
    '0.00'

    :param ref: the reference segmentation
    :type ref: str or list
    :param hyp: the segmentation to evaluate
    :type hyp: str or list
    :param k: window size, if None, set to half of the average reference segment length
    :type boundary: str or int or bool
    :param boundary: boundary value
    :type boundary: str or int or bool
    :rtype: float
    """

    if k is None:
        k = int(round(len(ref) / (ref.count(boundary) * 2.)))

    err = 0
    for i in xrange(len(ref)-k +1):
        r = ref[i:i+k].count(boundary) > 0
        h = hyp[i:i+k].count(boundary) > 0
        if r != h:
           err += 1
    return err / (len(ref)-k +1.)


# skip doctests if numpy is not installed 
Example 24
Project: serverless-chatbots-workshop   Author: datteswararao   File: earleychart.py    Apache License 2.0 5 votes vote down vote up
def _positions(self):
        return xrange(self.num_leaves() + 1) 
Example 25
Project: serverless-chatbots-workshop   Author: datteswararao   File: featurechart.py    Apache License 2.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in xrange(chart.num_leaves() + 1):
                new_edge = FeatureTreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


#////////////////////////////////////////////////////////////
# Feature Chart Parser
#//////////////////////////////////////////////////////////// 
Example 26
Project: serverless-chatbots-workshop   Author: datteswararao   File: chart.py    Apache License 2.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in compat.xrange(chart.num_leaves() + 1):
                new_edge = TreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


########################################################################
##  Filtered Bottom Up
######################################################################## 
Example 27
Project: serverless-chatbots-workshop   Author: datteswararao   File: segmentation.py    Apache License 2.0 5 votes vote down vote up
def pk(ref, hyp, k=None, boundary='1'):
    """
    Compute the Pk metric for a pair of segmentations A segmentation
    is any sequence over a vocabulary of two items (e.g. "0", "1"),
    where the specified boundary value is used to mark the edge of a
    segmentation.

    >>> '%.2f' % pk('0100'*100, '1'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0'*400, 2)
    '0.50'
    >>> '%.2f' % pk('0100'*100, '0100'*100, 2)
    '0.00'

    :param ref: the reference segmentation
    :type ref: str or list
    :param hyp: the segmentation to evaluate
    :type hyp: str or list
    :param k: window size, if None, set to half of the average reference segment length
    :type boundary: str or int or bool
    :param boundary: boundary value
    :type boundary: str or int or bool
    :rtype: float
    """

    if k is None:
        k = int(round(len(ref) / (ref.count(boundary) * 2.)))

    err = 0
    for i in xrange(len(ref)-k +1):
        r = ref[i:i+k].count(boundary) > 0
        h = hyp[i:i+k].count(boundary) > 0
        if r != h:
           err += 1
    return err / (len(ref)-k +1.)


# skip doctests if numpy is not installed 
Example 28
Project: serverless-chatbots-workshop   Author: datteswararao   File: featurechart.py    Apache License 2.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in xrange(chart.num_leaves() + 1):
                new_edge = FeatureTreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


#////////////////////////////////////////////////////////////
# Feature Chart Parser
#//////////////////////////////////////////////////////////// 
Example 29
Project: serverless-chatbots-workshop   Author: datteswararao   File: chart.py    Apache License 2.0 5 votes vote down vote up
def apply(self, chart, grammar):
        for prod in grammar.productions(empty=True):
            for index in compat.xrange(chart.num_leaves() + 1):
                new_edge = TreeEdge.from_production(prod, index)
                if chart.insert(new_edge, ()):
                    yield new_edge


########################################################################
##  Filtered Bottom Up
########################################################################