conlang 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 84b2be900d933ecb7e56837534a993f5ad6c4219
4
- data.tar.gz: e99c3ede33eb2a80daa0fa9a7d9d6b16594d9d7a
3
+ metadata.gz: 54c3fa9530fea0396261592ac22612c07dd94ad7
4
+ data.tar.gz: f325285e8db5859dc36ad9015c59e6b943f71a2f
5
5
  SHA512:
6
- metadata.gz: c01f9db7fc189ea4596934fa6b3dc4b6628340f6f6363b024226c26591df561c5558291bb8e256011d17e2b52e6511355105b4f7a8c12edb6c17594c1424c8ba
7
- data.tar.gz: fdd3340b5d0de13370d2a86080543246d0aa409524c393ff49b9c5d61544543e0497243a13c6ba349bc4e7f9be8aaf173757c4d7c131abaabbfb5ea80c78cffb
6
+ metadata.gz: 10ece298a14025c4e2d4ca094eaf9cc0c30c2f08b1f44e1b7b09299cc9e51ea652c376599c0da3db646fc44eca34908be4133ec3b7e0336c6ce3ab7decc28897
7
+ data.tar.gz: 00e1fe61645036d72e3b9609688605527cac8dc14a1c9239b58c233a6e4861c8bd7360da960edf8155ef2d4480412f816602b3b8a90c77097870b52463a24dda
@@ -41,6 +41,21 @@ A phoneme's statistical weight gives
41
41
  a priority to the symbol. It must be an
42
42
  integer greater than 0 and less than 100.
43
43
 
44
+ Replacements
45
+ ------------
46
+ Replacements are a way to add exceptions, so they are
47
+ simple ways to avoid complicated expressions.
48
+
49
+ All replacements are applied at the level of each word
50
+ in order.
51
+
52
+ replacements:
53
+ a: aj
54
+ ew: ej
55
+
56
+ The previous replacements would convert
57
+ words like `vakew` to `vajkej`.
58
+
44
59
  Expression and operators
45
60
  ------------------------
46
61
  Each LANG file has an expression at the end.
data/README.md CHANGED
@@ -36,7 +36,6 @@ Usage as a **gem**:
36
36
  # an array of strings.
37
37
  p x.get_words(10)
38
38
 
39
-
40
39
  There are `*.lang` files as examples at `lang-examples`
