Skip to content

Instantly share code, notes, and snippets.

@TheDahv
Created September 22, 2020 02:06
Show Gist options
  • Save TheDahv/ec1a816b8b2a1b50931feb2908bbf664 to your computer and use it in GitHub Desktop.
Save TheDahv/ec1a816b8b2a1b50931feb2908bbf664 to your computer and use it in GitHub Desktop.
Example of using Bucklescript bindings and ReasonML for JavaScript package interop
type t;
[@bs.module] external make: string => t = "compromise";
module Extraction = {
/* return an array of meta-data for the adjectives and their adverbs */
[@bs.send] external adjectives: t => t = "adjectives";
/* return an array of meta-data for the adverbs in this text */
[@bs.send] external adverbs: t => t = "adverbs";
/* return an array of words split by sentence phrase (clause) */
[@bs.send] external clauses: t => t = "clauses";
/* return an array of parsed hashtags used in the text */
[@bs.send] external hashtags: t => t = "hashtags";
/* return an array of question sentences in this text */
[@bs.send] external questions: t => t = "questions";
/* return an array of sentences that are not questions */
[@bs.send] external statements: t => t = "statements";
/* split all words into individual results, and return their metadata */
[@bs.send] external terms: t => t = "terms";
/* return the people, places, and organizations of this text */
[@bs.send] external topics: t => t = "topics";
/* return an array of urls mentioned in this text */
[@bs.send] external urls: t => t = "urls";
};
module Nouns = {
/* return a handy array of meta-data for the nouns in this text */
[@bs.send] external get: t => t = "nouns";
/* return a handy array of meta-data of people mentioned in the text */
[@bs.send] external people: t => t = "people";
/* return an array of named-organizations in this text */
[@bs.send] external organizations: t => t = "organizations";
/* return an array of locations mentioned in this text */
[@bs.send] external places: t => t = "places";
};
module Matching = {
/* zoom-in to a subset of the text, using a regex-like match statement */
[@bs.send] external match_: (t, string) => t = "match";
/* zoom-in to a subset of the text, using a regex-like match statement */
[@bs.send] external found: (t, string) => bool = "found";
};
module Sentences = {
[@bs.send] external get: t => t = "sentences";
};
module Ngrams = {
[@bs.deriving abstract]
type options = {
max: int,
size: int,
};
[@bs.send] external get: (t, options) => t = "ngrams";
};
/* pretty-print the current selection to the console */
[@bs.send] external debug: t => unit = "debug";
[@bs.deriving jsConverter]
type format = [
| [@bs.as "text"] `Text
| [@bs.as "normal"] `Normal
| [@bs.as "array"] `Array
| [@bs.as "html"] `Html
| [@bs.as "grid"] `Grid
| [@bs.as "color"] `Color
| [@bs.as "debug"] `Debug
| [@bs.as "csv"] `Csv
];
/* TODO: Hide with interface file */
[@bs.send] external _out: (t, string) => string = "out";
/* render parsed data as an output */
let out = (stream, format) => _out(stream, formatToJs(format));
/* return a handy array of meta-data for this subset. Default subset is
* sentences, but it can be anything.
*/
[@bs.send] external data: t => Js.Array.t(Js.t({..})) = "data";
/* merge matches into one term, with shared tags. Should be chained with
* something like "all" and "out"
*/
[@bs.send] external lump: t => t = "lump";
/* set a particular interpretation for these terms. Can tag your match as
* anything. Supported tags do dependency/conflict logic.
*/
[@bs.send] external tag: (t, string) => t = "tag";
/* remove a particular tag for all these terms. Passing in a '*' removes all the
* current tags.
*/
[@bs.send] external unTag: (t, string) => t = "unTag";
/* return only terms that have no conflicts with this tag */
[@bs.send] external canBe: (t, string) => t = "canBe";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment