conlang 0.2.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a2e2972b439eaeeff3a865a94f862ca266bf57c2
4
- data.tar.gz: c1b94be6c8d7dbee38a2b55be794882f653f42f1
3
+ metadata.gz: 84b2be900d933ecb7e56837534a993f5ad6c4219
4
+ data.tar.gz: e99c3ede33eb2a80daa0fa9a7d9d6b16594d9d7a
5
5
  SHA512:
6
- metadata.gz: 873f2eb94c953f7a7df75c60f750c66787f2b028c516b19c2158293afc87957663bb0394da6797ee0fe49071f035e86df23cdcf3cf0ffb9f44dc9d1be3457931
7
- data.tar.gz: 49fa67c205f1468b636bca6bf9cf0cfba42a89340ed04c85a7108b7985474c67fdb8befd5fbfebf1c9ce72ed656a7119f4e83ea0e68067a0705ebde73ce6f012
6
+ metadata.gz: c01f9db7fc189ea4596934fa6b3dc4b6628340f6f6363b024226c26591df561c5558291bb8e256011d17e2b52e6511355105b4f7a8c12edb6c17594c1424c8ba
7
+ data.tar.gz: fdd3340b5d0de13370d2a86080543246d0aa409524c393ff49b9c5d61544543e0497243a13c6ba349bc4e7f9be8aaf173757c4d7c131abaabbfb5ea80c78cffb
@@ -14,4 +14,8 @@ Implemented features
14
14
  * An executable in `\bin` has been included.
15
15
  See [README.md](README.md) to see
16
16
  documentation about how to use this gem
17
- as a command line utility.
17
+ as a command line utility.
18
+ * New release (v0.3.0)
19
+ * Better and more specific Exceptions
20
+ * More tests, Rake file
21
+
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ConlangWordGenerator, 0.2.0 beta.
1
+ ConlangWordGenerator, v0.3.0
2
2
  =================================
3
3
  **Now it's a gem!**
4
4
 
@@ -7,8 +7,8 @@ Description
7
7
  This gem allows the user to generate words for
8
8
  constructed languages, given a LANG file that
9
9
  describes the language. It can also be useful
10
- for linguistics to study and generate words from
11
- a given language.
10
+ for linguistics to study and generate valid words
11
+ from a descripted language.
12
12
 
13
13
  The `*.lang` file must include sets of phonemes with
14
14
  their individual probability weight, and a grammatical
@@ -17,7 +17,7 @@ described language.
17
17
 
18
18
  Instructions
19
19
  ------------
20
- Usage as **command line executable**:
20
+ Usage as a **command line executable**:
21
21
 
22
22
  conlang <words count> '<LANG file>'
23
23
 
@@ -25,7 +25,7 @@ This command produces an `output-<name>.txt`
25
25
  file that includes a list of the generated words,
26
26
  separated by newlines.
27
27
 
28
- Usage as **gem**:
28
+ Usage as a **gem**:
29
29
 
30
30
  require 'conlang'
31
31
 
@@ -55,3 +55,12 @@ Again, examples at `lang-examples` directory can help you a lot.
55
55
  The class `WordGenerator` takes a path to a valid `*.lang`
56
56
  file to produce a generator of words, as described previously
57
57
  in the **Instructions** section.
58
+
59
+ Root-level identifiers
60
+ ----------------------
61
+ - **WordGenerator** - Main class
62
+ - **ConlangWordGenerator** - Namespace module for the gem
63
+ - Exceptions
64
+ - **LangFileError** < StandardError, for LANG files
65
+ - **LangFileIOError** < LangFileError
66
+ - **LangSyntaxError** < LangFileError
@@ -10,7 +10,6 @@ USAGE = " Usage:\n conlang <words count> '<LANG file>'\n\n" +
10
10
 
11
11
  # If incorrect quantity of arguments
12
12
  if ARGV.length != 2 && ARGV[0] !~ /d+/ && ARGV[1] !~ /\.lang$/
13
- p "argv"
14
13
  puts USAGE
15
14
  exit
16
15
  end
@@ -23,15 +22,27 @@ require 'conlang'
23
22
  begin
24
23
  # Parse arguments
25
24
  words_qty = ARGV[0].to_i
26
- filename = ARGV[1].split(".")[0]
25
+ filename = ARGV[1]
26
+ name = filename.split(".")[0]
27
27
 
28
28
  # Generate words
29
- generator = WordGenerator.new(filename + ".lang")
29
+ generator = WordGenerator.new(filename)
30
30
  words = generator.get_words(words_qty)
31
31
 
32
32
  # Produce output file
33
- IO.write(filename + "-output.txt", words.join("\n"))
34
- puts " '#{filename + "-output.txt"}' file has been produced."
35
- rescue
33
+ IO.write(name + "-output.txt", words.join("\n"))
34
+ puts " '#{name}-output.txt' file has been produced."
35
+
36
+ rescue LangFileIOError => e
37
+ puts e.inspect + "\n"
36
38
  puts USAGE
