Skip to content
Browse files

Adding keyword extraction plugin

  • Loading branch information...
1 parent 10bc241 commit b59637864b23e18942a4694a22bb4a781e84fd12 @sorensen sorensen committed
Showing with 37 additions and 0 deletions.
  1. +37 −0 lib/extractKeywords.js
View
37 lib/extractKeywords.js
@@ -0,0 +1,37 @@
+
+/*!
+ * Copyright(c) Beau Sorensen
+ * MIT Licensed
+ */
+
+module.exports = function(schema, options) {
+ options || (options = {})
+
+ var target = options.target
+ , source = options.source
+
+ schema
+ .pre('save', function(next) {
+ var words = []
+
+ if (!(source instanceof Array)) source = [source]
+
+ for (var i = 0; i < source.length; i++) {
+ var add = this.extractKeywords(this[source[i]])
+ for (var s = 0; s < add.length; s++) {
+ if (!~words.indexOf(ext[s])) words.push(add[s])
+ }
+ }
+ this[target] = words
+ next()
+ })
+
+ schema.method('extractKeywords', function (str) {
+ if (!str) return []
+ return str
+ .toLowerCase()
+ .split(/\s+/)
+ .filter(function(v) { return v.length > 2 })
+ .filter(function(v, i, a) { return a.lastIndexOf(v) === i })
+ })
+}

0 comments on commit b596378

Please sign in to comment.
Something went wrong with that request. Please try again.