feat: added linkDensity function
parent
c470261d41
commit
e2600231ac
@ -0,0 +1,23 @@
|
||||
// Determines what percentage of the text
|
||||
// in a node is link text
|
||||
// Takes a node, returns a float
|
||||
export default function linkDensity(node) {
|
||||
const totalTextLength = textLength(node.text())
|
||||
|
||||
const linkText = node.find('a').text()
|
||||
const linkLength = textLength(linkText)
|
||||
|
||||
if (totalTextLength > 0) {
|
||||
return linkLength / totalTextLength
|
||||
} else if (totalTextLength === 0 && linkLength > 0) {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
function textLength(text) {
|
||||
return text.trim()
|
||||
.replace(/\s+/g, ' ')
|
||||
.length
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
import assert from 'assert'
|
||||
import cheerio from 'cheerio'
|
||||
|
||||
import HTML from '../fixtures/html'
|
||||
|
||||
import { linkDensity } from './index'
|
||||
|
||||
describe('linkDensity($)', () => {
|
||||
it("returns 0.5 if half of the text is a link", () => {
|
||||
const $ = cheerio.load(HTML.linkDensity5)
|
||||
|
||||
const density = linkDensity($('div').first(), $)
|
||||
|
||||
assert.equal(density, 0.5)
|
||||
})
|
||||
|
||||
it("returns 1 if all of the text is a link", () => {
|
||||
const $ = cheerio.load(HTML.linkDensity1)
|
||||
|
||||
const density = linkDensity($('div').first(), $)
|
||||
|
||||
assert.equal(density, 1)
|
||||
})
|
||||
|
||||
it("returns 0 if there's no text", () => {
|
||||
const $ = cheerio.load(HTML.linkDensity0)
|
||||
|
||||
const density = linkDensity($('div').first())
|
||||
|
||||
assert.equal(density, 0)
|
||||
})
|
||||
|
||||
})
|
||||
|
Loading…
Reference in New Issue