@gram-data/gram-ops
Advanced tools
Comparing version 0.2.9 to 0.2.10
@@ -5,6 +5,7 @@ 'use strict'; | ||
var gramAst = require('@gram-data/gram-ast'); | ||
var gramBuilder = require('@gram-data/gram-builder'); | ||
var count = function count(p) { | ||
return p === undefined ? 0 : p.children === undefined ? 1 : p.children.reduce(function (acc, child) { | ||
return p.children.reduce(function (acc, child) { | ||
return acc + count(child); | ||
@@ -20,8 +21,8 @@ }, 1); | ||
var nodes = function nodes(p) { | ||
return p === undefined ? [] : p.children === undefined || p.children.length === 0 ? [p] : p.children.reduce(function (acc, child) { | ||
return p.children.reduce(function (acc, child) { | ||
return [].concat(acc, nodes(child)); | ||
}, []); | ||
}, gramAst.isGramNode(p) ? [p] : []); | ||
}; | ||
var edges = function edges(p) { | ||
return p === undefined ? [] : p.children === undefined || p.children.length === 0 ? [] : p.children.length === 2 ? [].concat(edges(p.children[0]), p.relation !== undefined && p.relation !== 'pair' ? [gramBuilder.edge([tail(p.children[0]), head(p.children[1])], p.relation, p.id, p.labels, p.record)] : [], edges(p.children[1])) : p.children.reduce(function (acc, child) { | ||
return p === undefined ? [] : p.children === undefined || p.children.length === 0 ? [] : p.children.length === 2 ? [].concat(edges(p.children[0]), p.kind !== undefined && p.kind !== 'pair' ? [gramBuilder.edge([tail(p.children[0]), head(p.children[1])], p.kind, p.id, p.labels, p.record)] : [], edges(p.children[1])) : p.children.reduce(function (acc, child) { | ||
return [].concat(acc, edges(child)); | ||
@@ -28,0 +29,0 @@ }, []); |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@gram-data/gram-builder"),r=function e(r){return void 0===r.children||0===r.children.length?r:e(r.children[0])},n=function e(r){return void 0===r.children||0===r.children.length?r:e(r.children[r.children.length-1])};exports.count=function e(r){return void 0===r?0:void 0===r.children?1:r.children.reduce((function(r,n){return r+e(n)}),1)},exports.edges=function i(d){return void 0===d||void 0===d.children||0===d.children.length?[]:2===d.children.length?[].concat(i(d.children[0]),void 0!==d.relation&&"pair"!==d.relation?[e.edge([n(d.children[0]),r(d.children[1])],d.relation,d.id,d.labels,d.record)]:[],i(d.children[1])):d.children.reduce((function(e,r){return[].concat(e,i(r))}),[])},exports.head=r,exports.nodes=function e(r){return void 0===r?[]:void 0===r.children||0===r.children.length?[r]:r.children.reduce((function(r,n){return[].concat(r,e(n))}),[])},exports.tail=n; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@gram-data/gram-ast"),r=require("@gram-data/gram-builder"),n=function e(r){return void 0===r.children||0===r.children.length?r:e(r.children[0])},i=function e(r){return void 0===r.children||0===r.children.length?r:e(r.children[r.children.length-1])};exports.count=function e(r){return r.children.reduce((function(r,n){return r+e(n)}),1)},exports.edges=function e(d){return void 0===d||void 0===d.children||0===d.children.length?[]:2===d.children.length?[].concat(e(d.children[0]),void 0!==d.kind&&"pair"!==d.kind?[r.edge([i(d.children[0]),n(d.children[1])],d.kind,d.id,d.labels,d.record)]:[],e(d.children[1])):d.children.reduce((function(r,n){return[].concat(r,e(n))}),[])},exports.head=n,exports.nodes=function r(n){return n.children.reduce((function(e,n){return[].concat(e,r(n))}),e.isGramNode(n)?[n]:[])},exports.tail=i; | ||
//# sourceMappingURL=gram-ops.cjs.production.min.js.map |
@@ -1,6 +0,6 @@ | ||
import { GramNode, GramPath, GramPathlike } from '@gram-data/gram-ast'; | ||
export declare const count: (p: GramPathlike) => number; | ||
export declare const head: (p: GramPathlike) => GramNode; | ||
export declare const tail: (p: GramPathlike) => GramNode; | ||
export declare const nodes: (p: GramPathlike) => GramPath[]; | ||
export declare const edges: (p: GramPathlike) => GramPath[]; | ||
import { GramNode, GramPath } from '@gram-data/gram-ast'; | ||
export declare const count: (p: GramPath) => number; | ||
export declare const head: (p: GramPath) => GramNode; | ||
export declare const tail: (p: GramPath) => GramNode; | ||
export declare const nodes: (p: GramPath) => GramPath[]; | ||
export declare const edges: (p: GramPath) => GramPath[]; |
@@ -0,5 +1,6 @@ | ||
import { isGramNode } from '@gram-data/gram-ast'; | ||
import { edge } from '@gram-data/gram-builder'; | ||
var count = function count(p) { | ||
return p === undefined ? 0 : p.children === undefined ? 1 : p.children.reduce(function (acc, child) { | ||
return p.children.reduce(function (acc, child) { | ||
return acc + count(child); | ||
@@ -15,8 +16,8 @@ }, 1); | ||
var nodes = function nodes(p) { | ||
return p === undefined ? [] : p.children === undefined || p.children.length === 0 ? [p] : p.children.reduce(function (acc, child) { | ||
return p.children.reduce(function (acc, child) { | ||
return [].concat(acc, nodes(child)); | ||
}, []); | ||
}, isGramNode(p) ? [p] : []); | ||
}; | ||
var edges = function edges(p) { | ||
return p === undefined ? [] : p.children === undefined || p.children.length === 0 ? [] : p.children.length === 2 ? [].concat(edges(p.children[0]), p.relation !== undefined && p.relation !== 'pair' ? [edge([tail(p.children[0]), head(p.children[1])], p.relation, p.id, p.labels, p.record)] : [], edges(p.children[1])) : p.children.reduce(function (acc, child) { | ||
return p === undefined ? [] : p.children === undefined || p.children.length === 0 ? [] : p.children.length === 2 ? [].concat(edges(p.children[0]), p.kind !== undefined && p.kind !== 'pair' ? [edge([tail(p.children[0]), head(p.children[1])], p.kind, p.id, p.labels, p.record)] : [], edges(p.children[1])) : p.children.reduce(function (acc, child) { | ||
return [].concat(acc, edges(child)); | ||
@@ -23,0 +24,0 @@ }, []); |
@@ -7,2 +7,31 @@ (function (global, factory) { | ||
/** | ||
* Type guard for a Path. | ||
* | ||
* @param o any object | ||
*/ | ||
var isGramPath = function isGramPath(o) { | ||
return !!o.type && o.type === 'path'; | ||
}; | ||
var EMPTY_PATH_ID = 'ø'; | ||
/** | ||
* Type guard for GramNode. | ||
* | ||
* In practice both nodes and empty paths both have no children. | ||
* The distinguishing feature is that all empty paths have | ||
* no labels, no record, and always has the identity `ø`, while | ||
* a node may have labels, a record and always has an identity | ||
* other than `ø`. | ||
* | ||
* @param o any object | ||
*/ | ||
var isGramNode = function isGramNode(o) { | ||
return isGramPath(o) && o.children && o.children.length === 0 && o.id !== EMPTY_PATH_ID; | ||
}; | ||
function _extends() { | ||
@@ -29,3 +58,3 @@ _extends = Object.assign || function (target) { | ||
* @param children | ||
* @param relation | ||
* @param kind | ||
* @param id | ||
@@ -37,3 +66,3 @@ * @param labels | ||
var edge = function edge(children, relation, id, labels, record) { | ||
var edge = function edge(children, kind, id, labels, record) { | ||
return _extends({ | ||
@@ -47,3 +76,3 @@ type: 'path', | ||
}, { | ||
relation: relation, | ||
kind: kind, | ||
children: children | ||
@@ -54,3 +83,3 @@ }); | ||
var count = function count(p) { | ||
return p === undefined ? 0 : p.children === undefined ? 1 : p.children.reduce(function (acc, child) { | ||
return p.children.reduce(function (acc, child) { | ||
return acc + count(child); | ||
@@ -66,8 +95,8 @@ }, 1); | ||
var nodes = function nodes(p) { | ||
return p === undefined ? [] : p.children === undefined || p.children.length === 0 ? [p] : p.children.reduce(function (acc, child) { | ||
return p.children.reduce(function (acc, child) { | ||
return [].concat(acc, nodes(child)); | ||
}, []); | ||
}, isGramNode(p) ? [p] : []); | ||
}; | ||
var edges = function edges(p) { | ||
return p === undefined ? [] : p.children === undefined || p.children.length === 0 ? [] : p.children.length === 2 ? [].concat(edges(p.children[0]), p.relation !== undefined && p.relation !== 'pair' ? [edge([tail(p.children[0]), head(p.children[1])], p.relation, p.id, p.labels, p.record)] : [], edges(p.children[1])) : p.children.reduce(function (acc, child) { | ||
return p === undefined ? [] : p.children === undefined || p.children.length === 0 ? [] : p.children.length === 2 ? [].concat(edges(p.children[0]), p.kind !== undefined && p.kind !== 'pair' ? [edge([tail(p.children[0]), head(p.children[1])], p.kind, p.id, p.labels, p.record)] : [], edges(p.children[1])) : p.children.reduce(function (acc, child) { | ||
return [].concat(acc, edges(child)); | ||
@@ -74,0 +103,0 @@ }, []); |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(((e=e||self).gram=e.gram||{},e.gram.ops={}))}(this,(function(e){"use strict";function n(){return(n=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var i in r)Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i])}return e}).apply(this,arguments)}var r=function e(n){return void 0===n.children||0===n.children.length?n:e(n.children[0])},i=function e(n){return void 0===n.children||0===n.children.length?n:e(n.children[n.children.length-1])};e.count=function e(n){return void 0===n?0:void 0===n.children?1:n.children.reduce((function(n,r){return n+e(r)}),1)},e.edges=function e(t){return void 0===t||void 0===t.children||0===t.children.length?[]:2===t.children.length?[].concat(e(t.children[0]),void 0!==t.relation&&"pair"!==t.relation?[(c=[i(t.children[0]),r(t.children[1])],d=t.relation,o=t.id,l=t.labels,h=t.record,n({type:"path",id:o},l&&{labels:l},h&&{record:h},{relation:d,children:c}))]:[],e(t.children[1])):t.children.reduce((function(n,r){return[].concat(n,e(r))}),[]);var c,d,o,l,h},e.head=r,e.nodes=function e(n){return void 0===n?[]:void 0===n.children||0===n.children.length?[n]:n.children.reduce((function(n,r){return[].concat(n,e(r))}),[])},e.tail=i,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(((e=e||self).gram=e.gram||{},e.gram.ops={}))}(this,(function(e){"use strict";function n(){return(n=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t])}return e}).apply(this,arguments)}var r=function e(n){return void 0===n.children||0===n.children.length?n:e(n.children[0])},t=function e(n){return void 0===n.children||0===n.children.length?n:e(n.children[n.children.length-1])};e.count=function e(n){return n.children.reduce((function(n,r){return n+e(r)}),1)},e.edges=function e(i){return void 0===i||void 0===i.children||0===i.children.length?[]:2===i.children.length?[].concat(e(i.children[0]),void 0!==i.kind&&"pair"!==i.kind?[(c=[t(i.children[0]),r(i.children[1])],d=i.kind,o=i.id,l=i.labels,u=i.record,n({type:"path",id:o},l&&{labels:l},u&&{record:u},{kind:d,children:c}))]:[],e(i.children[1])):i.children.reduce((function(n,r){return[].concat(n,e(r))}),[]);var c,d,o,l,u},e.head=r,e.nodes=function e(n){return n.children.reduce((function(n,r){return[].concat(n,e(r))}),function(e){return!!e.type&&"path"===e.type}(r=n)&&r.children&&0===r.children.length&&"ø"!==r.id?[n]:[]);var r},e.tail=t,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=gram-ops.umd.production.min.js.map |
@@ -8,3 +8,3 @@ { | ||
], | ||
"version": "0.2.9", | ||
"version": "0.2.10", | ||
"license": "MIT", | ||
@@ -67,4 +67,4 @@ "repository": { | ||
"dependencies": { | ||
"@gram-data/gram-ast": "^0.2.9", | ||
"@gram-data/gram-builder": "^0.2.9", | ||
"@gram-data/gram-ast": "^0.2.10", | ||
"@gram-data/gram-builder": "^0.2.10", | ||
"fp-ts": "^2.8.6" | ||
@@ -75,3 +75,3 @@ }, | ||
}, | ||
"gitHead": "1ff6bdb1c799a29a95540314bdb5b2a881971774" | ||
"gitHead": "5ab0f6f900db922abaf69906f62d6cf23e12215e" | ||
} |
@@ -1,3 +0,46 @@ | ||
# Gram Operators | ||
Utility operations for extracting path features. | ||
Do stuff with gram. | ||
## How to gram-ops | ||
### Install: | ||
``` bash | ||
npm install @gram-data/gram-ops | ||
``` | ||
### Build a path | ||
Use [[gram-builder]] to create a path. | ||
``` TypeScript | ||
import { node, edge } from '@gram-data/gram-builder'; | ||
const left = node('a'); | ||
const right = node('b'); | ||
const path = edge([left, right], 'right'); | ||
``` | ||
### Extract features from the path | ||
Extract a node list: | ||
``` TypeScript | ||
import { nodes } from '@gram-data/gram-ops'; | ||
const ns:GramPath[] = nodes(path); | ||
``` | ||
Extract an edge list: | ||
``` TypeScript | ||
import { nodes } from '@gram-data/gram-ops'; | ||
const es:GramPath[] = edges(path); | ||
console.log(ns.map(n => stringify(n)).join()); // (a),(b) | ||
``` | ||
## Next Steps | ||
- Transform to js objects using [[gram-value]] | ||
- Write back to a string using [[gram-stringify]] | ||
- Introspect the AST using [[gram-ast]] |
@@ -1,44 +0,55 @@ | ||
import { GramNode, GramPath, GramPathlike } from '@gram-data/gram-ast'; | ||
import { GramNode, GramPath, isGramNode } from '@gram-data/gram-ast'; | ||
import { edge } from '@gram-data/gram-builder'; | ||
export const count = (p:GramPathlike):number => { | ||
return (p === undefined) ? 0 : | ||
(p.children === undefined ) ? 1 : | ||
(p.children.reduce( | ||
(acc: number, child: GramPath) => acc + count(child), | ||
1 | ||
)) | ||
} | ||
export const count = (p: GramPath): number => { | ||
return (p.children as GramPath[]).reduce( | ||
(acc: number, child: GramPath) => acc + count(child), | ||
1 | ||
); | ||
}; | ||
export const head = (p:GramPathlike):GramNode => { | ||
return (p.children === undefined || p.children.length === 0) ? p as GramNode | ||
export const head = (p: GramPath): GramNode => { | ||
return p.children === undefined || p.children.length === 0 | ||
? (p as GramNode) | ||
: head(p.children[0]); | ||
} | ||
}; | ||
export const tail = (p:GramPathlike):GramNode => { | ||
return (p.children === undefined || p.children.length === 0) ? p as GramNode | ||
export const tail = (p: GramPath): GramNode => { | ||
return p.children === undefined || p.children.length === 0 | ||
? (p as GramNode) | ||
: tail(p.children[p.children.length - 1]); | ||
} | ||
}; | ||
export const nodes = (p:GramPathlike):GramPath[] => { | ||
return (p === undefined) ? [] : | ||
(p.children === undefined || p.children.length === 0) ? [p as GramPath] : | ||
(p.children.reduce( | ||
(acc:GramPath[], child: GramPath) => [...acc, ...nodes(child)], | ||
[] as GramPath[] | ||
)) | ||
} | ||
export const nodes = (p: GramPath): GramPath[] => { | ||
return (p.children as GramPath[]).reduce( | ||
(acc: GramPath[], child: GramPath) => [...acc, ...nodes(child)], | ||
isGramNode(p) ? [p] : ([] as GramPath[]) | ||
); | ||
}; | ||
export const edges = (p:GramPathlike):GramPath[] => { | ||
return (p === undefined) ? [] | ||
: (p.children === undefined || p.children.length === 0) ? [] | ||
: (p.children.length === 2) ? [ | ||
...edges(p.children[0]), | ||
...((p.relation !== undefined && p.relation !== 'pair') ? [edge([tail(p.children[0]), head(p.children[1])], p.relation, p.id, p.labels, p.record)] : []), | ||
...edges(p.children[1]) | ||
] | ||
: (p.children.reduce( | ||
(acc:GramPath[], child: GramPath) => [...acc, ...edges(child)], | ||
[] as GramPath[] | ||
)) | ||
} | ||
export const edges = (p: GramPath): GramPath[] => { | ||
return p === undefined | ||
? [] | ||
: p.children === undefined || p.children.length === 0 | ||
? [] | ||
: p.children.length === 2 | ||
? [ | ||
...edges(p.children[0]), | ||
...(p.kind !== undefined && p.kind !== 'pair' | ||
? [ | ||
edge( | ||
[tail(p.children[0]), head(p.children[1])], | ||
p.kind, | ||
p.id, | ||
p.labels, | ||
p.record | ||
), | ||
] | ||
: []), | ||
...edges(p.children[1]), | ||
] | ||
: p.children.reduce( | ||
(acc: GramPath[], child: GramPath) => [...acc, ...edges(child)], | ||
[] as GramPath[] | ||
); | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39475
228
46