Class: Phonology::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/phonology/inventory.rb

Overview

An inventory of Phonological feature sets.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sets) ⇒ Inventory

Returns a new instance of Inventory.



16
17
18
# File 'lib/phonology/inventory.rb', line 16

def initialize(sets)
  @sets = sets
end

Instance Attribute Details

#setsObject (readonly)

A Hash with a set of features as keys, and UTF-8 codepoints for IPA letters as values.



8
9
10
# File 'lib/phonology/inventory.rb', line 8

def sets
  @sets
end

Class Method Details

.from_ipa(*chars) ⇒ Object

Get an instance, building its set from a list IPA characters.



11
12
13
14
# File 'lib/phonology/inventory.rb', line 11

def self.from_ipa(*chars)
  chars = chars.flatten.map {|char| char.unpack("U*")}
  new Hash[Features::SETS.select {|key, val| chars.include? val}]
end

Instance Method Details

#codepoints(*features) ⇒ Object

Given a set of features, return an array of UTF-8 codepoints.



41
42
43
44
# File 'lib/phonology/inventory.rb', line 41

def codepoints(*features)
  features = setify(*features)
  @sets[features] || (raise FeatureError, "No such set #{features.inspect}")
end

#features(symbol) ⇒ Object

Given an IPA symbol, return a corresponding set of distinctive features.



22
23
24
# File 'lib/phonology/inventory.rb', line 22

def features(symbol)
  @sets.index(symbol.unpack("U*"))
end

#symbol(*features) ⇒ Object

Given a set of features, return an IPA symbol



32
33
34
# File 'lib/phonology/inventory.rb', line 32

def symbol(*features)
  codepoints(features).pack("U*")
end

#symbolsObject



36
37
38
# File 'lib/phonology/inventory.rb', line 36

def symbols
  @sets.values.map {|symbol| symbol.pack("U*")}.to_set
end

#with(*features) ⇒ Object

Return an instance of Sounds whose sets include any of the given features.



48
49
50
51
52
53
# File 'lib/phonology/inventory.rb', line 48

def with(*features)
  pos, neg = mangle_args(*features)
  self.class.new(Hash[@sets.select do |key, val|
    !key.intersection(pos).empty?
  end]).without_any(neg)
end

#with_all(*features) ⇒ Object

Return feature sets that include all of the given features



56
57
58
59
60
61
# File 'lib/phonology/inventory.rb', line 56

def with_all(*features)
  pos, neg = mangle_args(*features)
  self.class.new(Hash[@sets.select do |key, val|
    pos.subset?(key)
  end]).without_any(neg)
end

#without(*features) ⇒ Object

Return an instance of Sounds whose sets exclude any of the given features.



65
66
67
68
# File 'lib/phonology/inventory.rb', line 65

def without(*features)
  features = setify(*features)
  self.class.new Hash[@sets.select {|key, val| !features.subset?(key)}]
end

#without_any(*features) ⇒ Object

Return an instance of Sounds whose sets exclude all of the given features.



72
73
74
75
# File 'lib/phonology/inventory.rb', line 72

def without_any(*features)
  features = setify(*features)
  self.class.new Hash[@sets.select {|key, val| key.intersection(features).empty?}]
end