Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 20x 1847x 1845x 1845x 1845x 20x 20x 20x 20x 1306x 1306x 217612x 217612x 217612x 1845x 1845x 1845x 529x 20x 20x 9x 9x 11x 11x 11x 1306x 1306x 11x 529x 529x 11x 11x 1306x 11x 1306x 1306x 1306x 100x 100x 100x 100x 100x 11x 11x 11x 44x 44x 44x 44x 22x 22x 44x 22x 22x 22x 18x 1306x 1306x 11x 11x 11x 100x | import {Box, LinearProgress, Stack, Typography} from '@mui/material'
import type {PlotOptions, ProcessOptions, TermEntry} from './analysisMenu'
import {useContext, useEffect, useState} from 'react'
import {getProcessedTerm} from './processTerms'
import type {FixedTerm, NetworkLookup} from './term'
import {ResourceContext} from './resources'
import {BuildContext, type NumberObject} from './building'
import {timers} from './addedTerms'
import type {DictEntry} from './storage'
import dynamic from 'next/dynamic'
const Graph = dynamic(() => import('./graph'))
export function getIntersects(a: string, b: NetworkLookup) {
return {
lemma: +(a in b.lemma),
lemma_related: +(a in b.lemma_related),
lemma_synset: +(a in b.lemma_synset),
related: +(a in b.related),
related_lemma: +(a in b.related_lemma),
related_related: +(a in b.related_related),
related_synset: +(a in b.related_synset),
synset: +(a in b.synset),
synset_lemma: +(a in b.synset_lemma),
synset_related: +(a in b.synset_related),
synset_synset: +(a in b.synset_synset),
}
}
function getSimilarity(a: TermEntry, b: TermEntry, dense: boolean) {
Iif (!b.processed) return 0
const sim = getIntersects(a.term, b.processed.lookup)
return dense ?
0.35 * sim.lemma +
0.1 * sim.lemma_related +
0.001 * sim.lemma_synset +
0.25 * sim.related +
0.1 * sim.related_lemma +
0.001 * sim.related_related +
0.001 * sim.related_synset +
0.001 * sim.synset +
0.001 * sim.synset_lemma +
0.001 * sim.synset_related +
0.001 * sim.synset_synset
: 0.35 * sim.lemma + 0.25 * sim.related + 0.001 * sim.synset
}
export type Edge = {
source: string
target: string
value: number
lineStyle: {width: number; opacity: number}
}
export type Node = {
x: number
y: number
category: string | string[]
host: string
name: string
value: number
prop: number
symbolSize: number
itemStyle: {
opacity: number
}
label: {
show: boolean
}
}
async function processComparisons(
i: number,
terms: {[index: string]: TermEntry},
edges: Edge[],
record: {[index: string]: boolean},
catCounts: NumberObject,
progress: (progress: number[]) => void,
finish: (data: {edges: Edge[]; nodes: Node[]}) => void,
options: ProcessOptions,
) {
clearTimeout(timers.comparisons)
const termKeys = Object.keys(terms)
const n = termKeys.length
for (let step = 0; i < n && step < 100; step++, i++) {
const term = terms[termKeys[i]]
term.processed.lookup.map.forEach((_, child) => {
const key = term.term + '.' + child
const rkey = child + '.' + term.term
if (term.term !== child && !(key in record) && !(rkey in record) && child in terms) {
record[key] = record[rkey] = true
const value = getSimilarity(term, terms[child], options.dense)
if (value > options.min_sim)
edges.push({
source: term.term,
target: child,
value: value,
lineStyle: {width: 0.3 + value * 2, opacity: 0.35},
})
}
})
}
progress([i, n])
if (i < n) {
timers.comparisons = setTimeout(
() => processComparisons(i, terms, edges, record, catCounts, progress, finish, options),
0,
)
} else {
setTimeout(() => {
const nodeIndices: {[index: string]: number} = {}
const nodes: Node[] = Object.values(terms).map((term, index) => {
nodeIndices[term.term] = index
return {
x: Math.random() * window.innerWidth,
y: Math.random() * window.innerHeight,
host: term.host || '',
category: Object.keys(term.categories).sort((a, b) => catCounts[a] - catCounts[b]),
name: term.term,
prop: 1,
value: 0,
symbolSize: 10,
itemStyle: {
opacity: 1,
},
label: {
show: true,
},
}
})
edges.forEach(edge => {
nodes[nodeIndices[edge.source]].value += edge.value
nodes[nodeIndices[edge.target]].value += edge.value
})
let max = 0
nodes.forEach(({value}) => {
if (value > max) max = value
})
finish({
edges,
nodes: nodes.map(node => {
node.prop = node.value / max
node.itemStyle = {opacity: 0.6 + node.prop * 0.4}
return node
}),
})
}, 0)
}
}
export default function Results({
allTerms,
catCounts,
selectedCategories,
options,
plotOptions,
}: {
allTerms: Map<string, DictEntry>
catCounts: NumberObject
selectedCategories: string[]
options: ProcessOptions
plotOptions: PlotOptions
}) {
const data = useContext(ResourceContext)
const dict = useContext(BuildContext)
const [network, setNetwork] = useState<{edges: Edge[]; nodes: Node[]}>({edges: [], nodes: []})
const [progress, setProgress] = useState([0, 0])
useEffect(() => {
const selectedMap = new Map(selectedCategories.map((cat, index) => [index, cat]))
const terms: {[index: string]: TermEntry} = {}
allTerms.forEach((entry, term) => {
let keep = false
const cats: {[index: string]: number} = {}
selectedMap.forEach(cat => {
if (cat in entry.categories || (cat === 'no categories' && !Object.keys(entry.categories).length)) {
keep = true
cats[cat] = entry.categories[cat]
}
})
if (keep) {
const processed = getProcessedTerm(term, data, dict, true)
Iif (processed.type === 'fixed') {
if (!(term in terms)) {
terms[term] = {term, categories: cats, processed}
}
} else if (options.include_fuzzy) {
processed.matches.forEach(match => {
Eif (!(match in dict) && !(match in terms)) {
terms[match] = {
host: term,
term: match,
categories: cats,
processed: getProcessedTerm(match, data, {}, true) as FixedTerm,
}
}
})
}
}
})
const record: {[index: string]: boolean} = {}
const edges: Edge[] = []
processComparisons(0, terms, edges, record, catCounts, setProgress, setNetwork, options)
}, [dict, data, options, selectedCategories, allTerms, catCounts])
return progress[0] < progress[1] ?
<Box sx={{position: 'relative', width: '100%', height: '100%'}}>
<Box
sx={{
display: 'flex',
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
alignItems: 'center',
justifyContent: 'center',
textAlign: 'center',
}}
>
<Stack>
<Typography variant="h5">Calculating Pairwise Similarities</Typography>
<LinearProgress variant="determinate" value={(progress[0] / progress[1]) * 100} />
<Typography variant="caption">{progress[0] + ' / ' + progress[1]}</Typography>
</Stack>
</Box>
</Box>
: <Box sx={{position: 'relative', width: '100%', height: '100%'}}>
<Graph allNodes={network.nodes} edges={network.edges} options={plotOptions} />
</Box>
}
|