/usr/lib/python2.7/dist-packages/tegaki/trainer.py is in python-tegaki 0.3.1-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | # -*- coding: utf-8 -*-
# Copyright (C) 2008-2009 The Tegaki project contributors
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Contributors to this file:
# - Mathieu Blondel
import glob
import os
import imp
from cStringIO import StringIO
from tegaki.engine import Engine
from tegaki.dictutils import SortedDict
class TrainerError(Exception):
"""
Raised when something went wrong in a Trainer.
"""
pass
class Trainer(Engine):
"""
Base Trainer class.
A trainer can train models based on sample data annotated with labels.
"""
def __init__(self):
pass
@classmethod
def get_available_trainers(cls):
"""
Return trainers installed on the system.
@rtype: dict
@return: a dict where keys are trainer names and values \
are trainer classes
"""
if not "available_trainers" in cls.__dict__:
cls._load_available_trainers()
return cls.available_trainers
@classmethod
def _load_available_trainers(cls):
cls.available_trainers = SortedDict()
for directory in cls._get_search_path("engines"):
if not os.path.exists(directory):
continue
for f in glob.glob(os.path.join(directory, "*.py")):
if f.endswith("__init__.py") or f.endswith("setup.py"):
continue
module_name = os.path.basename(f).replace(".py", "")
module_name += "trainer"
module = imp.load_source(module_name, f)
try:
name = module.TRAINER_CLASS.TRAINER_NAME
cls.available_trainers[name] = module.TRAINER_CLASS
except AttributeError:
pass
def set_options(self, options):
"""
Process trainer/model specific options.
@type options: dict
@param options: a dict where keys are option names and values are \
option values
"""
pass
# To be implemented by child class
def train(self, character_collection, meta, path=None):
"""
Train a model.
@type character_collection: L{CharacterCollection}
@param character_collection: collection containing training data
@type meta: dict
@param meta: meta dict obtained with L{Engine.read_meta_file}
@type path: str
@param path: path to the ouput model \
(if None, the personal directory is assumed)
The meta dict needs the following keys:
- name: full name (mandatory)
- shortname: name with less than 3 characters (mandatory)
- language: model language (optional)
"""
raise NotImplementedError
def _check_meta(self, meta):
if not meta.has_key("name") or not meta.has_key("shortname"):
raise TrainerError, "meta must contain a name and a shortname"
def _write_meta_file(self, meta, meta_file):
io = StringIO()
for k,v in meta.items():
io.write("%s = %s\n" % (k,v))
if os.path.exists(meta_file):
f = open(meta_file)
contents = f.read()
f.close()
# don't rewrite the file if same
if io.getvalue() == contents:
return
f = open(meta_file, "w")
f.write(io.getvalue())
f.close()
|