Python nltk.compat.imap() Examples

The following are code examples for showing how to use nltk.compat.imap(). 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: hmm.py    GNU General Public License v3.0 4 votes vote down vote up
def test(self, test_sequence, verbose=False, **kwargs):
        """
        Tests the HiddenMarkovModelTagger instance.

        :param test_sequence: a sequence of labeled test instances
        :type test_sequence: list(list)
        :param verbose: boolean flag indicating whether training should be
            verbose or include printed output
        :type verbose: bool
        """

        def words(sent):
            return [word for (word, tag) in sent]

        def tags(sent):
            return [tag for (word, tag) in sent]

        def flatten(seq):
            return list(itertools.chain(*seq))

        test_sequence = self._transform(test_sequence)
        predicted_sequence = list(imap(self._tag, imap(words, test_sequence)))

        if verbose:
            for test_sent, predicted_sent in izip(test_sequence, predicted_sequence):
                print('Test:',
                    ' '.join('%s/%s' % (token, tag)
                             for (token, tag) in test_sent))
                print()
                print('Untagged:',
                    ' '.join("%s" % token for (token, tag) in test_sent))
                print()
                print('HMM-tagged:',
                    ' '.join('%s/%s' % (token, tag)
                              for (token, tag) in predicted_sent))
                print()
                print('Entropy:',
                    self.entropy([(token, None) for
                                  (token, tag) in predicted_sent]))
                print()
                print('-' * 60)

        test_tags = flatten(imap(tags, test_sequence))
        predicted_tags = flatten(imap(tags, predicted_sequence))

        acc = accuracy(test_tags, predicted_tags)
        count = sum(len(sent) for sent in test_sequence)
        print('accuracy over %d tokens: %.2f' % (count, acc * 100)) 
Example 2
Project: OpenBottle   Author: xiaozhuchacha   File: hmm.py    MIT License 4 votes vote down vote up
def test(self, test_sequence, verbose=False, **kwargs):
        """
        Tests the HiddenMarkovModelTagger instance.

        :param test_sequence: a sequence of labeled test instances
        :type test_sequence: list(list)
        :param verbose: boolean flag indicating whether training should be
            verbose or include printed output
        :type verbose: bool
        """

        def words(sent):
            return [word for (word, tag) in sent]

        def tags(sent):
            return [tag for (word, tag) in sent]

        def flatten(seq):
            return list(itertools.chain(*seq))

        test_sequence = self._transform(test_sequence)
        predicted_sequence = list(imap(self._tag, imap(words, test_sequence)))

        if verbose:
            for test_sent, predicted_sent in izip(test_sequence, predicted_sequence):
                print('Test:',
                    ' '.join('%s/%s' % (token, tag)
                             for (token, tag) in test_sent))
                print()
                print('Untagged:',
                    ' '.join("%s" % token for (token, tag) in test_sent))
                print()
                print('HMM-tagged:',
                    ' '.join('%s/%s' % (token, tag)
                              for (token, tag) in predicted_sent))
                print()
                print('Entropy:',
                    self.entropy([(token, None) for
                                  (token, tag) in predicted_sent]))
                print()
                print('-' * 60)

        test_tags = flatten(imap(tags, test_sequence))
        predicted_tags = flatten(imap(tags, predicted_sequence))

        acc = accuracy(test_tags, predicted_tags)
        count = sum(len(sent) for sent in test_sequence)
        print('accuracy over %d tokens: %.2f' % (count, acc * 100)) 
Example 3
Project: OpenBottle   Author: xiaozhuchacha   File: hmm.py    MIT License 4 votes vote down vote up
def test(self, test_sequence, verbose=False, **kwargs):
        """
        Tests the HiddenMarkovModelTagger instance.

        :param test_sequence: a sequence of labeled test instances
        :type test_sequence: list(list)
        :param verbose: boolean flag indicating whether training should be
            verbose or include printed output
        :type verbose: bool
        """

        def words(sent):
            return [word for (word, tag) in sent]

        def tags(sent):
            return [tag for (word, tag) in sent]

        def flatten(seq):
            return list(itertools.chain(*seq))

        test_sequence = self._transform(test_sequence)
        predicted_sequence = list(imap(self._tag, imap(words, test_sequence)))

        if verbose:
            for test_sent, predicted_sent in izip(test_sequence, predicted_sequence):
                print('Test:',
                    ' '.join('%s/%s' % (token, tag)
                             for (token, tag) in test_sent))
                print()
                print('Untagged:',
                    ' '.join("%s" % token for (token, tag) in test_sent))
                print()
                print('HMM-tagged:',
                    ' '.join('%s/%s' % (token, tag)
                              for (token, tag) in predicted_sent))
                print()
                print('Entropy:',
                    self.entropy([(token, None) for
                                  (token, tag) in predicted_sent]))
                print()
                print('-' * 60)

        test_tags = flatten(imap(tags, test_sequence))
        predicted_tags = flatten(imap(tags, predicted_sequence))

        acc = accuracy(test_tags, predicted_tags)
        count = sum(len(sent) for sent in test_sequence)
        print('accuracy over %d tokens: %.2f' % (count, acc * 100)) 
