<commit_before># -*- coding: utf-8 -*-

"""
file: corpus.py

Description: defines the Corpus object. It is an object representation
of a CoNLL-formatted corpus.

author: <NAME>
copyright (c) 2016 <NAME> - all rights reserved

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

class Corpus(object):
    def __init__(self, fields=[], sentences=[]):
        self.fields    = fields
        self.sentences = sentences
    
    def __iter__(self):
        for element in self.iterate_on_sentences():
            yield element
    
    def __unicode__(self):
        return self.unicode(self.fields)
    
    def unicode(self, fields, separator=u"\t"):
        fmt       = u"\t".join([u"%%(%s)s" %field for field in fields])
        sentences = []
        for sentence in self:
            sentences.append([])
            for token in sentence:
                sentences[-1].append((fmt %token) + u"\n")
        return u"\n".join([u"".join(sentence) for sentence in sentences])
    
    def iterate_on_sentences(self):
        for element in self.sentences:
            yield element<commit_msg>corrected a bug in obj.storage.corpus.Corpus: when creating a new Corpus object, fields and sentences would not be correctly reinitialized.<commit_after># -*- coding: utf-8 -*-

"""
file: corpus.py

Description: defines the Corpus object. It is an object representation
of a CoNLL-formatted corpus.

author: <NAME>
copyright (c) 2016 <NAME> - all rights reserved

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

class Corpus(object):
    def __init__(self, fields=None, sentences=None):
        if fields:
            self.fields = fields[:]
        else:
            self.fields = []
        
        if sentences:
            self.sentences = sentences[:]
        else:
            self.sentences = []
    
    def __iter__(self):
        for element in self.iterate_on_sentences():
            yield element
    
    def __unicode__(self):
        return self.unicode(self.fields)
    
    def unicode(self, fields, separator=u"\t"):
        fmt       = u"\t".join([u"%%(%s)s" %field for field in fields])
        sentences = []
        for sentence in self:
            sentences.append([])
            for token in sentence:
                sentences[-1].append((fmt %token) + u"\n")
        return u"\n".join([u"".join(sentence) for sentence in sentences])
    
    def iterate_on_sentences(self):
        for element in self.sentences:
            yield element
{'fraction_non_alphanumeric': 0.09126321647189761, 'fraction_numerical': 0.0038953811908736783, 'mean_word_length': 2.7253886010362693, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6053004', 'n_tokens_mistral': 978, 'n_tokens_neox': 948, 'n_words': 399}