cycr 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: 2d7f4bcddb89239483cb031ff210bf14f23a55e6
4
+ data.tar.gz: 2f070d448de23cb1c881638b2d9f914b558b1195
5
+ SHA512:
6
+ metadata.gz: 9b016868803302a20f55688ed9192a84190a5caf2a6b59f716f10614f117bca91b7a33b7a74f4027651e524490f99b65d123e09711c388f85222111ad322275b
7
+ data.tar.gz: 318d115f7645310244a7480cad11917c4618219ddcb56ffa8566802f32e7068b4f9fee7685872b4c061acf7ee20fbe9b3659f96f9cbe221af86f030406d86e4c
data/Rakefile CHANGED
@@ -11,6 +11,7 @@ task :test do
11
11
  sh "rspec spec/parser.rb"
12
12
  sh "rspec integration/assertion.rb"
13
13
  sh "rspec integration/client.rb"
14
+ sh "rspec integration/name_service.rb"
14
15
  end
15
16
 
16
17
  desc "Build the gem"
@@ -19,7 +20,7 @@ task :build do
19
20
  end
20
21
 
21
22
  desc "Install the library at local machnie"
22
- task :install => :build do
23
+ task :install => :build do
23
24
  sh "sudo gem install #$gem_name -l"
24
25
  end
25
26
 
@@ -30,7 +31,7 @@ end
30
31
 
31
32
  desc "Clean"
32
33
  task :clean do
33
- sh "rm #$gem_name*.gem"
34
+ sh "rm #$gem_name*.gem"
34
35
  end
35
36
 
36
37
  desc "Show changelog from the last release"
@@ -1,3 +1,5 @@
1
+ 0.2.7
2
+ - Cyc name service implementation
1
3
  0.2.6
2
4
  - fix deadlock in cache
