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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | 56x 56x 56x 56x 16x 42x 16x 56x 16x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 1x 1x 56x 2x 3x 3x 1x 1x 2x 2x 2x 1x 1x 1x 2x 1x 2x 10x 10x 24x 1x 10x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 5x 3x 3x 3x 7x 5x 2x 2x 2x 2x 3x 3x | import {Close} from '@mui/icons-material'
import {
Autocomplete,
Button,
Chip,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
FormControlLabel,
IconButton,
Stack,
Switch,
TextField,
Toolbar,
Tooltip,
Typography,
} from '@mui/material'
import {type ChangeEvent, type KeyboardEvent, useContext, useMemo, useState} from 'react'
import {BuildContext, type DictionaryActions, type NumberObject} from './building'
import {ResourceContext} from './resources'
import {globToRegex, special, wildcards} from './lib/utils'
import {extractMatches, getProcessedTerm} from './processTerms'
import {getIntersects} from './analysisResults'
import type {FixedTerm, NetworkLookup} from './term'
import type {DictEntry} from './storage'
export function CategoryWeights({
name,
current,
edit,
}: {
name: string
current: NumberObject
edit: (action: DictionaryActions) => void
}) {
const data = useContext(ResourceContext)
const {terms, collapsedTerms, termLookup} = data
const dict = useContext(BuildContext)
const dictTerms = useMemo(() => {
const out: {[index: string]: boolean} = {}
Object.keys(dict).forEach(id => (out[dict[id].term || id] = true))
return out
}, [dict])
const dictTermsCollapsed = useMemo(() => {
return {all: ';;' + Object.keys(dictTerms).join(';;') + ';;'}
}, [dictTerms])
const [menuOpen, setMenuOpen] = useState(false)
const toggleMenu = () => setMenuOpen(!menuOpen)
const [toAllTerms, setToAllTerms] = useState(true)
const [similarityBased, setSimilarityBased] = useState(true)
const [scale, setScale] = useState(true)
const [anyTerms, setAnyTerms] = useState(true)
const [value, setValue] = useState('1')
const [inputTerm, setInputTerm] = useState('')
const [termSuggestions, setTermSuggestions] = useState<string[]>([])
const [coreTerms, setCoreTerms] = useState<string[]>([])
const isValidTerm =
anyTerms ? (term: string) => termLookup && term in termLookup : (term: string) => term in dictTerms
const addCore = (term: string) => {
Eif (term && !coreTerms.includes(term) && isValidTerm(term)) {
setCoreTerms([...coreTerms, term])
}
}
return (
<>
<Button onClick={toggleMenu}>Fill Weights</Button>
{menuOpen && (
<Dialog open={menuOpen} onClose={toggleMenu}>
<DialogTitle>Category Weight Filler</DialogTitle>
<IconButton
aria-label="close category weight filler"
onClick={toggleMenu}
sx={{
position: 'absolute',
right: 8,
top: 12,
}}
className="close-button"
>
<Close />
</IconButton>
<DialogContent sx={{minWidth: '300px', minHeight: '300px', p: 1}}>
<Stack spacing={2}>
<Tooltip
title={
similarityBased ?
'Weights will be similarity to the specified core terms'
: 'Will use the specified value as the weight of all terms'
}
placement="right"
>
<FormControlLabel
control={
<Switch
size="small"
checked={similarityBased}
onChange={() => setSimilarityBased(!similarityBased)}
></Switch>
}
label={<Typography variant="caption">Similarity-based</Typography>}
labelPlacement="start"
/>
</Tooltip>
{similarityBased && terms ?
<Stack>
<Autocomplete
multiple
disableCloseOnSelect
options={termSuggestions}
onKeyUp={(e: KeyboardEvent<HTMLDivElement>) => {
const inputValue = 'value' in e.target ? (e.target.value as string) : ''
if (
(e.code === 'Enter' || e.code === 'NumpadEnter') &&
inputTerm &&
(!inputValue || inputValue === inputTerm)
) {
addCore(inputValue)
return
}
Eif (terms) {
const suggestions: string[] = []
if (inputValue && collapsedTerms) {
let ex: RegExp | undefined
try {
ex = new RegExp(
wildcards.test(inputValue) ? globToRegex(inputValue) : ';' + inputValue + '[^;]*;',
'g',
)
} catch {
ex = new RegExp(';' + inputValue.replace(special, '\\%&') + ';', 'g')
}
extractMatches('', ex, anyTerms ? collapsedTerms : dictTermsCollapsed, suggestions, 100)
}
coreTerms.forEach(term => {
Eif (!suggestions.includes(term)) suggestions.push(term)
})
setTermSuggestions(suggestions)
}
}}
value={coreTerms}
onChange={(e, value) => {
if (Math.abs(value.length - coreTerms.length) === 1) {
setCoreTerms([...value])
}
}}
renderTags={(value: readonly string[], getTagProps) => {
return value.map((option: string, index: number) => (
<Chip label={option} {...getTagProps({index})} key={option} />
))
}}
renderInput={params => (
<TextField
{...params}
size="small"
label="Core Terms"
aria-label="category weight core terms"
value={inputTerm}
onChange={(e: ChangeEvent<HTMLInputElement>) => {
Eif (!coreTerms.includes(e.target.value)) setInputTerm(e.target.value)
}}
></TextField>
)}
filterOptions={x => x}
noOptionsText="No matches"
filterSelectedOptions
selectOnFocus
clearOnBlur
clearOnEscape
handleHomeEndKeys
fullWidth
></Autocomplete>
<Toolbar sx={{m: 0, justifyContent: 'space-between'}}>
<Button
onClick={() => {
setTermSuggestions([])
setCoreTerms([])
}}
>
Clear
</Button>
<Tooltip
title={
anyTerms ?
'Allowing any terms from the full list on record'
: 'Allowing only terms included in the category'
}
placement="top"
>
<FormControlLabel
control={
<Switch
size="small"
checked={anyTerms}
onChange={() => {
setTermSuggestions([])
setAnyTerms(!anyTerms)
}}
></Switch>
}
label={<Typography variant="caption">Allow any terms</Typography>}
labelPlacement="top"
/>
</Tooltip>
<Tooltip
title={
scale ?
'Will rescaling similarities to be between 0 and 1'
: 'Will display raw similarity values'
}
placement="top"
>
<FormControlLabel
control={<Switch size="small" checked={scale} onChange={() => setScale(!scale)}></Switch>}
label={<Typography variant="caption">Scale</Typography>}
labelPlacement="top"
/>
</Tooltip>
</Toolbar>
</Stack>
: <TextField
fullWidth
size="small"
label="Value"
id="category_weight_filler_value"
value={value}
onChange={(e: ChangeEvent<HTMLInputElement>) => setValue(e.target.value)}
/>
}
<Tooltip
title={
toAllTerms ?
'Will apply weight to all terms in the dictionary'
: 'Will apply weight only to terms with weight in the current category'
}
placement="right"
>
<FormControlLabel
control={
<Switch size="small" checked={toAllTerms} onChange={() => setToAllTerms(!toAllTerms)}></Switch>
}
label={<Typography variant="caption">Apply to all terms</Typography>}
labelPlacement="start"
/>
</Tooltip>
</Stack>
</DialogContent>
<DialogActions sx={{justifyContent: 'space-between'}}>
<Button onClick={toggleMenu}>Cancel</Button>
<Button
variant="contained"
onClick={() => {
const reWeighted: NumberObject = {}
if (similarityBased) {
const processedCores: Map<string, NetworkLookup> = new Map()
coreTerms.forEach(term => {
const processed = getProcessedTerm(term, data, dict, true)
if (processed.type === 'fixed') {
processedCores.set(term, processed.lookup)
} else E{
processed.matches.forEach(match => {
processedCores.set(match, (getProcessedTerm(match, data, dict, true) as FixedTerm).lookup)
})
}
})
const range = [Infinity, -Infinity]
Object.keys(toAllTerms ? dictTerms : current).forEach(term => {
let totalSim = 0
processedCores.forEach(processedCoreTerm => {
const processed = getProcessedTerm(term, data, dict)
if (processed.type === 'fixed') {
const sim = getIntersects(term, processedCoreTerm)
totalSim +=
1 * (sim.lemma || sim.related || sim.synset) +
0.1 * (sim.lemma_related || sim.lemma_synset) +
0.01 * (sim.related_lemma || sim.related_related || sim.related_synset) +
0.001 * (sim.synset_lemma || sim.synset_related || sim.synset_synset)
} else E{
processed.matches.forEach(match => {
const sim = getIntersects(match, processedCoreTerm)
totalSim +=
1 * (sim.lemma || sim.related || sim.synset) +
0.1 * (sim.lemma_related || sim.lemma_synset) +
0.01 * (sim.related_lemma || sim.related_related || sim.related_synset) +
0.001 * (sim.synset_lemma || sim.synset_related || sim.synset_synset)
})
}
})
Eif (scale) {
if (range[0] > totalSim) range[0] = totalSim
Eif (range[1] < totalSim) range[1] = totalSim
}
Eif (totalSim) reWeighted[term] = totalSim
})
Eif (scale) {
const top = range[1] - range[0]
Object.keys(reWeighted).forEach(term => {
reWeighted[term] = Math.max((reWeighted[term] - range[0]) / top, 0.0001)
})
}
} else {
if (toAllTerms) {
Object.keys(dict).forEach(term => (reWeighted[term] = +value))
} else E{
Object.keys(current).forEach(term => {
if (current[term]) reWeighted[term] = +value
})
}
}
const subset = toAllTerms ? dict : current
const appliedWeights: NumberObject = {}
Object.keys(subset).forEach(id => {
if (id in reWeighted) {
appliedWeights[id] = reWeighted[id]
} else Eif (toAllTerms) {
const {term} = subset[id] as DictEntry
Eif (term && term in reWeighted) {
appliedWeights[id] = reWeighted[term]
}
}
})
edit({type: 'reweight_category', name, weights: appliedWeights})
toggleMenu()
}}
>
Fill
</Button>
</DialogActions>
</Dialog>
)}
</>
)
}
|