Skip to content

Commit

Permalink
Add a flatten option
Browse files Browse the repository at this point in the history
  • Loading branch information
bxjx committed Oct 16, 2013
1 parent ab63f62 commit 7ee1527
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ keyword.extract('beep beep and foo bar and beep beep and beep beep and foo bar',

Returns `['beep beep']`.

#### Option: flatten

Returns all occurrences of the ngram. Useful for passing data to Natural's
TF-IDF function. Note: the original order is not maintained. Off by default.

```js
keyword.extract('beep beep and foo bar and beep beep and beep beep and foo bar', {flaten: true})
```

Returns `['beep beep', 'beep beep', 'beep beep', 'foo bar', 'foo bar']`.

#### Option: html

Extracts the keywords from html text elements. The default is false.
Expand Down
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,23 @@ exports.extract = function(text, options){
});
}

// Return results with scores or without depending on options
combined = options.score ? combined : _.pluck(combined, 'term');
if (options.flatten){
// Flatten the results so that there is a list item for every occurence of
// the term
combined = _.flatten(
_.map(combined, function(result){
var flattened = [];
for (var i=0; i < result.tf; i++){
flattened.push(result.term);
}
return flattened;
})
);
}else{
// Return results with scores or without depending on options
combined = options.score ? combined : _.pluck(combined, 'term');
}


// Limit the results
if (options.limit){
Expand Down

0 comments on commit 7ee1527

Please sign in to comment.