3
5
  0.2.5
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ $:.unshift "lib"
3
+ require 'cycr'
4
+
5
+
6
+ describe Cyc::Service::NameService do
7
+ let(:service) { Cyc::Service::NameService.new(@client) }
8
+
9
+ before(:all) do
10
+ @client = Cyc::Client.new()
11
+ #@client.debug = true
12
+ end
13
+
14
+ after(:all) do
15
+ @client.close
16
+ end
17
+
18
+ it "should find term by exact name" do
19
+ service.find_by_term_name("Dog").should_not == nil
20
+ end
21
+
22
+ it "should find term by id" do
23
+ term = service.find_by_term_name("Dog")
24
+ service.find_by_id(term.id).should_not == nil
25
+ end
26
+
27
+ it "should find concept by name" do
28
+ service.find_by_name("dog").should_not == nil
29
+ service.find_by_name("dog").should_not be_empty
30
+ end
31
+
32
+ it "should find concept by label" do
33
+ service.find_by_label("dog").should_not == nil
34
+ end
35
+
36
+ it "should convert Ruby term" do
37
+ term = service.convert_ruby_term(:Dog)
38
+ term.name.should == "Dog"
39
+ end
40
+
41
+ it "should transliterate non-ascii characters" do
42
+ service.find_by_term_name("Dóg").name.should == "Dog"
43
+ end
44
+
45
+ describe "with term" do
46
+ let(:term) { service.find_by_term_name("Dog") }
47
+
48
+ it "should return canonical label" do
49
+ service.canonical_label(term).should == "dog"
50
+ end
51
+
52
+ it "should return labels" do
53
+ service.labels(term).should include("dogs")
54
+ end
55
+ end
56
+ end
57
+
@@ -0,0 +1,123 @@
1
+ # encoding: utf-8
2
+ require 'iconv'
3
+
4
+ module Cyc
5
+ module Service
6
+ class AmbiguousResult < Exception
7
+ attr_reader :results
8
+
9
+ def initialize(results)
10
+ super("Ambiguous prefered label results")
11
+ @results = results
12
+ end
13
+ end
14
+
15
+ # Class used to find Cyc terms by their name.
16
+ class NameService
17
+ attr_accessor :cyc
18
+
19
+ def initialize(cyc_client=Client.new,term_factory=Term)
20
+ @cyc = cyc_client
21
+ @cyc.connect
22
+ @factory = term_factory
23
+ end
24
+
25
+ # Look-up by +name+, i.e. the exact name of the Cyc term.
26
+ def find_by_term_name(name)
27
+ if name =~ /\A\[/
28
+ convert_ruby_term(eval(name))
29
+ else
30
+ term = @cyc.find_constant(transliterate(name))
31
+ if term
32
+ id = @cyc.compact_hl_external_id_string(term)
33
+ @factory.new(term,id)
34
+ end
35
+ end
36
+ end
37
+
38
+ # Look-up by ID, i.e. external identifier of the term.
39
+ def find_by_id(id)
40
+ begin
41
+ term = @cyc.find_cycl_object_by_compact_hl_external_id_string(id)
42
+ if term and term!='<The'
43
+ @factory.new(term,id)
44
+ end
45
+ rescue CycError
46
+ nil
47
+ end
48
+ end
49
+
50
+ # Approximate find by name - the result is ambiguous, so the result is an
51
+ # array.
52
+ def find_by_name(name)
53
+ result = @cyc.denotation_mapper(transliterate(name))
54
+ if result
55
+ result.select{|label,_,_| label == name }.
56
+ map{|_,_,ruby_term| convert_ruby_term(ruby_term) }
57
+ else
58
+ []
59
+ end
60
+ end
61
+
62
+ # Find term by prefered +label+. In rare cases the result is ambiguous - an
63
+ # AmbiguousResult is raised then. You can get the result, by calling 'result' on
64
+ # the returned exception.
65
+ def find_by_label(label)
66
+ result = @cyc.cyc_query( -> { '`(#$prettyString-Canonical ?s ' + transliterate(label).to_cyc + ')' }, :EnglishMt)
67
+ if result
68
+ result.map!{|e| convert_ruby_term(extract_term_name(e.first))}
69
+ if result.size == 1
70
+ result.first
71
+ else
72
+ raise AmbiguousResult.new(result)
73
+ end
74
+ end
75
+ end
76
+
77
+ # Returns canonical label for the +term+.
78
+ def canonical_label(term)
79
+ (@cyc.cyc_query(-> { '\'(#$prettyString-Canonical ' + term.to_cyc(true) + ' ?s)'}, :EnglishMt) || []).
80
+ map{|e| e.first.last }.first
81
+ end
82
+
83
+ # Returns all (non-canonical) labels for the +term+.
84
+ def labels(term)
85
+ begin
86
+ (@cyc.cyc_query(-> { '\'(#$prettyString ' + term.to_cyc(true) + ' ?s)'}, :EnglishMt) || []).
87
+ map{|e| e.first.last }
88
+ rescue CycError
89
+ []
90
+ end
91
+ end
92
+
93
+ # Close connection to Cyc server.
94
+ def close
95
+ @cyc.close
96
+ end
97
+
98
+ # Convert Ruby term (e.g. :Dog) to cyc term representation.
99
+ def convert_ruby_term(term)
100
+ id = @cyc.compact_hl_external_id_string(term)
101
+ if id
102
+ @factory.new(term,id)
103
+ end
104
+ end
105
+
106
+ private
107
+ # Extracts term name from a result that might be either Lisp pair or Lisp
108
+ # list.
109
+ def extract_term_name(expression)
110
+ if expression[1] == "."
111
+ expression[2]
112
+ else
113
+ expression[1..-1]
114
+ end
115
+ end
116
+
117
+ # Transliterate non-ascii characters.
118
+ def transliterate(string)
119
+ Iconv.iconv('ascii//translit//ignore', 'utf-8', string).join("")
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,49 @@
1
+ module Cyc
2
+ class Term
3
+ # Create new Term using Ruby representation of the term (Symbol or
4
+ # Array).
5
+ def initialize(ruby_term,id)
6
+ @ruby_term = ruby_term
7
+ @id = id
8
+ end
9
+
10
+ # Return Ruby representation of the term.
11
+ def to_ruby
12
+ @ruby_term
13
+ end
14
+
15
+ # Return external ID of the term.
16
+ def id
17
+ @id
18
+ end
19
+
20
+ # Return Cyc-protocol compatible representation of the term.
21
+ def to_cyc(raw=false)
22
+ self.to_ruby.to_cyc(raw)
23
+ end
24
+
25
+ # Inspect the term.
26
+ def inspect
27
+ "#{@ruby_term}:#{@id}"
28
+ end
29
+
30
+ def name
31
+ self.to_ruby.to_s
32
+ end
33
+
34
+ def ==(other)
35
+ return false if !(self.class === other)
36
+ @ruby_term == other.to_ruby
37
+ end
38
+
39
+ def eql?(other)
40
+ self == other
41
+ end
42
+
43
+ def hash
44
+ @ruby_term.hash
45
+ end
46
+
47
+ alias to_s inspect
48
+ end
49
+ end
@@ -1,3 +1,3 @@
1
1
  module Cyc
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
@@ -10,3 +10,5 @@ require 'cyc/symbol'
10
10
  require 'cyc/variable'
11
11
  require 'cyc/version'
12
12
  require 'cyc/cache'
13
+ require 'cyc/term'
14
+ require 'cyc/service/name_service'
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cycr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
5
- prerelease:
4
+ version: 0.2.7
6
5
  platform: ruby
7
6
  authors:
8
7
  - Aleksander Pohl
@@ -10,12 +9,11 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2014-04-12 00:00:00.000000000 Z
12
+ date: 2015-08-21 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rspec
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
18
  - - ~>
21
19
  - !ruby/object:Gem::Version
@@ -23,7 +21,6 @@ dependencies:
23
21
  type: :development
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
25
  - - ~>
29
26
  - !ruby/object:Gem::Version
@@ -31,7 +28,6 @@ dependencies:
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: em-synchrony
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
32
  - - ~>
37
33
  - !ruby/object:Gem::Version
@@ -39,7 +35,6 @@ dependencies:
39
35
  type: :development
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
39
  - - ~>
45
40
  - !ruby/object:Gem::Version
@@ -47,7 +42,6 @@ dependencies:
47
42
  - !ruby/object:Gem::Dependency
48
43
  name: ref
49
44
  requirement: !ruby/object:Gem::Requirement
50
- none: false
51
45
  requirements:
52
46
  - - ~>
53
47
  - !ruby/object:Gem::Version
@@ -55,7 +49,6 @@ dependencies:
55
49
  type: :runtime
56
50
  prerelease: false
57
51
  version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
52
  requirements:
60
53
  - - ~>
61
54
  - !ruby/object:Gem::Version
@@ -74,6 +67,7 @@ files:
74
67
  - cycr.gemspec
75
68
  - integration/assertion.rb
76
69
  - integration/client.rb
70
+ - integration/name_service.rb
77
71
  - lib/cyc/assertion.rb
78
72
  - lib/cyc/builder.rb
79
73
  - lib/cyc/cache.rb
@@ -87,9 +81,11 @@ files:
87
81
  - lib/cyc/exception.rb
88
82
  - lib/cyc/extensions.rb
89
83
  - lib/cyc/parser.rb
84
+ - lib/cyc/service/name_service.rb
90
85
  - lib/cyc/sexpr.rex
91
86
  - lib/cyc/sexpr.rex.rb
92
87
  - lib/cyc/symbol.rb
88
+ - lib/cyc/term.rb
93
89
  - lib/cyc/variable.rb
94
90
  - lib/cyc/version.rb
95
91
  - lib/cycr.rb
@@ -97,6 +93,7 @@ files:
97
93
  - spec/parser.rb
98
94
  homepage: http://github.com/apohllo/cycr
99
95
  licenses: []
96
+ metadata: {}
100
97
  post_install_message:
101
98
  rdoc_options:
102
99
  - --main
@@ -104,26 +101,25 @@ rdoc_options:
104
101
  require_paths:
105
102
  - lib
106
103
  required_ruby_version: !ruby/object:Gem::Requirement
107
- none: false
108
104
  requirements:
109
105
  - - ! '>='
110
106
  - !ruby/object:Gem::Version
111
107
  version: 1.9.2
112
108
  required_rubygems_version: !ruby/object:Gem::Requirement
113
- none: false
114
109
  requirements:
115
110
  - - ! '>='
116
111
  - !ruby/object:Gem::Version
117
112
  version: '0'
118
113
  requirements: []
119
114
  rubyforge_project:
120
- rubygems_version: 1.8.25
115
+ rubygems_version: 2.2.2
121
116
  signing_key:
122
- specification_version: 3
117
+ specification_version: 4
123
118
  summary: Ruby client for the (Open)Cyc server
124
119
  test_files:
125
120
  - spec/parser.rb
126
121
  - spec/conversion.rb
122
+ - integration/name_service.rb
127
123
  - integration/assertion.rb
128
124
  - integration/client.rb
129
125
  has_rdoc: true