39
+
40
+ rescue LangFileIOError => e
41
+ puts e.inspect + "\n"
42
+ puts USAGE
43
+
44
+ rescue StandardError => e
45
+ puts e.inspect + "\n"
46
+ puts USAGE
47
+
37
48
  end
@@ -1,4 +1,4 @@
1
- #encoding:utf-8
1
+ require 'conlang/errors'
2
2
  require 'conlang/operators'
3
3
  require 'conlang/symbolset'
4
4
  require 'conlang/wordgenerator'
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ class LangFileError < StandardError
4
+ def initialize(msg)
5
+ super msg
6
+ end
7
+ end
8
+
9
+ # If LANG file doesn't exists
10
+ # or the loading fails
11
+ class LangFileIOError < LangFileError
12
+ end
13
+
14
+ # If syntax error in LANG file
15
+ class LangSyntaxError < LangFileError
16
+ end
@@ -1,3 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
1
3
  # Operators for .lang grammatical expressions.
2
4
  # - maybe(weight, symbolset)
3
5
  # The symbolset may appear or not (empty string),
@@ -21,8 +23,8 @@ module ConlangWordGenerator
21
23
  class Or
22
24
  def initialize(weight, setA, setB)
23
25
  unless weight > 0 and weight < 100
24
- raise "Error: Weight for an or() operator " +
25
- "must be between 1 and 100 (exclusive)."
26
+ raise LangSyntaxError, "Weight for an or() operator must " +
27
+ "be between 1 and 100 (exclusive)."
26
28
  end
27
29
 
28
30
  @weight = weight
@@ -64,8 +66,8 @@ module ConlangWordGenerator
64
66
  class Maybe
65
67
  def initialize(weight, set)
66
68
  unless weight > 0 and weight < 100
67
- raise "Error: Weight for an maybe() operator " +
68
- "must be between 1 and 100 (exclusive)."
69
+ raise LangSyntaxError, "Weight for an maybe() operator " +
70
+ "must be between 1 and 100 (exclusive)."
69
71
  end
70
72
 
71
73
  @weight = weight
@@ -95,7 +97,7 @@ module ConlangWordGenerator
95
97
  elsif args.length == 3
96
98
  Or.new(args[0].to_i, args[1], args[2])
97
99
  else
98
- raise "Error evaluating an Or() expression."
100
+ raise LangSyntaxError, "Invalid Or() operator arguments."
99
101
  end
100
102
  end
101
103
 
@@ -105,7 +107,7 @@ module ConlangWordGenerator
105
107
  elsif args.length == 2
106
108
  Maybe.new(args[0].to_i, args[1])
107
109
  else
108
- raise "Error evaluating Maybe() expression."
110
+ raise LangSyntaxError, "Invalid Maybe() operator arguments."
109
111
  end
110
112
  end
111
113
 
@@ -132,6 +134,11 @@ module ConlangWordGenerator
132
134
 
133
135
  # Evaluate all generated assigments
134
136
  # and then the grammatical expression.
135
- eval(expr)
137
+ begin
138
+ eval(expr)
139
+ rescue
140
+ raise LangSyntaxError, "Invalid operators or bindings " +
141
+ "in grammatical expression."
142
+ end
136
143
  end
137
144
  end
@@ -12,7 +12,8 @@ module ConlangWordGenerator
12
12
  # Add new symbol with its probability into the set
13
13
  def add_pair(symbol, weight)
14
14
  unless weight > 0 and weight < 100
15
- raise "Weight of symbol '#{symbol}' must be a value between 1 and 99."
15
+ raise LangSyntaxError, "Weight of symbol '#{symbol}' must be " +
16
+ "a value between 1 and 99, inclusive."
16
17
  end
17
18
 
18
19
  # Insert new pair in the sorted array of pairs
@@ -1,3 +1,3 @@
1
1
  module ConlangWordGenerator
2
- VERSION = '0.2.1.1'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -1,11 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- #require 'lib/symbolset.rb'
4
-
5
3
  #
6
4
  # This Ruby class generates random words using
7
5
  # prabability-weighted symbols, and grammar rules
8
- # from a given .lang file as input.
6
+ # from a given *.lang file as input.
9
7
  #
10
8
 
11
9
  class WordGenerator
@@ -13,12 +11,14 @@ class WordGenerator
13
11
  def initialize(file)
14
12
  # Check file extension.
15
13
  unless file.match(/.*\.lang/)
16
- raise "Given file, \"#{file}\", is not a .lang file."
14
+ raise LangFileIOError,
15
+ "Given file, \"#{file}\", is not a .lang file."
17
16
  end
18
17
 
19
18
  # Check if files exist
20
19
  unless File.exist?(file)
21
- raise "File \"#{file}\" was not found."
20
+ raise LangFileIOError,
21
+ "File \"#{file}\" was not found."
22
22
  end
23
23
 
24
24
  # # # # # # # # #
@@ -75,8 +75,8 @@ class WordGenerator
75
75
  # Copying expression
