Package schlachtfeld :: Package amov :: Module Namensgrammatik
[hide private]
[frames] | no frames]

Source Code for Module schlachtfeld.amov.Namensgrammatik

  1  #!/bin/env python 
  2  # encoding: utf-8 
  3   
  4  # Charakterverwaltung - Verwalte Charaktere im lesbaren YAML Format 
  5  # Copyright © 2007 - 2007 Arne Babenhauserheide 
  6   
  7  # This program is free software; you can redistribute it and/or modify 
  8  # it under the terms of the GNU General Public License as published by 
  9  # the Free Software Foundation; either version 2 of the License, or 
 10  # (at your option) any later version. 
 11   
 12  # This program is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15  # GNU General Public License for more details. 
 16   
 17  # You should have received a copy of the GNU General Public License 
 18  # along with this program; if not, write to the Free Software 
 19  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 
 20  # MA 02110-1301 USA 
 21   
 22  """Manage Grammars for Name-Generation.  
 23   
 24  This file returns a grammar-object if it is given an ID or tag.  
 25  Useage:  
 26   
 27  >>> grammar = Grammar(ID=None, art=type, tag="tag:1w6.org,2007:Foo") 
 28  >>> print "The Grammar is:", grammar.grammar 
 29  The Grammar is: {'nameMiddle': ['<nmCons><nmVowel>'], 'nameStart': ['<nsCons><nmVowel>', '<nsCons><nmVowel>', '<nsCons><nmVowel>', '<nsVowel>'], 'nameMiddle0to2': ['', '<nameMiddle>', '<nameMiddle><nameMiddle>'], 'name': ['<nameStart><nameMiddle0to2><nameEnd>'], 'neVowel': ['e', 'i', 'a', 'au'], 'nmCons': ['l', 'm', 'lm', 'th', 'r', 's', 'ss', 'p', 'f', 'mb', 'b', 'lb', 'd', 'lf'], 'nsVowel': ['A', 'Au', 'Ei'], 'nsCons': ['J', 'M', 'P', 'N', 'Y', 'D', 'F'], 'nmVowel': ['a', 'e', 'i', 'o', 'u', 'au', 'oa', 'ei'], 'neCons': ['r', 'n', 'm', 's', 'y', 'l', 'th', 'b', 'lb', 'f', 'lf'], 'nameEnd': ['<neCons><neVowel>', '<neCons>', '<neCons>']} 
 30   
 31   
 32   
 33  You need either an ID or a tag. If you use a tag, you must set the ID to None, else teh default ID will be used:  
 34   
 35  grammar = Grammar(ID=None, art=type, tag=tag)""" 
 36   
 37  ### Imports ### 
 38   
 39  # Für das Dateiformat brauchen wir yaml 
 40  import yaml 
 41   
 42  # Um die Dateien zu laden, nutzen wir die Objekt-Klasse.  
 43  import Skripte.Kernskripte.Objekt as Objekt 
 44   
 45  # Um Charaktere per Kommandozeile übergeben zu können brauchen wir sys 
 46  import sys 
 47   
 48  # Für die Prüfung ob Grammatikdateien schon existieren brauchen wir außerdem os.path 
 49  import os 
 50  # TODO: Interaktion mit Versionsverwaltung etc. auslagern. Das brauchen wir in jeder Objektart. 
 51   
 52  # Außerdem brauchen wir die Versionsverwaltung. 
 53  import Versionsverwaltung 
 54   
 55  ### Imports ### 
 56   
 57  ### Klassen ### 
 58   
59 -class Grammar:
60 - def __init__(self, ID=yaml.load("""ID: tag:1w6.org,2007:Foo 61 Version: 0.15 62 Kategorie: Namensgrammatiken 63 """), art=None, tag="tag:1w6.org,2007:Foo"):
64 # If we only get a tag and no ID, use Versionsverwaltung, to create an ID from the tag. 65 if ID != None: 66 self.ID = ID 67 else: 68 versionen = Versionsverwaltung.Versionen(tag=tag,kategorie="Namensgrammatiken") 69 self.ID = versionen.neuste 70 #: The kind of name we want to generate. This isn't used yet. 71 self.art = art 72 # Utilize the Objekt coremodule to load the Grammar Object from a file via the ID. Also supply a template for the case that the file doesn't exist. 73 self.objekt = Objekt.Objekt(ID=self.ID, template=yaml.load(self.grammar_template_yaml())) 74 # Get self.grammer, the content of the object which is the attribute we use for name-generation. 75 self.grammar = self.objekt.objekt 76
77 - def grammar_template_yaml(self):
78 """Return a grammar template.""" 79 return """name: [<nameStart><nameMiddle0to2><nameEnd>] 80 nameEnd: [<neCons><neVowel>, <neCons>, <neCons>] 81 nameMiddle: [<nmCons><nmVowel>] 82 nameMiddle0to2: ['', <nameMiddle>, <nameMiddle><nameMiddle>] 83 nameStart: [<nsCons><nmVowel>, <nsCons><nmVowel>, <nsCons><nmVowel>, <nsVowel>] 84 neCons: [r, n, m, s, y, l, th, b, lb, f, lf] 85 neVowel: [e, i, a, au] 86 nmCons: [l, m, lm, th, r, s, ss, p, f, mb, b, lb, d, lf] 87 nmVowel: [a, e, i, o, u, au, oa, ei] 88 nsCons: [J, M, P, N, Y, D, F] 89 nsVowel: [A, Au, Ei] 90 """ 91 92 #### Classes #### 93 94 #### Self-Test #### 95 96 if __name__ == '__main__': 97 # Erst holen wir uns das Namensgrammatik-Modul mit Standardwerten. 98 #: Das Grammatikobjekt 99 grammar = Grammar() 100 # Dann geben wir dei Grammatik aus. 101 print grammar.grammar 102 # Danach testen wir das Modul mittels Docstrings. (Infos im pydoc String am Anfang der Datei) 103 # Dafür holen wir uns das doctest modul. 104 import doctest 105 # und starten dann dessen Test. 106 doctest.testmod() 107 108 #### Self-Test #### 109