cycr 0.0.3

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.
@@ -0,0 +1,74 @@
1
+ package pl.apohllo.lexicon.pwn.server;
2
+
3
+ import java.io.StringReader;
4
+ %%
5
+
6
+ %class Lexer
7
+ %unicode
8
+ %line
9
+ %public
10
+ %type Symbol
11
+
12
+ %{
13
+ StringBuffer string = new StringBuffer();
14
+
15
+ public Lexer(String input){
16
+ this.zzReader = new StringReader(input);
17
+ }
18
+
19
+ private Symbol symbol(int type) {
20
+ return new Symbol(type, yyline, yycolumn);
21
+ }
22
+ private Symbol symbol(int type, String value) {
23
+ return new Symbol(type, yyline, yycolumn, value);
24
+ }
25
+ %}
26
+
27
+ LineTerminator = \r|\n|\r\n
28
+ InputCharacter = [^\r\n\"\(\):& ]
29
+ WhiteSpace = {LineTerminator} | [ \t\f]
30
+
31
+ Symbol = : {InputCharacter}+
32
+
33
+ Atom = {InputCharacter}+
34
+
35
+ OpenPar = "("
36
+ ClosePar = ")"
37
+
38
+ %state STRING
39
+
40
+
41
+ %%
42
+
43
+ <YYINITIAL> {
44
+ /* keywords */
45
+ {OpenPar} { return symbol(Symbol.OPEN_PAR); }
46
+ {ClosePar} { return symbol(Symbol.CLOSE_PAR); }
47
+
48
+ /* identifiers */
49
+ {Symbol} { return symbol(Symbol.SYMBOL,yytext()); }
50
+ {Atom} { return symbol(Symbol.ATOM,yytext()); }
51
+
52
+
53
+ /* literals */
54
+ \" { string.setLength(0); yybegin(STRING); }
55
+
56
+ /* whitespace */
57
+ {WhiteSpace} { /* ignore */ }
58
+ }
59
+
60
+ <STRING> {
61
+ \" { yybegin(YYINITIAL);
62
+ return symbol(Symbol.STRING_LITERAL, string.toString()); }
63
+ [^\n\r\"\\]+ { string.append( yytext() ); }
64
+ \\t { string.append('\t'); }
65
+ \\n { string.append('\n'); }
66
+
67
+ \\r { string.append('\r'); }
68
+ \\\" { string.append('\"'); }
69
+ \\ { string.append('\\'); }
70
+ }
71
+
72
+ /* error fallback */
73
+ .|\n { throw new Error("Illegal character <"+ yytext()+">"); }
74
+
@@ -0,0 +1,55 @@
1
+ class Cyc::SExpressionLexer
2
+
3
+ option
4
+ independent
5
+
6
+ macro
7
+ LINE_TERMINATOR \r|\n|\r\n
8
+ INPUT_CHARACTER [^\r\n\"\(\):& ]
9
+ WHITE_SPACE [\ \t\f\r\n] | \r\n
10
+ SYMBOL :[^<>\r\n\"\(\):&\ ]+
11
+ CYC_SYMBOL \#\$[a-zA-Z0-9-]+
12
+ ATOM [^\r\n\"\(\):&\ ]+
13
+ OPEN_PAR \(
14
+ CLOSE_PAR \)
15
+ QUOTE \"
16
+ OPEN_AS_QUOTE \#<AS:
17
+ OPEN_LIST_QUOTE \#<
18
+ CLOSE_LIST_QUOTE >
19
+ DOT \.
20
+
21
+
22
+ rule
23
+ # lists
24
+ {OPEN_AS_QUOTE} { [:open_as,text] }
25
+ {OPEN_LIST_QUOTE} { [:open_quote,text] }
26
+ {CLOSE_LIST_QUOTE} { [:close_quote,text] }
27
+ {DOT}{DOT}{DOT} { [:continuation] }
28
+ # keywords
29
+ {OPEN_PAR} { [:open_par,text] }
30
+ {CLOSE_PAR} { [:close_par,text] }
31
+ NIL { [:nil,text] }
32
+ # identifiers
33
+ {SYMBOL} { [:symbol,text] }
34
+ {CYC_SYMBOL} { [:cyc_symbol,text] }
35
+ {ATOM} { [:atom,text] }
36
+ # literals
37
+ {QUOTE} { state = :STRING; @str = ""; [:in_string] }
38
+ # whitespace
39
+ {WHITE_SPACE} # ignore
40
+ :STRING {QUOTE} { state = nil; [:string,@str] }
41
+ :STRING [^\n\r\"\\]+ { @str << text; [:in_string]}
42
+ :STRING \t { @str << '\t'; [:in_string] }
43
+ :STRING \n { @str << '\n'; [:in_string] }
44
+ :STRING \r { @str << '\r'; [:in_string] }
45
+ :STRING \\" { @str << '\"'; [:in_string] }
46
+ :STRING \\ { @str << '\\'; [:in_string] }
47
+ # error fallback
48
+ .|\n { raise "Illegal character <#{text}>" }
49
+ inner
50
+ def do_parse
51
+ # while !(token = next_token).nil?
52
+ # print " #{token[0]}:#{token[1]}" unless token[0] == :in_string
53
+ # end
54
+ end
55
+ end
@@ -0,0 +1,136 @@
1
+ #--
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by rex 1.0.2
4
+ # from lexical definition file "sexpr.rex".
5
+ #++
6
+
7
+ module Cyc
8
+ class SExpressionLexer
9
+ require 'strscan'
10
+
11
+ class ScanError < StandardError ; end
12
+
13
+ attr_reader :lineno
14
+ attr_reader :filename
15
+
16
+ def scan_setup ; end
17
+
18
+ def action &block
19
+ yield
20
+ end
21
+
22
+ def scan_str( str )
23
+ scan_evaluate str
24
+ do_parse
25
+ end
26
+
27
+ def load_file( filename )
28
+ @filename = filename
29
+ open(filename, "r") do |f|
30
+ scan_evaluate f.read
31
+ end
32
+ end
33
+
34
+ def scan_file( filename )
35
+ load_file filename
36
+ do_parse
37
+ end
38
+
39
+ def next_token
40
+ @rex_tokens.shift
41
+ end
42
+
43
+ def scan_evaluate( str )
44
+ scan_setup
45
+ @rex_tokens = []
46
+ @lineno = 1
47
+ ss = StringScanner.new(str)
48
+ state = nil
49
+ until ss.eos?
50
+ text = ss.peek(1)
51
+ @lineno += 1 if text == "\n"
52
+ case state
53
+ when nil
54
+ case
55
+ when (text = ss.scan(/\#<AS:/))
56
+ @rex_tokens.push action { [:open_as,text] }
57
+
58
+ when (text = ss.scan(/\#</))
59
+ @rex_tokens.push action { [:open_quote,text] }
60
+
61
+ when (text = ss.scan(/>/))
62
+ @rex_tokens.push action { [:close_quote,text] }
63
+
64
+ when (text = ss.scan(/\.\.\./))
65
+ @rex_tokens.push action { [:continuation] }
66
+
67
+ when (text = ss.scan(/\(/))
68
+ @rex_tokens.push action { [:open_par,text] }
69
+
70
+ when (text = ss.scan(/\)/))
71
+ @rex_tokens.push action { [:close_par,text] }
72
+
73
+ when (text = ss.scan(/NIL/))
74
+ @rex_tokens.push action { [:nil,text] }
75
+
76
+ when (text = ss.scan(/:[^<>\r\n\"\(\):&\ ]+/))
77
+ @rex_tokens.push action { [:symbol,text] }
78
+
79
+ when (text = ss.scan(/\#\$[a-zA-Z0-9-]+/))
80
+ @rex_tokens.push action { [:cyc_symbol,text] }
81
+
82
+ when (text = ss.scan(/[^\r\n\"\(\):&\ ]+/))
83
+ @rex_tokens.push action { [:atom,text] }
84
+
85
+ when (text = ss.scan(/\"/))
86
+ @rex_tokens.push action { state = :STRING; @str = ""; [:in_string] }
87
+
88
+ when (text = ss.scan(/[\ \t\f\r\n]/))
89
+ ;
90
+
91
+ when (text = ss.scan(/.|\n/))
92
+ @rex_tokens.push action { raise "Illegal character <#{text}>" }
93
+
94
+ else
95
+ text = ss.string[ss.pos .. -1]
96
+ raise ScanError, "can not match: '" + text + "'"
97
+ end # if
98
+
99
+ when :STRING
100
+ case
101
+ when (text = ss.scan(/\"/))
102
+ @rex_tokens.push action { state = nil; [:string,@str] }
103
+
104
+ when (text = ss.scan(/[^\n\r\"\\]+/))
105
+ @rex_tokens.push action { @str << text; [:in_string]}
106
+
107
+ when (text = ss.scan(/\t/))
108
+ @rex_tokens.push action { @str << '\t'; [:in_string] }
109
+
110
+ when (text = ss.scan(/\n/))
111
+ @rex_tokens.push action { @str << '\n'; [:in_string] }
112
+
113
+ when (text = ss.scan(/\r/))
114
+ @rex_tokens.push action { @str << '\r'; [:in_string] }
115
+
116
+ when (text = ss.scan(/\\"/))
117
+ @rex_tokens.push action { @str << '\"'; [:in_string] }
118
+
119
+ when (text = ss.scan(/\\/))
120
+ @rex_tokens.push action { @str << '\\'; [:in_string] }
121
+
122
+ else
123
+ text = ss.string[ss.pos .. -1]
124
+ raise ScanError, "can not match: '" + text + "'"
125
+ end # if
126
+
127
+ else
128
+ raise ScanError, "undefined state: '" + state.to_s + "'"
129
+ end # case state
130
+ end # until ss
131
+ end # def scan_evaluate
132
+
133
+ def do_parse
134
+ end
135
+ end # class
136
+ end # module
@@ -0,0 +1,204 @@
1
+ ;(csetq *PATH* "/home/fox/src/nlp/wsd/cyc/~A")
2
+ (csetq *PATH* "/mnt/cyc/export/~A")
3
+ ;(csetq *WORDS-PATH* (format nil *PATH* "m_100_en_hasla.txt"))
4
+ (csetq *WORDS-PATH* (format nil *PATH* "all_english.txt"))
5
+ (csetq *WORDS-FILE* "")
6
+ (csetq *WORDS-OUTPUT* "")
7
+ (csetq *WORDS-OUTPUT-PROP* "")
8
+ (csetq *WORDS-MISSING* "")
9
+
10
+ (define load-code () (load (format nil *PATH* "words_reader.lisp")))
11
+ (define next-word ()
12
+ (clet (
13
+ (word (read *WORDS-FILE* nil))
14
+ )
15
+ (pif (null word) (ret '(("EOF" :eof))) ())
16
+ (clet (
17
+ (denotations (denotation-mapper (format nil "~A" word)))
18
+ (correct-denots ())
19
+ )
20
+ (pif (consp denotations)
21
+ ()
22
+ (princ (format nil "~A~%" word) *WORDS-MISSING*)
23
+ )
24
+ ; (csetq *PROP-WORD* word)
25
+ (cdolist (denot denotations)
26
+ (pif (denot-word-p denot word) (cpush denot correct-denots) nil)
27
+ )
28
+ ; (csetq denotations (mremove-if #'prop-word-p denotations))
29
+ (ret (reverse correct-denots))
30
+ )
31
+ )
32
+ )
33
+
34
+ (define symbol-str (symbol)
35
+ (ret (cdr (caar
36
+ (cyc-query `(#$prettyString-Canonical ,symbol ?w) #$EnglishMt)
37
+ )))
38
+ )
39
+
40
+ ; same as above but prettyString (without canonical) is used,
41
+ ; so it can return many results
42
+ (define symbol-strs (symbol)
43
+ (clet ((result ()))
44
+ (cdolist (el (cyc-query `(#$prettyString ,symbol ?w) #$EnglishMt))
45
+ (cpush (cdar el) result))
46
+ (ret result))
47
+ )
48
+
49
+ (define print-names (symbols output)
50
+ (cdolist (el symbols)
51
+ (princ (format nil "~A" el) output)
52
+ (princ (format nil " ~S"
53
+ (strip-s (symbol-str el) "type of "))
54
+ output)
55
+ ; (terpri output)
56
+ )
57
+ )
58
+ (define min-genls-flat (concept stop-words)
59
+ ;(ret (remove-invalid (min-genls concept #$EverythingPSC) stop-words))
60
+ ;CycNounLearnerMt UniversalVocabularyMt
61
+ (ret (remove-invalid (min-genls concept #$CycNounLearnerMt) stop-words))
62
+ )
63
+ (define min-isa-flat (concept stop-words)
64
+ ;(ret (remove-invalid (min-isa concept #$EverythingPSC) stop-words))
65
+ (ret (remove-invalid (min-isa concept #$CycNounLearnerMt) stop-words))
66
+ )
67
+
68
+ (define read-all ()
69
+ (clet (
70
+ (stop-words (read-stop))
71
+ (output nil)
72
+ )
73
+ (cdo ((words (next-word) (next-word)))
74
+ ((equal words '(("EOF" :eof))))
75
+ (cdolist (word words)
76
+ (pif (consp word)
77
+ (progn
78
+ (pif (isa? (cdr word) #$Individual)
79
+ (csetq output *WORDS-OUTPUT-PROP*)
80
+ (csetq output *WORDS-OUTPUT*))
81
+ (princ (format nil "~A[~A]"
82
+ (car word)
83
+ (cdr word)
84
+ )
85
+ output)
86
+ (print-names (min-genls-flat (cdr word) stop-words) output)
87
+ (princ " ISA" output)
88
+ (print-names (min-isa-flat (cdr word) stop-words) output)
89
+ (terpri output)
90
+ )
91
+ ()
92
+ )
93
+ )
94
+ )
95
+ )
96
+ )
97
+
98
+ ; my implementation of the mapcar function
99
+ (defmacro mmapcar (fun elems)
100
+ (clet ((var (gensym)))
101
+ (ret `(clet ((result nil))
102
+ (cdolist (,var ,elems)
103
+ (cpush (funcall ,fun ,var ) result) )
104
+ (reverse result))
105
+ )
106
+ )
107
+ )
108
+
109
+ ; removes elements from list if the fun returns nil
110
+ (define mremove-if (fun elems)
111
+ (remove-if #'null (mmapcar fun elems))
112
+ )
113
+
114
+ ;T if denotation's word equals word
115
+ (define denot-word-p (denot word)
116
+ (fif (equal (car denot) word) denot nil))
117
+
118
+ ;T if the car of the cons is equal to word
119
+ (define prop-word-p (el word)
120
+ (fif (equal (car (eval el)) word) (eval el) nil))
121
+
122
+
123
+ (define open-words ()
124
+ (csetq *WORDS-FILE* (open-text *WORDS-PATH* :input))
125
+ )
126
+
127
+ (define open-missing ()
128
+ (csetq *WORDS-MISSING* (open-text (format nil *PATH* "missing.txt") :output))
129
+ )
130
+
131
+ (define close-words ()
132
+ (close *WORDS-FILE*))
133
+
134
+
135
+ (define open-output (version)
136
+ (csetq *WORDS-OUTPUT*
137
+ (open-text (format nil *PATH* (format nil "pohl_cyc_~A.txt" version)) :output))
138
+ (csetq *WORDS-OUTPUT-PROP*
139
+ (open-text (format nil *PATH* (format nil "pohl_cyc_prop_~A.txt" version)) :output))
140
+ )
141
+
142
+ ;close output file
143
+ (define close-output ()
144
+ (close *WORDS-OUTPUT*)
145
+ (close *WORDS-OUTPUT-PROP*)
146
+ )
147
+
148
+ ;returns the stop-list
149
+ (define read-stop ()
150
+ (clet ((stop-file (open-text (format nil *PATH* "stop_list.txt") :input))
151
+ (stop-words (read stop-file))
152
+ )
153
+ (close stop-file)
154
+ (ret stop-words)
155
+ )
156
+ )
157
+
158
+ ; removes sequence #removed from the beginning of the sequence #sequence
159
+ (define strip-s (sequence removed)
160
+ (ret(fif (eq 0 (search removed sequence))
161
+ (subseq sequence (length removed))
162
+ sequence))
163
+ )
164
+
165
+ ;main transforming loop
166
+ (define main-prog (version)
167
+ (open-words)
168
+ (open-output version)
169
+ (open-missing)
170
+ (read-all)
171
+ (close-words)
172
+ (close-output)
173
+ (close *WORDS-MISSING*)
174
+ )
175
+
176
+ (define remove-invalid (elements invalid)
177
+ (clet ( (result () ) )
178
+ (cdolist (el elements)
179
+ (pif (null (cor (member el invalid)
180
+ (cand (nart-p el)
181
+ (intersection invalid (nart-hl-formula el)) )))
182
+ (cpush el result) ())
183
+ )
184
+ (ret result)
185
+ )
186
+ )
187
+
188
+ (define term-mts (term)
189
+ (clet ((result ()))
190
+ (cdolist (e (gather-index-in-any-mt term))
191
+ (csetq result (adjoin (assertion-mt e) result)))
192
+ result)
193
+ )
194
+
195
+ (define any-assertion? (term mt)
196
+ (with-mt mt (pif (gather-index term) T ())))
197
+
198
+ (define nart-denotation-mapper (word)
199
+ (clet ((result ()))
200
+ (cdolist (el (denotation-mapper word))
201
+ (pif (nart-p (cdr el))
202
+ (cpush (nart-id (cdr el)) result)
203
+ (cpush (cdr el) result))) result
204
+ (ret result)))
data/lib/cycr.rb ADDED
@@ -0,0 +1 @@
1
+ Dir.glob(File.join(File.dirname(__FILE__), 'cycr/**.rb')).each { |f| require f }
data/spec/assertion.rb ADDED
@@ -0,0 +1,36 @@
1
+ $:.unshift "lib"
2
+ require 'cycr'
3
+
4
+ describe Cyc::Assertion do
5
+ before(:all) do
6
+ @client = Cyc::Client.new()
7
+ #@client.debug = true
8
+ end
9
+
10
+ after(:all) do
11
+ @client.close
12
+ end
13
+
14
+ it "should parse results with assertions" do
15
+ @client.talk("(gather-predicate-extent-index \#$minimizeExtent \#$BaseKB)").should_not == nil
16
+ end
17
+
18
+ it "should return assertions as results if present in the result" do
19
+ @client.talk("(gather-predicate-extent-index \#$minimizeExtent \#$BaseKB)").
20
+ first.class.should == Cyc::Assertion
21
+ end
22
+
23
+ it "should have microtheory assigned" do
24
+ @client.talk("(gather-predicate-extent-index \#$minimizeExtent \#$BaseKB)").
25
+ first.microtheory.should_not == nil
26
+ end
27
+
28
+ it "should have formula assigned" do
29
+ @client.talk("(gather-predicate-extent-index \#$minimizeExtent \#$BaseKB)").
30
+ first.formula.should_not == nil
31
+ end
32
+
33
+ it "should return many results" do
34
+ @client.talk("(gather-predicate-extent-index \#$minimizeExtent)").size.should > 100
35
+ end
36
+ end
data/spec/client.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'lib/cycr'
2
+
3
+ describe Cyc::Client do
4
+ before(:each) do
5
+ @client = Cyc::Client.new()
6
+ # @client.debug = true
7
+ end
8
+
9
+ after(:each) do
10
+ @client.close
11
+ end
12
+
13
+ it "should allow to talk to the server" do
14
+ @client.talk("(constant-count)").should_not == nil
15
+ end
16
+
17
+ it "should allow to talk to server by calling non-existent methods" do
18
+ @client.constant_count.should_not == nil
19
+ end
20
+
21
+ it "should allow to find 'Cat' collection" do
22
+ cat = @client.find_collection("Cat")
23
+ cat.should_not == nil
24
+ cat.should be_instance_of(Cyc::Collection)
25
+ cat.symbol.should == :Cat
26
+ end
27
+
28
+ it "should allow multiple processes to use the client" do
29
+ parent_pid = Process.pid
30
+ if fork
31
+ @client.find_collection("Cat")
32
+ else
33
+ @client.find_collection("Dog")
34
+ end
35
+ if Process.pid == parent_pid
36
+ Process.waitall
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cycr
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
11
+ platform: ruby
12
+ authors:
13
+ - Aleksander Pohl
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-19 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Ruby wrapper for (Open)Cyc server and ontology
38
+ email: apohllo@o2.pl
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README.txt
45
+ files:
46
+ - Rakefile
47
+ - cycr.gemspec
48
+ - lib/cycr.rb
49
+ - lib/cycr/sexpr.rex
50
+ - lib/cycr/parser.rb
51
+ - lib/cycr/domains.lisp
52
+ - lib/cycr/sexpr.flex
53
+ - lib/cycr/nart.rb
54
+ - lib/cycr/constants.rb
55
+ - lib/cycr/collection.rb
56
+ - lib/cycr/sexpr.rex.rb
57
+ - lib/cycr/client.rb
58
+ - lib/cycr/extensions.rb
59
+ - lib/cycr/assertion.rb
60
+ - lib/cycr/words_reader.lisp
61
+ - README.txt
62
+ - spec/client.rb
63
+ - spec/assertion.rb
64
+ has_rdoc: true
65
+ homepage: http://www.opencyc.org
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --main
71
+ - README.txt
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.7
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Ruby client for the (Open)Cyc server
99
+ test_files:
100
+ - spec/client.rb
101
+ - spec/assertion.rb