| const onecolor = one.color |
| const styleCheckList = Vue.ref({ |
| isPosition: false, |
| isColor: true, |
| isFontSize: true, |
| isFontStyle: false, |
| isFontWeight: false, |
| isBackgroundColor: true, |
| isBorderRadius: true, |
| isBorderWidth: true, |
| isOpacity: false, |
| ignoreUnvisible: true |
| }) |
| const maxElsLimit = Vue.ref(1500) |
|
|
| const sourceEls = Vue.ref([]) |
| const targetEls = Vue.ref([]) |
| const targetHaveAssEls = Vue.ref([]) |
| const targetNoAssEls = Vue.ref([]) |
|
|
| const getElements = (toPlaywright = true) => { |
| let toList = []; |
| const ref = document.body; |
| const parentWidth = ref.offsetWidth |
| const parentHeight = document.documentElement.clientHeight |
| const parentScrollHeight = ref.scrollHeight |
| let elements = []; |
| elements = document.querySelectorAll('body *') |
| toList.splice(0, toList.length); |
| for (let el of elements) { |
| if (['script'].includes(el.tagName.toLowerCase())) continue |
| if (isUnvisible(el)) continue |
| toList.push(el) |
| } |
| if (toList.length > maxElsLimit.value) { |
| toList = toList.slice(0, maxElsLimit.value) |
| } |
| toList.forEach((el) => { |
| let clientRect = el.getBoundingClientRect() |
| let pos = { |
| left: clientRect.left + scrollX, |
| right: clientRect.right + scrollX, |
| top: clientRect.top + scrollY, |
| bottom: clientRect.bottom + scrollY, |
| width: clientRect.width, |
| height: clientRect.height |
| } |
| const isFixedOrSticky = ['fixed', 'sticky'].includes( |
| getComputedStyle(el).position |
| ) |
| if (isFixedOrSticky) { |
| pos.left = clientRect.left |
| pos.right = clientRect.right |
| pos.top = clientRect.top |
| pos.bottom = clientRect.bottom |
| } |
| let biasX = [] |
| let biasY = [] |
| if (pos.left < parentWidth / 2) biasX = ['left'] |
| else biasX = ['right'] |
| if (pos.left < parentWidth / 2 && pos.right > parentWidth / 2) biasX.push('mid') |
|
|
| if (pos.top < parentHeight / 2) biasY = ['top'] |
| else if (pos.top > parentScrollHeight - parentHeight / 2) biasY = ['bottom'] |
| else biasY = ['scroll'] |
| if (pos.top < parentHeight / 2 && pos.bottom > parentHeight / 2 && isFixedOrSticky) |
| biasY.push('mid') |
|
|
| el.alisaInfo = { |
| elInfo: { |
| pos: pos, |
| biasX, |
| biasY |
| }, |
| parentInfo: { |
| width: parentWidth, |
| height: parentHeight, |
| scrollHeight: parentScrollHeight |
| }, |
| styles: getComputedStyle(el), |
| groupLeft: [], |
| groupRight: [], |
| groupTop: [], |
| groupBottom: [], |
| groupAxisWidth: [], |
| groupAxisHeight: [], |
| groupAll: [], |
| raceList: [], |
| assElement: null, |
| assLayoutSimScore: 0, |
| assStyleSimScore: 0 |
| } |
| }) |
|
|
| toList.forEach((el) => { |
| getSameGroup(el, toList, 'left') |
| getSameGroup(el, toList, 'right') |
| getSameGroup(el, toList, 'top') |
| getSameGroup(el, toList, 'bottom') |
| getSameGroup(el, toList, 'axisWidth') |
| getSameGroup(el, toList, 'axisHeight') |
| }) |
| toList.forEach((el) => { |
| getSameRace(el, toList) |
| }) |
|
|
| if (!toPlaywright) return toList; |
|
|
| let formatList = []; |
| toList.forEach((el, idx) => { |
| el._idx = idx; |
| let formatItem = { |
| idx: idx, |
| isAlisaObj: true, |
| tagName: el.tagName, |
| innerText: el.innerText ? el.innerText : false, |
| alisaInfo: { |
| ...el.alisaInfo |
| } |
| } |
| formatList.push(formatItem) |
| }) |
|
|
| formatList.forEach((el) => { |
| let decodeKeys = [ |
| 'groupLeft', |
| 'groupRight', |
| 'groupTop', |
| 'groupBottom', |
| 'groupAxisWidth', |
| 'groupAxisHeight', |
| 'groupAll', |
| 'raceList' |
| ] |
| for (let key of decodeKeys) { |
| let dl = [] |
| for (let keyItem of el.alisaInfo[key]) { |
| dl.push(keyItem._idx) |
| } |
| el.alisaInfo[key] = dl; |
| } |
| }) |
|
|
| return formatList; |
| } |
|
|
| const isUnvisible = (el) => { |
| const style = getComputedStyle(el); |
| const rect = el.getBoundingClientRect(); |
| const hidden = |
| style.display === 'none' || |
| style.visibility === 'hidden' || |
| style.opacity === '0' || rect.width === 0 || |
| rect.height === 0; |
| return hidden; |
| } |
|
|
| const getSameGroup = (el, elList, direction, offset = 5) => { |
| let idx = elList.indexOf(el) |
| if (idx < 0) return |
| const Direction = direction.slice(0, 1).toUpperCase() + direction.slice(1) |
| const isSamePos = (direction, pos1, pos2, offset) => { |
| if (['left', 'top', 'right', 'bottom'].includes(direction)) |
| if (Math.abs(pos1[direction] - pos2[direction]) <= offset) return true |
| if (direction == 'axisWidth') |
| if (Math.abs(pos1.width / 2 + pos1.left - pos2.width / 2 - pos2.left) <= offset) |
| return true |
| if (direction == 'axisHeight') |
| if (Math.abs(pos1.height / 2 + pos1.top - pos2.height / 2 - pos2.top) <= offset) |
| return true |
| return false |
| } |
| elList.forEach((element, i) => { |
| if (idx == i) return |
| if ( |
| isSamePos( |
| direction, |
| el.alisaInfo.elInfo.pos, |
| element.alisaInfo.elInfo.pos, |
| offset |
| ) |
| ) { |
| el.alisaInfo[`group${Direction}`].push(element) |
| let itemIndex = el.alisaInfo.groupAll.indexOf(element) |
| if (itemIndex < 0) el.alisaInfo.groupAll.push(element) |
| } |
| }) |
| } |
|
|
| const getSameRace = (el, elList) => { |
| let idx = elList.indexOf(el) |
| if (idx < 0) return |
| const isSameRace = (el1, el2) => { |
| const groupAll = el1.alisaInfo.groupAll |
| if (groupAll.indexOf(el2) < 0) return false |
| if (el1.tagName != el2.tagName) return false |
| return el1.className == el2.className |
| } |
| elList.forEach((element, i) => { |
| if (idx == i) return |
| if (isSameRace(el, element)) { |
| el.alisaInfo.raceList.push(element) |
| } |
| }) |
| } |
|
|
| const getAllAssociated = (sources, offset = 5, callback = null) => { |
| let encodeKeys = [ |
| 'groupLeft', |
| 'groupRight', |
| 'groupTop', |
| 'groupBottom', |
| 'groupAxisWidth', |
| 'groupAxisHeight', |
| 'groupAll', |
| 'raceList' |
| ] |
| sources.forEach(el => { |
| for (let key of encodeKeys) { |
| let dl = [] |
| for (let idx of el.alisaInfo[key]) { |
| dl.push(sources[idx]) |
| } |
| el.alisaInfo[key] = dl; |
| } |
| }); |
| sourceEls.value = sources; |
| targetHaveAssEls.value = []; |
| targetNoAssEls.value = []; |
| targetEls.value.forEach((el) => { |
| getElAssociated(el, sourceEls.value, offset, callback) |
| }) |
| } |
|
|
| const getLayoutSim = (sources, offset = 5, callback = null) => { |
| getAllAssociated(sources, offset, callback); |
| let allTargetEls = targetEls.value |
| let viewedEls = [] |
| let count = 0 |
| allTargetEls.forEach((ei) => { |
| if (viewedEls.indexOf(ei) < 0) { |
| count += 1 |
| viewedEls.push(ei) |
| ei.alisaInfo.raceList.forEach((ri) => { |
| viewedEls.push(ri) |
| }) |
| } |
| }) |
| let arraysEqual = (a, b) => { |
| if (!(Array.isArray(a) && Array.isArray(b))) return 'not equal' |
| let status = 'equal' |
| a.forEach((ai) => { |
| if (ai !== 'mid' && b.indexOf(ai) < 0) { |
| status = 'not equal' |
| return |
| } |
| if (ai === 'mid' && b.indexOf(ai) < 0) { |
| status = 'partial equal' |
| return |
| } |
| }) |
| return status |
| } |
| let posDiff = (val1, val2, ref) => { |
| if (Math.abs(val1 - val2) / ref > 1) return 0 |
| return 1 - Math.abs(val1 - val2) / ref |
| } |
| targetHaveAssEls.value.forEach((el) => { |
| let layoutWeight = 1 / (el.alisaInfo.raceList.length + 1) / count |
| let score = layoutWeight * 100 |
| let assEl = el.alisaInfo.assElement |
| let halfWidth = el.alisaInfo.parentInfo.width / 2 |
| let halfHeight = el.alisaInfo.parentInfo.height / 2 |
| let biasXEqual = arraysEqual( |
| el.alisaInfo.elInfo.biasX, |
| assEl.alisaInfo.elInfo.biasX |
| ) |
| let biasYEqual = arraysEqual( |
| el.alisaInfo.elInfo.biasY, |
| assEl.alisaInfo.elInfo.biasY |
| ) |
| if (biasXEqual == 'not equal' || biasYEqual == 'not equal') score *= 0 |
| if (biasXEqual == 'partial equal' || biasYEqual == 'partial equal') score *= 0.5 |
| score = |
| score * |
| posDiff( |
| el.alisaInfo.elInfo.pos.left, |
| assEl.alisaInfo.elInfo.pos.left, |
| halfWidth |
| ) * |
| posDiff(el.alisaInfo.elInfo.pos.top, assEl.alisaInfo.elInfo.pos.top, halfHeight) |
| el.alisaInfo.assLayoutSimScore = score |
| }) |
| let relativeLayoutScore = 0 |
| targetHaveAssEls.value.forEach((el) => { |
| relativeLayoutScore += el.alisaInfo.assLayoutSimScore |
| }) |
|
|
| let misMatchedLength = 0 |
| let matchedLength = 0 |
| targetEls.value.forEach((targetEl) => { |
| let assEl = targetEl.alisaInfo.assElement |
| if (!assEl) return |
| let targetGroupAll = targetEl.alisaInfo.groupAll |
| let sourceGroupAll = assEl.alisaInfo.groupAll |
| targetGroupAll.forEach((gi) => { |
| if (!gi.alisaInfo.assElement) misMatchedLength += 1 |
| else if (sourceGroupAll.indexOf(gi.alisaInfo.assElement) < 0) |
| misMatchedLength += 1 |
| else matchedLength += 1 |
| }) |
| }) |
| let groupLayoutScore = (matchedLength / (matchedLength + misMatchedLength)) * 100 |
|
|
| if (misMatchedLength === 0 && matchedLength === 0) { |
| groupLayoutScore = 0 |
| } |
|
|
| console.log( |
| relativeLayoutScore, |
| groupLayoutScore, |
| (relativeLayoutScore + groupLayoutScore) / 2 |
| ) |
| console.log(targetHaveAssEls.value) |
| console.log(targetNoAssEls.value) |
|
|
| return { |
| relativeLayoutScore, |
| groupLayoutScore, |
| targetHaveAssEls: targetHaveAssEls.value.length, |
| targetNoAssEls: targetNoAssEls.value.length, |
| overallScore: (relativeLayoutScore + groupLayoutScore) / 2 |
| } |
| } |
|
|
| const getStyleSim = (sources, offset = 5, callback = null) => { |
| getAllAssociated(sources, offset, callback); |
| let allTargetEls = targetEls.value |
| let viewedEls = [] |
| let count = 0 |
| allTargetEls.forEach((ei) => { |
| if (viewedEls.indexOf(ei) < 0) { |
| count += 1 |
| viewedEls.push(ei) |
| ei.alisaInfo.raceList.forEach((ri) => { |
| viewedEls.push(ri) |
| }) |
| } |
| }) |
|
|
| const DEFAULT_COLOR = 'rgba(0, 0, 0, 0)' |
|
|
| const getGradientColors = (gradientCSS) => { |
| const colorRegex = /rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(,\s*\d?\.?\d+)?\s*\)/gi; |
| let colors = gradientCSS.match(colorRegex) || []; |
| if (colors.length === 0) return null; |
| |
| const colorObjs = colors.map(color => onecolor(color)); |
| let avgColor = { _red: 0, _green: 0, _blue: 0 }; |
| for (let color of colorObjs) { |
| avgColor._red += color._red; |
| avgColor._green += color._green; |
| avgColor._blue += color._blue; |
| } |
| avgColor._red = Math.round(avgColor._red / colorObjs.length); |
| avgColor._green = Math.round(avgColor._green / colorObjs.length); |
| avgColor._blue = Math.round(avgColor._blue / colorObjs.length); |
| return avgColor; |
| } |
|
|
| const perceptualColorDistance = (c1, c2) => { |
| const rMean = (c1._red + c2._red) / 2; |
| const rDiff = c1._red - c2._red; |
| const gDiff = c1._green - c2._green; |
| const bDiff = c1._blue - c2._blue; |
| return Math.sqrt( |
| (2 + rMean / 256) * rDiff * rDiff + |
| 4 * gDiff * gDiff + |
| (2 + (255 - rMean) / 256) * bDiff * bDiff |
| ); |
| } |
|
|
| let computedStyles = (el) => { |
| return el.alisaInfo.styles; |
| } |
|
|
| let colorDiff = (tgt, src) => { |
| const color1 = onecolor(computedStyles(src).color || DEFAULT_COLOR) |
| const color2 = onecolor(computedStyles(tgt).color || DEFAULT_COLOR) |
| const colorDiff = perceptualColorDistance(color1, color2); |
| return 1 - colorDiff < 0 ? 0 : 1 - colorDiff; |
| } |
|
|
| let backgroundColorDiff = (tgt, src) => { |
| let imageColor1 = getGradientColors(computedStyles(src).backgroundImage) |
| let imageColor2 = getGradientColors(computedStyles(tgt).backgroundImage) |
| let colorDiff = 0; |
| if (imageColor1 || imageColor2) { |
| if (!imageColor1) imageColor1 = onecolor(DEFAULT_COLOR); |
| if (!imageColor2) imageColor2 = onecolor(DEFAULT_COLOR); |
| colorDiff = perceptualColorDistance(imageColor1, imageColor2); |
| } |
| else { |
| const color1 = onecolor(computedStyles(src).backgroundColor || DEFAULT_COLOR) |
| const color2 = onecolor(computedStyles(tgt).backgroundColor || DEFAULT_COLOR) |
| colorDiff = perceptualColorDistance(color1, color2); |
| } |
| return 1 - colorDiff < 0 ? 0 : 1 - colorDiff; |
| } |
|
|
| let fontSizeDiff = (tgt, src) => { |
| let size1 = computedStyles(src).fontSize; |
| let size2 = computedStyles(tgt).fontSize; |
| size1 = parseFloat(size1); |
| size2 = parseFloat(size2); |
| let sizeDiff = Math.abs(size1 - size2); |
| return 1 - sizeDiff / size2; |
| } |
|
|
| let fontWeightDiff = (tgt, src) => { |
| let fontWeight1 = computedStyles(src).fontWeight; |
| let fontWeight2 = computedStyles(tgt).fontWeight; |
| fontWeight1 = parseFloat(fontWeight1); |
| fontWeight2 = parseFloat(fontWeight2); |
| if (fontWeight1.toString() === 'NaN') fontWeight1 = 400; |
| if (fontWeight2.toString() === 'NaN') fontWeight2 = 400; |
| let sizeDiff = Math.abs(fontWeight1 - fontWeight2); |
| return 1 - sizeDiff / fontWeight2; |
| } |
|
|
| let positionDiff = (tgt, src) => { |
| let position1 = computedStyles(src).position; |
| let position2 = computedStyles(tgt).position; |
| return position1 == position2 ? 1 : 0; |
| } |
|
|
| let fontStyleDiff = (tgt, src) => { |
| let fontStyle1 = computedStyles(src).fontStyle; |
| let fontStyle2 = computedStyles(tgt).fontStyle; |
| return fontStyle1 == fontStyle2 ? 1 : 0; |
| } |
|
|
| let borderRadiusDiff = (tgt, src) => { |
| let topLeft1 = computedStyles(src).borderTopLeftRadius; |
| let topLeft2 = computedStyles(tgt).borderTopLeftRadius; |
| let topRight1 = computedStyles(src).borderTopRightRadius; |
| let topRight2 = computedStyles(tgt).borderTopRightRadius; |
| let bottomLeft1 = computedStyles(src).borderBottomLeftRadius; |
| let bottomLeft2 = computedStyles(tgt).borderBottomLeftRadius; |
| let bottomRight1 = computedStyles(src).borderBottomRightRadius; |
| let bottomRight2 = computedStyles(tgt).borderBottomRightRadius; |
| topLeft1 = parseFloat(topLeft1) |
| topLeft2 = parseFloat(topLeft2) |
| topRight1 = parseFloat(topRight1) |
| topRight2 = parseFloat(topRight2) |
| bottomLeft1 = parseFloat(bottomLeft1) |
| bottomLeft2 = parseFloat(bottomLeft2) |
| bottomRight1 = parseFloat(bottomRight1) |
| bottomRight2 = parseFloat(bottomRight2) |
| let topLeftDiff = Math.abs(topLeft1 - topLeft2); |
| let topRightDiff = Math.abs(topRight1 - topRight2); |
| let bottomLeftDiff = Math.abs(bottomLeft1 - bottomLeft2); |
| let bottomRightDiff = Math.abs(bottomRight1 - bottomRight2); |
| let targetRelative = tgt.alisaInfo.elInfo.pos.height; |
| targetRelative = targetRelative > tgt.alisaInfo.elInfo.pos.width ? tgt.alisaInfo.elInfo.pos.width / 2 : targetRelative / 2; |
| let avg = (topLeftDiff + topRightDiff + bottomLeftDiff + bottomRightDiff) / targetRelative; |
| avg = avg > 1 ? 1 : avg; |
| return 1 - avg; |
| } |
|
|
| let borderWidthDiff = (tgt, src) => { |
| let top1 = computedStyles(src).borderTopWidth; |
| let top2 = computedStyles(tgt).borderTopWidth; |
| let right1 = computedStyles(src).borderRightWidth; |
| let right2 = computedStyles(tgt).borderRightWidth; |
| let bottom1 = computedStyles(src).borderBottomWidth; |
| let bottom2 = computedStyles(tgt).borderBottomWidth; |
| let left1 = computedStyles(src).borderLeftWidth; |
| let left2 = computedStyles(tgt).borderLeftWidth; |
| top1 = parseFloat(top1) |
| top2 = parseFloat(top2) |
| right1 = parseFloat(right1) |
| right2 = parseFloat(right2) |
| bottom1 = parseFloat(bottom1) |
| bottom2 = parseFloat(bottom2) |
| left1 = parseFloat(left1) |
| left2 = parseFloat(left2) |
| let topDiff = Math.abs(top1 - top2); |
| let rightDiff = Math.abs(right1 - right2); |
| let bottomDiff = Math.abs(bottom1 - bottom2); |
| let leftDiff = Math.abs(left1 - left2); |
| let targetRelative = tgt.alisaInfo.elInfo.pos.height; |
| targetRelative = targetRelative > tgt.alisaInfo.elInfo.pos.width ? tgt.alisaInfo.elInfo.pos.width / 2 : targetRelative / 2; |
| let avg = (topDiff + rightDiff + bottomDiff + leftDiff) / targetRelative; |
| avg = avg > 1 ? 1 : avg; |
| return 1 - avg; |
| } |
|
|
| let opacityDiff = (tgt, src) => { |
| let opacity1 = computedStyles(src).opacity; |
| let opacity2 = computedStyles(tgt).opacity; |
| opacity1 = parseFloat(opacity1); |
| opacity2 = parseFloat(opacity2); |
| if (opacity1.toString() === 'NaN') opacity1 = 1; |
| if (opacity2.toString() === 'NaN') opacity2 = 1; |
| let sizeDiff = Math.abs(opacity1 - opacity2); |
| return 1 - sizeDiff / opacity2; |
| } |
|
|
| targetHaveAssEls.value.forEach((el) => { |
| let layoutWeight = 1 / (el.alisaInfo.raceList.length + 1) / count |
| let score = layoutWeight * 100 |
| let unitScoreList = []; |
| let assEl = el.alisaInfo.assElement |
| if (styleCheckList.value.isPosition) |
| unitScoreList.push(positionDiff(el, assEl)); |
| if (styleCheckList.value.isColor) |
| unitScoreList.push(colorDiff(el, assEl)); |
| if (styleCheckList.value.isBackgroundColor) |
| unitScoreList.push(backgroundColorDiff(el, assEl)); |
| if (styleCheckList.value.isFontSize) |
| unitScoreList.push(fontSizeDiff(el, assEl)); |
| if (styleCheckList.value.isFontStyle) |
| unitScoreList.push(fontStyleDiff(el, assEl)); |
| if (styleCheckList.value.isFontWeight) |
| unitScoreList.push(fontWeightDiff(el, assEl)); |
| if (styleCheckList.value.isBorderRadius) |
| unitScoreList.push(borderRadiusDiff(el, assEl)) |
| if (styleCheckList.value.isBorderWidth) |
| unitScoreList.push(borderWidthDiff(el, assEl)) |
| if (styleCheckList.value.isOpacity) |
| unitScoreList.push(opacityDiff(el, assEl)) |
| const sum = unitScoreList.reduce((acc, val) => acc + val, 0); |
| const avg = sum / unitScoreList.length; |
| el.alisaInfo.assStyleSimScore = score * avg; |
| }) |
|
|
| let relativeStyleScore = 0 |
| targetHaveAssEls.value.forEach((el) => { |
| relativeStyleScore += el.alisaInfo.assStyleSimScore |
| }) |
| console.log(relativeStyleScore) |
|
|
| return { |
| relativeStyleScore |
| } |
| } |
|
|
| const getElAssociated = (targetEl, sourceElList, offset = 5, callBack = null) => { |
| let candidateList = [...sourceElList] |
| let sizeDiff = (source, target) => { |
| return ( |
| Math.abs( |
| source.alisaInfo.elInfo.pos.width - target.alisaInfo.elInfo.pos.width |
| ) + |
| Math.abs( |
| source.alisaInfo.elInfo.pos.height - target.alisaInfo.elInfo.pos.height |
| ) |
| ) |
| } |
| let posDiff = (source, target) => { |
| return Math.sqrt( |
| Math.pow( |
| target.alisaInfo.elInfo.pos.left - source.alisaInfo.elInfo.pos.left, |
| 2 |
| ) + |
| Math.pow( |
| target.alisaInfo.elInfo.pos.top - source.alisaInfo.elInfo.pos.top, |
| 2 |
| ) |
| ) |
| } |
| candidateList.sort((a, b) => { |
| const aSizeDiff = sizeDiff(a, targetEl) |
| const bSizeDiff = sizeDiff(b, targetEl) |
| if (aSizeDiff == bSizeDiff) { |
| const aPosDiff = posDiff(a, targetEl) |
| const bPosDiff = posDiff(b, targetEl) |
| return aPosDiff - bPosDiff |
| } |
| return aSizeDiff - bSizeDiff |
| }) |
| if (targetEl.innerText == '' || !targetEl.innerText) { |
| for (let item of candidateList) { |
| if (sizeDiff(item, targetEl) <= offset * 2) { |
| targetEl.alisaInfo.assElement = item |
| item.alisaInfo.assElement = targetEl |
| targetHaveAssEls.value.push(targetEl) |
| if (callBack) |
| callBack(targetEl, item) |
| return |
| } |
| } |
| } else { |
| for (let item of candidateList) { |
| if (LCSSim(item.innerText, targetEl.innerText) >= 0.8) { |
| targetEl.alisaInfo.assElement = item |
| item.alisaInfo.assElement = targetEl |
| targetHaveAssEls.value.push(targetEl) |
| if (callBack) |
| callBack(targetEl, item) |
| return |
| } |
| } |
| } |
| targetNoAssEls.value.push(targetEl) |
| } |
|
|
| const LCS = (a, b) => { |
| const dp = Array(a.length + 1).fill() |
| .map(() => Array(b.length + 1).fill(0)) |
| for (let i = 1; i <= a.length; i++) { |
| for (let j = 1; j <= b.length; j++) { |
| dp[i][j] = |
| a[i - 1] === b[j - 1] |
| ? dp[i - 1][j - 1] + 1 |
| : Math.max(dp[i - 1][j], dp[i][j - 1]) |
| } |
| } |
| return dp[a.length][b.length] |
| } |
|
|
| const LCSSim = (a, b) => { |
| if (!a || !b) return 0; |
| const lcsLength = LCS(a, b) |
| return lcsLength / Math.max(a.length, b.length) |
| } |