<commit_before># Copyright (c) 2016 Baidu, Inc. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from paddle.trainer.PyDataProvider2 import *

UNK_IDX = 2
START = "<s>"
END = "<e>"


def hook(settings, src_dict_path, trg_dict_path, is_generating, file_list,
         **kwargs):
    # job_mode = 1: training mode
    # job_mode = 0: generating mode
    settings.job_mode = not is_generating
    settings.src_dict = dict()
    for line_count, line in enumerate(open(src_dict_path, "r")):
        settings.src_dict[line.strip()] = line_count
    settings.trg_dict = dict()
    for line_count, line in enumerate(open(trg_dict_path, "r")):
        settings.trg_dict[line.strip()] = line_count

    settings.logger.info("src dict len : %d" % (len(settings.src_dict)))
    settings.sample_count = 0

    if settings.job_mode:
        settings.slots = {
            'source_language_word':
            integer_value_sequence(len(settings.src_dict)),
            'target_language_word':
            integer_value_sequence(len(settings.trg_dict)),
            'target_language_next_word':
            integer_value_sequence(len(settings.trg_dict))
        }
        settings.logger.info("trg dict len : %d" % (len(settings.trg_dict)))
    else:
        settings.slots = {
            'source_language_word':
            integer_value_sequence(len(settings.src_dict)),
            'sent_id':
            integer_value_sequence(len(open(file_list[0], "r").readlines()))
        }


def _get_ids(s, dictionary):
    words = s.strip().split()
    return [dictionary[START]] + \
           [dictionary.get(w, UNK_IDX) for w in words] + \
           [dictionary[END]]<commit_msg>use dictionary comprehension to pythonic code<commit_after># Copyright (c) 2016 Baidu, Inc. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from paddle.trainer.PyDataProvider2 import *

UNK_IDX = 2
START = "<s>"
END = "<e>"


def hook(settings, src_dict_path, trg_dict_path, is_generating, file_list,
         **kwargs):
    # job_mode = 1: training mode
    # job_mode = 0: generating mode
    settings.job_mode = not is_generating
    settings.src_dict = dict()
    with open(src_dict_path, "r") as fin:
        settings.src_dict = {
            line.strip(): line_count
            for line_count, line in enumerate(fin)
        }
    settings.trg_dict = dict()
    with open(trg_dict_path, "r") as fin:
        settings.trg_dict = {
            line.strip(): line_count
            for line_count, line in enumerate(fin)
        }

    settings.logger.info("src dict len : %d" % (len(settings.src_dict)))
    settings.sample_count = 0

    if settings.job_mode:
        settings.slots = {
            'source_language_word':
            integer_value_sequence(len(settings.src_dict)),
            'target_language_word':
            integer_value_sequence(len(settings.trg_dict)),
            'target_language_next_word':
            integer_value_sequence(len(settings.trg_dict))
        }
        settings.logger.info("trg dict len : %d" % (len(settings.trg_dict)))
    else:
        settings.slots = {
            'source_language_word':
            integer_value_sequence(len(settings.src_dict)),
            'sent_id':
            integer_value_sequence(len(open(file_list[0], "r").readlines()))
        }


def _get_ids(s, dictionary):
    words = s.strip().split()
    return [dictionary[START]] + \
           [dictionary.get(w, UNK_IDX) for w in words] + \
           [dictionary[END]]
{'fraction_non_alphanumeric': 0.10626945309026234, 'fraction_numerical': 0.006224988883948421, 'mean_word_length': 2.9395796847635727, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1854945', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1335, 'n_words': 366}