76
76
  @full_expression += line.strip
77
77
  else
78
- raise "Runtime error when evaluating " +
79
- "\"#{@lang_file}\" at line #{lines_count}."
78
+ raise LangSyntaxError, "Runtime error when evaluating " +
79
+ "\"#{@lang_file}\" at line #{lines_count}."
80
80
  end
81
81
  end
82
82
 
@@ -89,7 +89,8 @@ class WordGenerator
89
89
  # This method evaluates the grammatical expression
90
90
  # and then generate random words.
91
91
  def eval_expression
92
- @evaluated_expression = ConlangWordGenerator::run_expression(@full_expression, @bindings)
92
+ @evaluated_expression =
93
+ ConlangWordGenerator::run_expression(@full_expression, @bindings)
93
94
  end
94
95
 
95
96
  # This method generates words
File without changes
@@ -0,0 +1,24 @@
1
+ require 'conlang'
2
+ require 'test/unit'
3
+
4
+ class TestSymbolset < Test::Unit::TestCase
5
+ def setup
6
+ @ss = ConlangWordGenerator::SymbolSet.new
7
+ end
8
+
9
+ def test_out_of_weight_upper_range
10
+ (100...110).each do |n|
11
+ assert_raise(LangSyntaxError) do
12
+ @ss.add_pair 'x', n
13
+ end
14
+ end
15
+ end
16
+
17
+ def test_out_of_weight_lower_range
18
+ (-5...1).each do |n|
19
+ assert_raise(LangSyntaxError) do
20
+ @ss.add_pair 'x', n
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ # Thest LANG file for MAYBE operators.
2
+
3
+ symbols for a vowel:
4
+ a: 50
5
+ o: 50
6
+
7
+ ########ERROR##########
8
+ maybe(vowel)
9
+ #######################
10
+
11
+ symbols for any constant:
12
+ p: 33
13
+ t: 33
14
+ k: 33
15
+
16
+ expression:
17
+ # Pattern: /^[ptk]*[ao]*$/
18
+ Maybe(constant) + maybe(10, vowel)
@@ -2,15 +2,43 @@ require 'conlang'
2
2
  require 'test/unit'
3
3
 
4
4
  class TestWordGenerator < Test::Unit::TestCase
5
+ #
6
+ # Test if an Error occurs when opening a file
7
+ #
8
+ def test_file_does_not_exist
9
+ assert_raise(LangFileIOError) do
10
+ WordGenerator.new(Dir.pwd + "/test/doesnt_exists.lang")
11
+ end
12
+ end
13
+
14
+ def test_not_lang_file
15
+ assert_raise(LangFileIOError) do
16
+ WordGenerator.new(Dir.pwd + "/test/not_lang.gaga")
17
+ end
18
+ end
19
+
20
+ #
21
+ # Catch a general error in LANG file's body
22
+ #
23
+ def test_lang_syntax_error
24
+ assert_raise(LangSyntaxError) do
25
+ WordGenerator.new(Dir.pwd + "/test/test_syntax_error.lang")
26
+ end
27
+ end
28
+
29
+ #
30
+ # Test if operators work fine throw
31
+ # the WordGenerator class.
32
+ #
5
33
  def test_or_operators
6
- words = WordGenerator.new("test_file_or.lang").get_words(10)
34
+ words = WordGenerator.new(Dir.pwd + "/test/test_file_or.lang").get_words(10)
7
35
  words.each do |word|
8
36
  assert_match(/^[aoptk][ao]$/, word)
9
37
  end
10
38
  end
11
39
 
12
40
  def test_maybe_operators
13
- words = WordGenerator.new("test_file_maybe.lang").get_words(10)
41
+ words = WordGenerator.new(Dir.pwd + "/test/test_file_maybe.lang").get_words(10)
14
42
  words.each do |word|
15
43
  assert_match(/^[ptk]*[ao]*$/, word)
16
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conlang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1.1
4
+ version: 0.3.0
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-12 00:00:00.000000000 Z
11
+ date: 2013-07-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'This gem takes constructed grammar and phonemes from a LANG file to
14
14
  generate random words. You can learn how to create LANG files, or explore some sample
@@ -20,6 +20,7 @@ extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
22
  - lib/conlang.rb
23
+ - lib/conlang/errors.rb
23
24
  - lib/conlang/operators.rb
24
25
  - lib/conlang/symbolset.rb
25
26
  - lib/conlang/version.rb
@@ -30,8 +31,11 @@ files:
30
31
  - README.md
31
32
  - LICENSE.txt
32
33
  - NOTICE.txt
34
+ - test/not_lang.gaga
33
35
  - test/test_file_maybe.lang
34
36
  - test/test_file_or.lang
37
+ - test/test_symbolset.rb
38
+ - test/test_syntax_error.lang
35
39
  - test/test_wordgenerator.rb
36
40
  homepage: https://github.com/fluorine/ConlangWordGenerator
37
41
  licenses: