Debian Codegraph @

bindings/node.js/src/index.litcoffee

raw
1 # Node.js Bindings for Link Grammar
2
3 ## Building the bindings.
4 Build Link Grammar first. Then, in this directory, say:
5 ```
6 npm install
7 npm run make
8 ```
9 ## Using
10 The parser is built using the awesome ffi library which allows
11 us to communicate with a native library under the covers.
12 Along with ffi, we also use ref library to reference the native
13 objects used by the library.
14
15 ffi = require 'ffi'
16 ref = require 'ref'
17 Struct = require 'ref-struct'
18 _ = require 'underscore'
19
20 Easier references to base types of data.
21
22 pointerType = 'pointer'
23 string = ref.types.CString
24 int = ref.types.int
25
26 These are just references to different native structs, which are
27 all just pointers because we never use their actual referenced object.
28
29 ParseOptions = pointerType
30 Dictionary = pointerType
31 Sentence = pointerType
32 Linkage = pointerType
33
34 Here are the templates for the native functions we will use.
35
36 apiTemplate =
37 parse_options_create: [ ParseOptions, [ ] ]
38 parse_options_set_islands_ok: [ ref.types.void, [ ParseOptions, int ] ]
39 parse_options_set_verbosity: [ ref.types.void, [ ParseOptions, int ] ]
40 parse_options_set_min_null_count: [ ref.types.void, [ ParseOptions, int ] ]
41 parse_options_set_max_null_count: [ ref.types.void, [ ParseOptions, int ] ]
42 dictionary_create_lang: [ Dictionary, [ string ] ]
43 sentence_create: [ Sentence, [ string, Dictionary ] ]
44 sentence_parse: [ int, [ Sentence, ParseOptions ] ]
45 sentence_length: [ int, [ Sentence ] ]
46 linkage_create: [ Linkage, [ int, Sentence, ParseOptions ] ]
47 linkage_print_diagram: [ string, [ Linkage ] ]
48 linkage_get_num_links: [ int, [ Linkage ] ]
49 linkage_get_link_label: [ string, [ Linkage, int ] ]
50 linkage_get_link_llabel: [ string, [ Linkage, int ] ]
51 linkage_get_link_rlabel: [ string, [ Linkage, int ] ]
52 linkage_get_word: [ string, [ Linkage, int ] ]
53 linkage_get_link_lword: [ int, [ Linkage, int ] ]
54 linkage_get_link_rword: [ int, [ Linkage, int ] ]
55
56 Load the library.
57
58 libPath = __dirname + '/../../../build/link-grammar/.libs/liblink-grammar.so'
59 lib = ffi.Library libPath, apiTemplate
60 defaultDataPath = __dirname + '/../../../data/en/'
61
62 Default configuration for data paths.
63
64 defaultConfig =
65 lang: 'en'
66 verbose: false
67
68 Main parser class which interfaces the native library to make
69 it very simple to get Link Grammar data from an input string.
70
71 class LinkGrammar
72
73 A few utility methods for the parser.
74
75 constructor: (config) ->
76 @config = _.extend config or {}, defaultConfig
77 @options = lib.parse_options_create()
78 lib.parse_options_set_verbosity @options, (if @config.verbose then 1 else 0)
79 lib.parse_options_set_min_null_count @options, 0
80 lib.parse_options_set_max_null_count @options, 3
81 @dictionary = lib.dictionary_create_lang @config.lang
82
83 Parse input, and return linkage, if it exists.
84
85 parse: (input, index) ->
86 sentence = lib.sentence_create input, @dictionary
87 numLinkages = lib.sentence_parse sentence, @options
88 if numLinkages > 0
89 new Linkage lib.linkage_create index or 0, sentence, @options
90 else
91 throw new Error('No linkages found')
92
93 Linkage class which allows for more specific parsing of grammar.
94
95 class Linkage
96
97 constructor: (@linkage) ->
98 @links = @getLinks()
99 @words = @getWords()
100
101 Get array of grammar links based on linkage.
102
103 getLinks: ->
104 _(lib.linkage_get_num_links @linkage).times ((index) ->
105 leftIndex = lib.linkage_get_link_lword @linkage, index
106 rightIndex = lib.linkage_get_link_rword @linkage, index
107 link =
108 label: lib.linkage_get_link_label @linkage, index
109 left:
110 label: lib.linkage_get_link_llabel @linkage, index
111 word: lib.linkage_get_word @linkage, leftIndex
112 index: leftIndex
113 right:
114 label: lib.linkage_get_link_rlabel @linkage, index
115 word: lib.linkage_get_word @linkage, rightIndex
116 index: rightIndex
117 if link.left.word.indexOf '.' isnt -1
118 temp = link.left.word.split '.'
119 link.left.word = temp[0]
120 link.left.type = temp[1]
121 if link.right.word.indexOf '.' isnt -1
122 temp = link.right.word.split '.'
123 link.right.word = temp[0]
124 link.right.type = temp[1]
125 link
126 ), @
127
128 Get parsed words with their grammar type and index
129
130 getWords: ->
131 _.chain(@links)
132 .map (link) -> [link.left, link.right]
133 .flatten()
134 .uniq (link) -> link.word
135 .value()
136
137 Get list of links by specific label type
138
139 linksByLabel: (labelPattern) ->
140 labelPattern = new RegExp labelPattern if typeof labelPattern is 'string'
141 @links.filter (link) ->
142 labelPattern.test link.left.label or labelPattern.test link.right.label
143
144 Get list of connector links for a specific word
145
146 getConnectorWords: (common) ->
147 words = []
148 @links.forEach (link) ->
149 if link.left.word is common
150 words.push
151 source: link.left
152 target: link.right
153 if link.right.word is common
154 words.push
155 source: link.right
156 target: link.left
157 words
158
159 module.exports = LinkGrammar