Example 4
Project: FancyWord   Author: EastonLee   File: hmm.py    GNU General Public License v3.0 4 votes vote down vote up
def test(self, test_sequence, verbose=False, **kwargs):
        """
        Tests the HiddenMarkovModelTagger instance.

        :param test_sequence: a sequence of labeled test instances
        :type test_sequence: list(list)
        :param verbose: boolean flag indicating whether training should be
            verbose or include printed output
        :type verbose: bool
        """

        def words(sent):
            return [word for (word, tag) in sent]

        def tags(sent):
            return [tag for (word, tag) in sent]

        def flatten(seq):
            return list(itertools.chain(*seq))

        test_sequence = self._transform(test_sequence)
        predicted_sequence = list(imap(self._tag, imap(words, test_sequence)))

        if verbose:
            for test_sent, predicted_sent in izip(test_sequence, predicted_sequence):
                print('Test:',
                    ' '.join('%s/%s' % (token, tag)
                             for (token, tag) in test_sent))
                print()
                print('Untagged:',
                    ' '.join("%s" % token for (token, tag) in test_sent))
                print()
                print('HMM-tagged:',
                    ' '.join('%s/%s' % (token, tag)
                              for (token, tag) in predicted_sent))
                print()
                print('Entropy:',
                    self.entropy([(token, None) for
                                  (token, tag) in predicted_sent]))
                print()
                print('-' * 60)

        test_tags = flatten(imap(tags, test_sequence))
        predicted_tags = flatten(imap(tags, predicted_sequence))

        acc = accuracy(test_tags, predicted_tags)
        count = sum(len(sent) for sent in test_sequence)
        print('accuracy over %d tokens: %.2f' % (count, acc * 100)) 
Example 5
Project: honours_project   Author: JFriel   File: hmm.py    GNU General Public License v3.0 4 votes vote down vote up
def test(self, test_sequence, verbose=False, **kwargs):
        """
        Tests the HiddenMarkovModelTagger instance.

        :param test_sequence: a sequence of labeled test instances
        :type test_sequence: list(list)
        :param verbose: boolean flag indicating whether training should be
            verbose or include printed output
        :type verbose: bool
        """

        def words(sent):
            return [word for (word, tag) in sent]

        def tags(sent):
            return [tag for (word, tag) in sent]

        def flatten(seq):
            return list(itertools.chain(*seq))

        test_sequence = self._transform(test_sequence)
        predicted_sequence = list(imap(self._tag, imap(words, test_sequence)))

        if verbose:
            for test_sent, predicted_sent in izip(test_sequence, predicted_sequence):
                print('Test:',
                    ' '.join('%s/%s' % (token, tag)
                             for (token, tag) in test_sent))
                print()
                print('Untagged:',
                    ' '.join("%s" % token for (token, tag) in test_sent))
                print()
                print('HMM-tagged:',
                    ' '.join('%s/%s' % (token, tag)
                              for (token, tag) in predicted_sent))
                print()
                print('Entropy:',
                    self.entropy([(token, None) for
                                  (token, tag) in predicted_sent]))
                print()
                print('-' * 60)

        test_tags = flatten(imap(tags, test_sequence))
        predicted_tags = flatten(imap(tags, predicted_sequence))

        acc = accuracy(test_tags, predicted_tags)
        count = sum(len(sent) for sent in test_sequence)
        print('accuracy over %d tokens: %.2f' % (count, acc * 100)) 
