Class: Cyc::Service::NameService
- Inherits:
-
Object
- Object
- Cyc::Service::NameService
- Defined in:
- lib/cyc/service/name_service.rb
Overview
Class used to find Cyc terms by their name.
Instance Attribute Summary collapse
-
#cyc ⇒ Object
Returns the value of attribute cyc.
Instance Method Summary collapse
-
#canonical_label(term) ⇒ Object
Returns canonical label for the
term
. -
#close ⇒ Object
Close connection to Cyc server.
-
#convert_ruby_term(term) ⇒ Object
Convert Ruby term (e.g. :Dog) to cyc term representation.
-
#find_by_id(id) ⇒ Object
Look-up by ID, i.e.
-
#find_by_label(label) ⇒ Object
Find term by prefered
label
. -
#find_by_name(name) ⇒ Object
Approximate find by name - the result is ambiguous, so the result is an array.
-
#find_by_term_name(name) ⇒ Object
Look-up by
name
, i.e. -
#initialize(cyc_client = Client.new, term_factory = Term) ⇒ NameService
constructor
A new instance of NameService.
-
#labels(term) ⇒ Object
Returns all (non-canonical) labels for the
term
.
Constructor Details
#initialize(cyc_client = Client.new, term_factory = Term) ⇒ NameService
Returns a new instance of NameService.
19 20 21 22 23 |
# File 'lib/cyc/service/name_service.rb', line 19 def initialize(cyc_client=Client.new,term_factory=Term) @cyc = cyc_client @cyc.connect @factory = term_factory end |
Instance Attribute Details
#cyc ⇒ Object
Returns the value of attribute cyc.
17 18 19 |
# File 'lib/cyc/service/name_service.rb', line 17 def cyc @cyc end |
Instance Method Details
#canonical_label(term) ⇒ Object
Returns canonical label for the term
.
78 79 80 81 |
# File 'lib/cyc/service/name_service.rb', line 78 def canonical_label(term) (@cyc.cyc_query(-> { '\'(#$prettyString-Canonical ' + term.to_cyc(true) + ' ?s)'}, :EnglishMt) || []). map{|e| e.first.last }.first end |
#close ⇒ Object
Close connection to Cyc server.
94 95 96 |
# File 'lib/cyc/service/name_service.rb', line 94 def close @cyc.close end |
#convert_ruby_term(term) ⇒ Object
Convert Ruby term (e.g. :Dog) to cyc term representation.
99 100 101 102 103 104 |
# File 'lib/cyc/service/name_service.rb', line 99 def convert_ruby_term(term) id = @cyc.compact_hl_external_id_string(term) if id @factory.new(term,id) end end |
#find_by_id(id) ⇒ Object
Look-up by ID, i.e. external identifier of the term.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cyc/service/name_service.rb', line 39 def find_by_id(id) begin term = @cyc.find_cycl_object_by_compact_hl_external_id_string(id) if term and term!='<The' @factory.new(term,id) end rescue CycError nil end end |
#find_by_label(label) ⇒ Object
Find term by prefered label
. In rare cases the result is ambiguous - an AmbiguousResult is raised then. You can get the result, by calling ‘result’ on the returned exception.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cyc/service/name_service.rb', line 65 def find_by_label(label) result = @cyc.cyc_query( -> { '`(#$prettyString-Canonical ?s ' + transliterate(label).to_cyc + ')' }, :EnglishMt) if result result.map!{|e| convert_ruby_term(extract_term_name(e.first))} if result.size == 1 result.first else raise AmbiguousResult.new(result) end end end |
#find_by_name(name) ⇒ Object
Approximate find by name - the result is ambiguous, so the result is an array.
52 53 54 55 56 57 58 59 60 |
# File 'lib/cyc/service/name_service.rb', line 52 def find_by_name(name) result = @cyc.denotation_mapper(transliterate(name)) if result result.select{|label,_,_| label == name }. map{|_,_,ruby_term| convert_ruby_term(ruby_term) } else [] end end |
#find_by_term_name(name) ⇒ Object
Look-up by name
, i.e. the exact name of the Cyc term.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cyc/service/name_service.rb', line 26 def find_by_term_name(name) if name =~ /\A\[/ convert_ruby_term(eval(name)) else term = @cyc.find_constant(transliterate(name)) if term id = @cyc.compact_hl_external_id_string(term) @factory.new(term,id) end end end |
#labels(term) ⇒ Object
Returns all (non-canonical) labels for the term
.
84 85 86 87 88 89 90 91 |
# File 'lib/cyc/service/name_service.rb', line 84 def labels(term) begin (@cyc.cyc_query(-> { '\'(#$prettyString ' + term.to_cyc(true) + ' ?s)'}, :EnglishMt) || []). map{|e| e.first.last } rescue CycError [] end end |