Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard van der Dys committed Sep 26, 2013
1 parent d7433e1 commit 123837f
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.DS_Store
node_modules/
build/
tests/*.js
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
hello

# Link Grammar Parser

A node library which interfaces a well known link grammar native library <http://www.link.cs.cmu.edu/link/>

The point of this project is to make the library easier to use, especially in node!

## Building Yourself

You will need [mocha](https://npmjs.org/package/mocha) and [kal](https://npmjs.org/package/kal) installed **globally** to build this project yourself.

After those are installed, you can just run:

```
npm run-script make
```

Which will clean, compile, and test the project.

## Usage

```
/**
* Load in the link grammar parser library
*/
Parser = require("link-grammar");
/**
* Gets grammar links
* @param {string} input - input sentence to parse
* @returns {array} - array of linkages which each contain grammar links
*/
links = Parser.getLinks "my name is sam"
/**
* Gets constituent tree
* @param {string} input - input sentence to parse
* @returns {object} - root node of tree, each node contains a label and which nodes it links to
*/
root = Parser.getTree "the dog ate my homework"
```

Link Grammar Documentation <http://www.link.cs.cmu.edu/link/dict/>
156 changes: 156 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
(function () {
var ffi, ref, Struct, pointerType, string, int, ParseOptions, Dictionary, Sentence, Linkage, CNode, CNodePtr, apiTemplate, libPath, lib, merge, getNodePtrFromPtr, defaultConfig;

/* # Link Grammar Parser
The parser is built using the awesome ffi library which allows us to communicate with a native library under the covers.
Along with ffi, we also use ref library to reference the native objects used by the library.
*/
ffi = require("ffi");
ref = require("ref");
Struct = require("ref-struct");
/*
Easier references to base types of data.
*/
pointerType = "pointer";
string = ref.types.CString;
int = ref.types.int;
/*
These are just references to different native structs, which are all just pointers because we never use their actual referenced object.
*/
ParseOptions = pointerType;
Dictionary = pointerType;
Sentence = pointerType;
Linkage = pointerType;
/* */
CNode = Struct({label: ref.types.CString, child: pointerType, next: pointerType, start: ref.types.int, end: ref.types.int});
/* */
CNodePtr = ref.refType(CNode);
/*
Here are the templates for the native functions we will use.
*/
apiTemplate = {parse_options_create: [ParseOptions, []], dictionary_create: [Dictionary, [string, string, string, string]], sentence_create: [Sentence, [string, Dictionary]], sentence_parse: [int, [Sentence, ParseOptions]], linkage_create: [Linkage, [int, Sentence, ParseOptions]], linkage_print_diagram: [string, [Linkage]], linkage_constituent_tree: [CNodePtr, [Linkage]], linkage_print_constituent_tree: [string, [Linkage, int]], linkage_get_num_links: [int, [Linkage]], linkage_get_link_label: [string, [Linkage, int]], linkage_get_link_llabel: [string, [Linkage, int]], linkage_get_link_rlabel: [string, [Linkage, int]], linkage_get_word: [string, [Linkage, int]], linkage_get_link_lword: [int, [Linkage, int]], linkage_get_link_rword: [int, [Linkage, int]]};
/*
Load the library.
*/
libPath = "./lib/libparser";
lib = ffi.Library(libPath, apiTemplate);
/*
Utility functions...
*/
merge = function (a, b) {
var c, key, ki$1, kobj$1, ki$2, kobj$2;
c = {};
kobj$1 = a;
for (key in kobj$1) {
if (!kobj$1.hasOwnProperty(key)) {continue;}
c[key] = a[key];
}
kobj$2 = b;
for (key in kobj$2) {
if (!kobj$2.hasOwnProperty(key)) {continue;}
c[key] = b[key];
}
return c;
};

/* */
String.prototype.contains = function (n) {
return this.indexOf(n) !== -1;
};

/* */
getNodePtrFromPtr = function (ptr) {
var tempPtr;
tempPtr = ref.alloc(CNodePtr);
ref.writePointer(tempPtr, 0, ptr);
return tempPtr.deref();
};

/*
Default configuration for data paths.
*/
defaultConfig = {dictPath: "./data/4.0.dict", ppPath: "./data/4.0.knowledge", consPath: "./data/4.0.constituent-knowledge", affixPath: "./data/4.0.affix"};

/*
Main parser class which interfaces the native library to make it very simple to get link grammar data from an input string.
*/
function Parser(config) {
this._config = merge(defaultConfig, config);
this._options = lib.parse_options_create();
this._dictionary = lib.dictionary_create(this._config.dictPath, this._config.ppPath, this._config.consPath, this._config.affixPath);
}

/* A few utility methods for the parser.
*/

/* */
Parser.prototype.buildNode = function (node, linkage) {
var n;
n = {label: node.label};
if (!(ref.isNull(node.child))) {
n.child = this.buildNode(getNodePtrFromPtr(node.child).deref(), linkage);
}
if (!(ref.isNull(node.next))) {
n.next = this.buildNode(getNodePtrFromPtr(node.next).deref(), linkage);
}
return n;
}

/* */
Parser.prototype.getLinkageLinks = function (linkage) {
var links, index, numLinks, link, temp;
links = [];
index = 0;
numLinks = lib.linkage_get_num_links(linkage);
while (index < numLinks) {
link = {label: lib.linkage_get_link_label(linkage, index), left: {label: lib.linkage_get_link_llabel(linkage, index), word: lib.linkage_get_word(linkage, lib.linkage_get_link_lword(linkage, index))}, right: {label: lib.linkage_get_link_rlabel(linkage, index), word: lib.linkage_get_word(linkage, lib.linkage_get_link_rword(linkage, index))}};
if (link.left.word.contains('.')) {
temp = link.left.word.split('.');
link.left.word = temp[0];
link.left.type = temp[1];
}
if (link.right.word.contains('.')) {
temp = link.right.word.split('.');
link.right.word = temp[0];
link.right.type = temp[1];
}
links.push(link);
index += 1;
}
return links;
}

/*
Get a tree of grammar nodes which map out how the input sentence is structered.
*/
Parser.prototype.getTree = function (input) {
var sentence, numLinkages, linkage, rootPtr;
sentence = lib.sentence_create(input, this._dictionary);
numLinkages = lib.sentence_parse(sentence, this._options);
linkage = lib.linkage_create(0, sentence, this._options);
rootPtr = lib.linkage_constituent_tree(linkage);
return this.buildNode(rootPtr.deref(), linkage);
}

/*
Get an array of linkages, each containing their grammar links. */
Parser.prototype.getLinks = function (input) {
var sentence, numLinkages, links, index, linkage;
sentence = lib.sentence_create(input, this._dictionary);
numLinkages = lib.sentence_parse(sentence, this._options);
links = [];
index = 0;
while (index < numLinkages) {
linkage = lib.linkage_create(index, sentence, this._options);
links.push(this.getLinkageLinks(linkage));
index += 1;
}
return links;
}

/*
Export the module, so others can use it.
*/
module.exports = Parser;
})()
34 changes: 25 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
{
"name": "link-grammer",
"version": "0.0.0",
"description": "ERROR: No README.md file found!",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "0.0.1",
"description": "Link grammar library node interface",
"main": "./build/index.js",
"repository": "",
"author": "",
"license": "BSD",
"author": "Richard van der Dys",
"keywords": ["link", "grammar", "word", "sentence", "parse"],
"licenses": [{
"type": "MIT",
"url": "https://raw.github.com/rompetoto/wiki/master/LICENSE"
}],
"engines": {
"node": ">=0.6.0"
},
"dependencies": {
"ffi": "latest",
"ref": "latest",
"ref-struct": "latest"
},
"homepage": "https://github.com/rompetoto/link-grammar",
"bugs": "https://github.com/rompetoto/link-grammar/issues",
"scripts": {
"clean": "rm -f build/*.js && rm -f tests/*.js",
"compile": "mkdir -p build/ && kal -o build/ src/*kal && kal -o tests/ tests/*.kal",
"test": "mocha -R spec -b tests/",
"make": "npm run-script clean && npm run-script compile && npm run-script test"
},
"repository": {
"type": "git",
"url": "git://github.com/rompetoto/link-grammar.git"
}
}
}
71 changes: 0 additions & 71 deletions src/index.kal

This file was deleted.

Loading

0 comments on commit 123837f

Please sign in to comment.