Example 6
Project: honours_project   Author: JFriel   File: hmm.py    GNU General Public License v3.0 4 votes vote down vote up
def test(self, test_sequence, verbose=False, **kwargs):
        """
        Tests the HiddenMarkovModelTagger instance.

        :param test_sequence: a sequence of labeled test instances
        :type test_sequence: list(list)
        :param verbose: boolean flag indicating whether training should be
            verbose or include printed output
        :type verbose: bool
        """

        def words(sent):
            return [word for (word, tag) in sent]

        def tags(sent):
            return [tag for (word, tag) in sent]

        def flatten(seq):
            return list(itertools.chain(*seq))

        test_sequence = self._transform(test_sequence)
        predicted_sequence = list(imap(self._tag, imap(words, test_sequence)))

        if verbose:
            for test_sent, predicted_sent in izip(test_sequence, predicted_sequence):
                print('Test:',
                    ' '.join('%s/%s' % (token, tag)
                             for (token, tag) in test_sent))
                print()
                print('Untagged:',
                    ' '.join("%s" % token for (token, tag) in test_sent))
                print()
                print('HMM-tagged:',
                    ' '.join('%s/%s' % (token, tag)
                              for (token, tag) in predicted_sent))
                print()
                print('Entropy:',
                    self.entropy([(token, None) for
                                  (token, tag) in predicted_sent]))
                print()
                print('-' * 60)

        test_tags = flatten(imap(tags, test_sequence))
        predicted_tags = flatten(imap(tags, predicted_sequence))

        acc = accuracy(test_tags, predicted_tags)
        count = sum(len(sent) for sent in test_sequence)
        print('accuracy over %d tokens: %.2f' % (count, acc * 100)) 
Example 7
Project: serverless-chatbots-workshop   Author: datteswararao   File: hmm.py    Apache License 2.0 4 votes vote down vote up
def test(self, test_sequence, verbose=False, **kwargs):
        """
        Tests the HiddenMarkovModelTagger instance.

        :param test_sequence: a sequence of labeled test instances
        :type test_sequence: list(list)
        :param verbose: boolean flag indicating whether training should be
            verbose or include printed output
        :type verbose: bool
        """

        def words(sent):
            return [word for (word, tag) in sent]

        def tags(sent):
            return [tag for (word, tag) in sent]

        def flatten(seq):
            return list(itertools.chain(*seq))

        test_sequence = self._transform(test_sequence)
        predicted_sequence = list(imap(self._tag, imap(words, test_sequence)))

        if verbose:
            for test_sent, predicted_sent in izip(test_sequence, predicted_sequence):
                print('Test:',
                    ' '.join('%s/%s' % (token, tag)
                             for (token, tag) in test_sent))
                print()
                print('Untagged:',
                    ' '.join("%s" % token for (token, tag) in test_sent))
                print()
                print('HMM-tagged:',
                    ' '.join('%s/%s' % (token, tag)
                              for (token, tag) in predicted_sent))
                print()
                print('Entropy:',
                    self.entropy([(token, None) for
                                  (token, tag) in predicted_sent]))
                print()
                print('-' * 60)

        test_tags = flatten(imap(tags, test_sequence))
        predicted_tags = flatten(imap(tags, predicted_sequence))

        acc = accuracy(test_tags, predicted_tags)
        count = sum(len(sent) for sent in test_sequence)
        print('accuracy over %d tokens: %.2f' % (count, acc * 100)) 
Example 8
Project: serverless-chatbots-workshop   Author: datteswararao   File: hmm.py    Apache License 2.0 4 votes vote down vote up
def test(self, test_sequence, verbose=False, **kwargs):
        """
        Tests the HiddenMarkovModelTagger instance.

        :param test_sequence: a sequence of labeled test instances
        :type test_sequence: list(list)
        :param verbose: boolean flag indicating whether training should be
            verbose or include printed output
        :type verbose: bool
        """

        def words(sent):
            return [word for (word, tag) in sent]

        def tags(sent):
            return [tag for (word, tag) in sent]

        def flatten(seq):
            return list(itertools.chain(*seq))

        test_sequence = self._transform(test_sequence)
        predicted_sequence = list(imap(self._tag, imap(words, test_sequence)))

        if verbose:
            for test_sent, predicted_sent in izip(test_sequence, predicted_sequence):
                print('Test:',
                    ' '.join('%s/%s' % (token, tag)
                             for (token, tag) in test_sent))
                print()
                print('Untagged:',
                    ' '.join("%s" % token for (token, tag) in test_sent))
                print()
                print('HMM-tagged:',
                    ' '.join('%s/%s' % (token, tag)
                              for (token, tag) in predicted_sent))
                print()
                print('Entropy:',
                    self.entropy([(token, None) for
                                  (token, tag) in predicted_sent]))
                print()
                print('-' * 60)

        test_tags = flatten(imap(tags, test_sequence))
        predicted_tags = flatten(imap(tags, predicted_sequence))

        acc = accuracy(test_tags, predicted_tags)
        count = sum(len(sent) for sent in test_sequence)
        print('accuracy over %d tokens: %.2f' % (count, acc * 100))