Skip to content

Commit

Permalink
Don't crash if link grammar cannot find links for input
Browse files Browse the repository at this point in the history
Added test for "bad" input
  • Loading branch information
dijs committed Nov 22, 2015
1 parent 9dbe721 commit 3f3ae39
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
10 changes: 7 additions & 3 deletions build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions src/index.litcoffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Easier references to base types of data.
These are just references to different native structs, which are all just pointers because we never use their actual referenced object.

ParseOptions = pointerType
ParseOptions = pointerType
Dictionary = pointerType
Sentence = pointerType
Linkage = pointerType
Expand Down Expand Up @@ -78,7 +78,7 @@ Default configuration for data paths.
Main parser class which interfaces the native library to make it very simple to get link grammar data from an input string.

class LinkGrammar
A few utility methods for the parser.

constructor: (config) ->
Expand All @@ -87,12 +87,15 @@ A few utility methods for the parser.
lib.parse_options_set_verbosity @options, (if @config.verbose then 1 else 0)
@dictionary = lib.dictionary_create @config.dictPath, @config.ppPath, @config.consPath, @config.affixPath
Parse input, and return linkage.
Parse input, and return linkage if exists.

parse: (input, index) ->
sentence = lib.sentence_create input, @dictionary
numLinkages = lib.sentence_parse sentence, @options
new Linkage lib.linkage_create index or 0, sentence, @options
numLinkages = lib.sentence_parse sentence, @options
if numLinkages > 0
new Linkage lib.linkage_create index or 0, sentence, @options
else
throw new Error('No links found')
Linkage class which allows for more specific parsing of grammar.

Expand Down Expand Up @@ -146,14 +149,14 @@ Get array of grammar links based on linkage.
link.right.type = temp[1]
link
), @
Get parsed words with their grammar type and index

getWords: ->
_.chain(@links)
.map (link) -> [link.left, link.right]
.flatten()
.uniq (link) -> link.word
.uniq (link) -> link.word
.value()
Get list of links by specific label type
Expand All @@ -177,5 +180,5 @@ Get list of connector links for a specific word
source: link.right
target: link.left
words
module.exports = LinkGrammar
module.exports = LinkGrammar
11 changes: 7 additions & 4 deletions src/test/spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ describe 'Link Grammar Tests', ->
require 'should'

linkGrammar = new LinkGrammar()

linkage = linkGrammar.parse 'turn off the light'


it 'should not crash', ->
(-> linkGrammar.parse('string')).should.throw('No links found')

it 'should have the correct number of links', ->
linkage.links[2].left.word.should.equal 'turn'
linkage.links.length.should.equal 5

it 'should have the correct tree', ->
linkage.tree.child.child.label.should.equal 'turn'

it 'should have word list', ->
linkage.words.length.should.equal 6

Expand All @@ -30,4 +33,4 @@ describe 'Link Grammar Tests', ->
connectors[1].source.label.should.equal 'J'
connectors[1].target.label.should.equal 'Jp'
connectors[1].target.word.should.equal 'light'
connectors[1].target.type.should.equal 'n'
connectors[1].target.type.should.equal 'n'

0 comments on commit 3f3ae39

Please sign in to comment.