41
40
  directory of this [project's source](https://github.com/fluorine/ConlangWordGenerator).
42
41
 
@@ -46,8 +45,8 @@ You must create your own .lang files to generate random
46
45
  words for your constructed languages.
47
46
 
48
47
  There files include a simple lightweight markup language
49
- to describe sets of phonemes, their probability, and a
50
- simple regular grammatical expression.
48
+ to describe sets of phonemes, their probability, replacements
49
+ (for exceptions) and a simple regular grammatical expression.
51
50
 
52
51
  You can learn how to create LANG files [**here**](LANG_FILES_DOC.md).
53
52
  Again, examples at `lang-examples` directory can help you a lot.
@@ -40,7 +40,10 @@ module ConlangWordGenerator
40
40
  accum = 0
41
41
  @pairs.each_index do |index|
42
42
  accum += @pairs[index][1]
43
- return @pairs[index][0] if accum > random
43
+
44
+ if accum > random
45
+ return @pairs[index][0]
46
+ end
44
47
  end
45
48
  end
46
49
 
@@ -1,3 +1,3 @@
1
1
  module ConlangWordGenerator
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
@@ -31,6 +31,9 @@ class WordGenerator
31
31
  @bindings = {}
32
32
  @full_expression = ""
33
33
 
34
+ # Bindings for replacements
35
+ @replacements = {}
36
+
34
37
  # Parse .lang file
35
38
  parse_lang_file
36
39
 
@@ -46,7 +49,7 @@ class WordGenerator
46
49
  # its full grammatical expression.
47
50
  File.open(@lang_file, "r:UTF-8") do |file|
48
51
  lines_count = 1
49
- on_expression = false
52
+ current = :none
50
53
 
51
54
  # Evaluating lines
52
55
  while (line = file.gets)
@@ -60,18 +63,30 @@ class WordGenerator
60
63
  captured = line.scan(/\s*(\w+)\s*:/)
61
64
  current_binding = captured[0][0]
62
65
  @bindings[current_binding] = ConlangWordGenerator::SymbolSet.new
66
+ current = :symbols
63
67
 
64
68
  when /^\s*expression\s*:\s*(#.*)?$/
65
69
  # Start of grammatical expression
66
- #puts "Evaluating expression:"
67
- on_expression = true
68
-
69
- when /^\s*(\S+)\s*[:=]\s*(\d*)\s*(#.*)?$/
70
- # Add a symbol to the current SymbolSet's binding
71
- @bindings[current_binding].add_pair($1, $2.to_i)
72
-
70
+ current = :expression
71
+
72
+ when /^\s*replacements\s*:\s*(#.*)?$/
73
+ # Start of list of replacements
74
+ current = :replacements
75
+
76
+ when /^\s*(\S+)\s*[:=]\s*(\S+)\s*(#.*)?$/
77
+ # Add bindings
78
+ case current
79
+ when :symbols
80
+ #Add a symbol to the current SymbolSet's binding
81
+ @bindings[current_binding].add_pair($1, $2.to_i)
82
+ when :replacements
83
+ @replacements[$1] = $2
84
+ else
85
+ raise LangSyntaxError, "Runtime error when evaluating " +
86
+ "\"#{@lang_file}\" at binding line #{lines_count}."
87
+ end
73
88
  else
74
- if on_expression
89
+ if current == :expression
75
90
  # Copying expression
76
91
  @full_expression += line.strip
77
92
  else
@@ -100,9 +115,18 @@ class WordGenerator
100
115
  words = []
101
116
 
102
117
  qty.times do
103
- words += [@evaluated_expression.sample]
118
+ words += [apply_replacements(@evaluated_expression.sample)]
104
119
  end
105
120
 
106
121
  words
107
122
  end
123
+
124
+ # Apply replacements to words
125
+ def apply_replacements(word)
126
+ @replacements.each do |pattern, target|
127
+ word.gsub!(pattern, target)
128
+ end
129
+
130
+ word
131
+ end
108
132
  end
@@ -0,0 +1,19 @@
1
+ # Thest LANG file for OR operators.
2
+
3
+ symbols for a vowel:
4
+ a: 50
5
+ o: 50
6
+
7
+ symbols for any constant:
8
+ p: 33
9
+ t: 33
10
+ k: 33
11
+
12
+ replacements:
13
+ p: b
14
+ t: d
15
+ k: g
16
+
17
+ expression:
18
+ # Pattern: /[aobdg][ao]/
19
+ or(constant, vowel) + or(10, vowel, vowel)
@@ -43,4 +43,14 @@ class TestWordGenerator < Test::Unit::TestCase
43
43
  assert_match(/^[ptk]*[ao]*$/, word)
44
44
  end
45
45
  end
46
+
47
+ #
48
+ # Test replacements
49
+ #
50
+ def test_replacements
51
+ words = WordGenerator.new(Dir.pwd + "/test/test_file_replacements.lang").get_words(10)
52
+ words.each do |word|
53
+ assert_match(/^[aobdg][ao]$/, word)
54
+ end
55
+ end
46
56
  end
metadata CHANGED
@@ -1,18 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conlang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-20 00:00:00.000000000 Z
11
+ date: 2013-08-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: 'This gem takes constructed grammar and phonemes from a LANG file to
14
- generate random words. You can learn how to create LANG files, or explore some sample
15
- files, at this project''s source repository on Github: https://github.com/fluorine/ConlangWordGenerator'
13
+ description: 'This gem allow users to generate random words. The user must provide
14
+ a LANG file with defined phonemes and a regular grammatical expression. You can
15
+ learn how to create LANG files, or explore some examples, at this project''s source
16
+ repository on Github: https://github.com/fluorine/ConlangWordGenerator'
16
17
  email: fluorine@github.com
17
18
  executables:
18
19
  - conlang
@@ -34,6 +35,7 @@ files:
34
35
  - test/not_lang.gaga
35
36
  - test/test_file_maybe.lang
36
37
  - test/test_file_or.lang
38
+ - test/test_file_replacements.lang
37
39
  - test/test_symbolset.rb
38
40
  - test/test_syntax_error.lang
39
41
  - test/test_wordgenerator.rb
@@ -62,6 +64,5 @@ rubyforge_project:
62
64
  rubygems_version: 2.0.3
63
65
  signing_key:
64
66
  specification_version: 4
65
- summary: 'Full name: ConlangWordGenerator. A statistical random words generator for
66
- constructed languages.'
67
+ summary: ConlangWordGenerator. A statistical random word generator for regular grammars.
67
68
  test_files: []