Skip to content

Commit

Permalink
Fix demo + change test framework to batr
Browse files Browse the repository at this point in the history
Also fixing demo and moving stopword library from dependency to devDependency. So not bundled, but possible to test with.
  • Loading branch information
eklem committed Oct 13, 2021
1 parent 5c99650 commit 06fc008
Show file tree
Hide file tree
Showing 18 changed files with 8,914 additions and 3,815 deletions.
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

25 changes: 6 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
language: node_js
cache:
directories:
- node_modules
os: linux
dist: xenial
notifications:
email: true
node_js:
- '10'
- '12'
- '14'
- '16'
before_script:
- npm prune
branches:
except:
- /^v\d+\.\d+\.\d+$/
addons:
apt:
packages:
- xvfb
install:
- export DISPLAY=':99.0'
- Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
- npm install
notifications:
slack: norch:wLsHiXjnAH5BdtTq3qtAqGp7
email: true
- npm prune
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,37 @@ Check out the [browser demo](https://eklem.github.io/eklem-headline-parser/demo/

## Usage

### Node.js

### Common JS
```javascript
const headlineParser = require('eklem-headline-parser')
// headlineParser.findKeywords(headline, body [, cutoff]) now available
```

### Script tag method
### ES Modules
```javascript
import headlineParser from 'eklem-headline-parser'
// headlineParser.findKeywords(headline, body [, cutoff]) now available
```

### UMD - Script tag method
```javascript
<script src="headline-parser.js"></script>
<script>
// ehp.findKeywords(headline, body [, cutoff]) now available
</script>
```

### Remove noise
To skip all words with little or no meaning, use a stopword stripping library, i.e. [stopword](https://github.com/fergiemcdowall/stopword).

```javascript
<script src="headline-parser.js"></script>
<script src="stopword.js"></script>
<script>
// ehp.findKeywords(sw.removeStopwords(headline), body [, cutoff])
</script>
```

### Calculating some keywords

```javascript
Expand Down
18 changes: 9 additions & 9 deletions demo/headline-parser-app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Populating div with only meaningful words
const populateKeywords = function(result) {
const populateKeywords = function (result) {
console.log('Boom')
console.log(result)
const node = document.createElement('span')
Expand All @@ -9,28 +9,28 @@ const populateKeywords = function(result) {
}

// Listen to key up on headlinetext and initiate a headline parser
document.getElementById("headlinetext").onkeyup = function() {
document.getElementById('headlinetext').onkeyup = function () {
calculateKeywords()
}

// Listen to key up on bodytext and initiate a headline parser
document.getElementById("bodytext").onkeyup = function() {
document.getElementById('bodytext').onkeyup = function () {
calculateKeywords()
}

// calculate keywords
const calculateKeywords = function () {
var headlinetext = document.getElementById("headlinetext").value.split(' ')
var bodytext = document.getElementById("bodytext").value.split(' ')
const headlinetext = document.getElementById('headlinetext').value.split(' ')
const bodytext = document.getElementById('bodytext').value.split(' ')
console.log(headlinetext)
console.log(bodytext)
var importantKeywords = []
var importantKeywords = ehp.findKeywords(sw.removeStopwords(headlinetext), bodytext);
let importantKeywords = []
importantKeywords = window.ehp.findKeywords(window.sw.removeStopwords(headlinetext), bodytext)
console.log('Keywords: ' + importantKeywords)
populateKeywords(importantKeywords)
}

// Empty HTML element
const emptyElement = function (element) {
document.getElementById(element).innerHTML = ''
}
document.getElementById(element).innerHTML = ''
}
4 changes: 2 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./mini-default.min.css">
<link rel="stylesheet" href="./mini-additions.css">
<script src="../dist/headline-parser.latest.js"></script>
<script src="https://fergiemcdowall.github.io/stopword/dist/stopword.latest.js"></script>
<script src="../dist/eklem-headline-parser.umd.js"></script>
<script src="https://fergiemcdowall.github.io/stopword/dist/stopword.js"></script>
<title>Headline parser browser demo</title>
</head>

Expand Down
66 changes: 66 additions & 0 deletions dist/eklem-headline-parser.cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

const findKeywords = function (headline, body, cutoff = 0) {
// remove duplicates in headline
headline = [...new Set(headline)];

// keywords array of word objects with word count
const keywordsCount = [];
// returned array (cutoff if != 0)
let keywords = [];

// loop through headline and body
for (let i = 0; i < headline.length; i++) {
for (let j = 0; j < body.length; j++) {
// headline/body mathching
if (headline[i] === body[j]) {
// check if word exists in keywordsCount first and update it
const existing = findExistingWord(keywordsCount, 'word', headline[i]);
if (existing > -1) {
keywordsCount[existing].count++;
} else {
// create object and push to array
const wordObj = { word: headline[i], count: 1 };
keywordsCount.push(wordObj);
}
}
}
}

// Sort on most times used in body
keywordsCount.sort(compareObjects);
// loop through keywordsCount and push word to keywords
for (let l = 0; l < keywordsCount.length; l++) {
keywords.push(keywordsCount[l].word);
}
// slice, if cutoff set
if (cutoff > 0) {
keywords = keywords.slice(0, cutoff);
}
return keywords
};

const findExistingWord = function (array, attr, value) {
for (let k = 0; k < array.length; k += 1) {
if (array[k][attr] === value) {
return k
}
}
return -1
};

const compareObjects = function (a, b) {
if (a.count > b.count) {
return -1
}
if (a.count < b.count) {
return 1
}
return 0
};

var src = {
findKeywords: findKeywords
};

module.exports = src;
64 changes: 64 additions & 0 deletions dist/eklem-headline-parser.esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const findKeywords = function (headline, body, cutoff = 0) {
// remove duplicates in headline
headline = [...new Set(headline)];

// keywords array of word objects with word count
const keywordsCount = [];
// returned array (cutoff if != 0)
let keywords = [];

// loop through headline and body
for (let i = 0; i < headline.length; i++) {
for (let j = 0; j < body.length; j++) {
// headline/body mathching
if (headline[i] === body[j]) {
// check if word exists in keywordsCount first and update it
const existing = findExistingWord(keywordsCount, 'word', headline[i]);
if (existing > -1) {
keywordsCount[existing].count++;
} else {
// create object and push to array
const wordObj = { word: headline[i], count: 1 };
keywordsCount.push(wordObj);
}
}
}
}

// Sort on most times used in body
keywordsCount.sort(compareObjects);
// loop through keywordsCount and push word to keywords
for (let l = 0; l < keywordsCount.length; l++) {
keywords.push(keywordsCount[l].word);
}
// slice, if cutoff set
if (cutoff > 0) {
keywords = keywords.slice(0, cutoff);
}
return keywords
};

const findExistingWord = function (array, attr, value) {
for (let k = 0; k < array.length; k += 1) {
if (array[k][attr] === value) {
return k
}
}
return -1
};

const compareObjects = function (a, b) {
if (a.count > b.count) {
return -1
}
if (a.count < b.count) {
return 1
}
return 0
};

var src = {
findKeywords: findKeywords
};

export { src as default };
72 changes: 72 additions & 0 deletions dist/eklem-headline-parser.umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ehp = factory());
})(this, (function () { 'use strict';

const findKeywords = function (headline, body, cutoff = 0) {
// remove duplicates in headline
headline = [...new Set(headline)];

// keywords array of word objects with word count
const keywordsCount = [];
// returned array (cutoff if != 0)
let keywords = [];

// loop through headline and body
for (let i = 0; i < headline.length; i++) {
for (let j = 0; j < body.length; j++) {
// headline/body mathching
if (headline[i] === body[j]) {
// check if word exists in keywordsCount first and update it
const existing = findExistingWord(keywordsCount, 'word', headline[i]);
if (existing > -1) {
keywordsCount[existing].count++;
} else {
// create object and push to array
const wordObj = { word: headline[i], count: 1 };
keywordsCount.push(wordObj);
}
}
}
}

// Sort on most times used in body
keywordsCount.sort(compareObjects);
// loop through keywordsCount and push word to keywords
for (let l = 0; l < keywordsCount.length; l++) {
keywords.push(keywordsCount[l].word);
}
// slice, if cutoff set
if (cutoff > 0) {
keywords = keywords.slice(0, cutoff);
}
return keywords
};

const findExistingWord = function (array, attr, value) {
for (let k = 0; k < array.length; k += 1) {
if (array[k][attr] === value) {
return k
}
}
return -1
};

const compareObjects = function (a, b) {
if (a.count > b.count) {
return -1
}
if (a.count < b.count) {
return 1
}
return 0
};

var src = {
findKeywords: findKeywords
};

return src;

}));
1 change: 0 additions & 1 deletion dist/headline-parser.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/headline-parser.js.map

This file was deleted.

Loading

0 comments on commit 06fc008

Please sign in to comment.