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 | 88x 88x 88x 4x 4x 4x 4x 4x 40x 40x 40x 40x 40x 4x 88x 88x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 4x 4x 4x 208x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 40x 40x 40x 40x 36x 36x 4x 4x 208x 4x 4x 40x 4x 4x 4x 4x 4x 4x 208x 4x 4x 4x 4x 208x 24x 24x 4x 12x 4x 4x 4x 4x 4x 4x 4x | import {Close, Visibility, VisibilityOff} from '@mui/icons-material' import { Box, Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControl, FormControlLabel, IconButton, InputLabel, MenuItem, Select, Stack, Switch, TextField, Tooltip, Typography, styled, } from '@mui/material' import {type ChangeEvent, type DragEvent, useState, useContext} from 'react' import {type CoarseSenseMap, ResourceContext, SenseMapSetter} from './resources' const HiddenInput = styled('input')({ clip: 'rect(0 0 0 0)', clipPath: 'inset(50%)', height: 1, overflow: 'hidden', position: 'absolute', bottom: 0, left: 0, whiteSpace: 'nowrap', width: 1, }) const quotes = /^"|"$/g const sep = /"*,"*/ function parseCoarseMap(rows: readonly string[][], fine_col: number, coarse_col: number) { const parsed: CoarseSenseMap = {} Eif (rows) { Eif (fine_col !== -1 && coarse_col !== -1) { try { rows.forEach(row => { const fine = row[fine_col] const coarse = row[coarse_col] Eif (fine && coarse && coarse.length) { Iif (fine in parsed) { const existing = parsed[fine] if (!existing.includes(coarse)) existing.push(coarse) } else { parsed[fine] = [coarse] } } }) } catch {} } } return parsed } const newLine = /[\r\n]+/ const senseKeySep = /%/ export function ImportCoarseSenseMap() { const {sense_keys, SenseLookup, NLTKLookup} = useContext(ResourceContext) const [menuOpen, setMenuOpen] = useState(false) const toggleMenu = () => setMenuOpen(!menuOpen) const [store, setStore] = useState(true) const [encrypt, setEncrypt] = useState(false) const [header, setHeader] = useState<string[]>([]) const [rows, setRows] = useState<readonly string[][]>([]) const [selectedCols, setSelectedCols] = useState(['', '']) const [NLTKLabels, setNLTKLabels] = useState(false) const [recognized, setRecognized] = useState([0, 0, 0]) const [map, setMap] = useState<CoarseSenseMap>({}) const [password, setPassword] = useState('') const [hide, setHide] = useState(true) const senseMapSetter = useContext(SenseMapSetter) const clear = () => { setHeader([]) setRows([]) setPassword('') } const parseMap = (rows: readonly string[][], fine: number, coarse: number) => { const map = parseCoarseMap(rows, fine, coarse) const recognizedMap: CoarseSenseMap = {} const fineMapped = Object.keys(map) Eif (fineMapped.length && sense_keys) { const isNLTK = !senseKeySep.test(fineMapped[0]) setNLTKLabels(isNLTK) const lookup = isNLTK ? NLTKLookup : SenseLookup const coarse: Set<string> = new Set() let nRecognized = 0 fineMapped.forEach(key => { const to = map[key] Iif ('string' === typeof to) { coarse.add(to) } else { to.forEach(l => coarse.add(l)) } if (key in lookup) { nRecognized++ recognizedMap[isNLTK ? sense_keys[NLTKLookup[key]] : key] = to } }) setRecognized([fineMapped.length, nRecognized, coarse.size]) } setMap(recognizedMap) } const handleUpload = (rawRows: string[]) => { Eif (rawRows.length > 1) { const header = rawRows.splice(0, 1)[0].replace(quotes, '').split(sep) const rows = rawRows.map(row => row.replace(quotes, '').split(sep)) setRows(Object.freeze(rows)) setHeader(header) Eif (header.length > 1) { const cols = [header[0], header[1]] setSelectedCols(cols) parseMap(rows, 0, 1) } } } const setCoarseMap = () => { const rawMap = {header, selectedCols, rows, NLTKLabels} senseMapSetter(map, {rawMap, store}, password) clear() setMenuOpen(false) } return ( <> <Button variant="outlined" aria-label="import coarse sense map" onClick={toggleMenu}> Import </Button> <Dialog open={menuOpen} onClose={toggleMenu} onDrop={(e: DragEvent) => { e.preventDefault() if (e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files.length) { const file = e.dataTransfer.files[0] const reader = new FileReader() reader.onload = () => { handleUpload((reader.result as string).split(newLine)) } reader.readAsText(file) } }} > <DialogTitle>Import Coarse Sense Mappings</DialogTitle> <IconButton aria-label="close coarse sense import menu" onClick={toggleMenu} sx={{ position: 'absolute', right: 8, top: 12, }} className="close-button" > <Close /> </IconButton> <DialogContent sx={{p: 1, minWidth: '400px'}}> {rows.length > 1 ? ( <Stack spacing={1} direction="row"> <Box sx={{width: '50%'}}> <Tooltip title="Name of the column containing fine-grained sense labels (such as sense keys)." placement="left" > <FormControl fullWidth> <InputLabel id="fine_sense_select">Fine Senses</InputLabel> <Select labelId="fine_sense_select" label="Fine Senses" size="small" value={selectedCols[0]} onChange={e => { const value = e.target.value const cols = [value, selectedCols[selectedCols[1] === value ? 0 : 1]] parseMap(rows, header.indexOf(cols[0]), header.indexOf(cols[1])) setSelectedCols(cols) }} > {header.map((colName, index) => { return ( <MenuItem key={colName + index} value={colName}> {colName} </MenuItem> ) })} </Select> </FormControl> </Tooltip> <Typography sx={{pl: 2}} variant="caption"> <span className="number">{recognized[1]} </span> of <span className="number"> {recognized[0]} </span> recognized </Typography> </Box> <Box sx={{width: '50%'}}> <Tooltip title="Name of the column containing coarse-grained sense labels (such as sense clusters)." placement="right" > <FormControl fullWidth> <InputLabel id="coarse_sense_select">Coarse Senses</InputLabel> <Select labelId="coarse_sense_select" label="Coarse Senses" size="small" value={selectedCols[1]} onChange={e => { const value = e.target.value const cols = [selectedCols[selectedCols[0] === value ? 1 : 0], value] parseMap(rows, header.indexOf(cols[0]), header.indexOf(cols[1])) setSelectedCols(cols) }} > {header.map((colName, index) => { return ( <MenuItem key={colName + index} value={colName}> {colName} </MenuItem> ) })} </Select> </FormControl> </Tooltip> <Typography sx={{pl: 2}} variant="caption"> <span className="number"> {recognized[2]} </span> unique labels </Typography> </Box> </Stack> ) : ( <Typography sx={{p: 2}}> Select a CSV file with columns containing fine- and coarse-gained sense labels. </Typography> )} <Stack spacing={1}> <Stack spacing={2} direction="row" sx={{justifyContent: 'flex-end'}}> <Tooltip title="Store the imported map locally." placement="top"> <FormControlLabel label="Store" labelPlacement="start" control={<Switch checked={store} onChange={() => setStore(!store)} />} /> </Tooltip> {store ? ( <Tooltip title="Encrypt the sense map when being stored." placement="right"> <FormControlLabel label="Encrypt" labelPlacement="start" control={<Switch checked={encrypt} onChange={() => setEncrypt(!encrypt)} />} /> </Tooltip> ) : ( <></> )} </Stack> {store && encrypt ? ( <Box> <TextField fullWidth size="small" label="Password" type={hide ? 'password' : 'text'} value={password} onChange={(e: ChangeEvent<HTMLInputElement>) => setPassword(e.target.value)} /> <IconButton sx={{position: 'absolute', right: 15}} aria-label="toggle password visibility" onClick={() => setHide(!hide)} > {hide ? <VisibilityOff /> : <Visibility />} </IconButton> </Box> ) : ( <></> )} </Stack> </DialogContent> <DialogActions sx={{justifyContent: 'space-between'}}> <Stack direction="row" spacing={1}> <Tooltip title="select a file to import a coarse sense map from"> <Button variant="outlined" component="label"> File <HiddenInput type="file" onChange={(e: ChangeEvent<HTMLInputElement>) => { Eif (e.target.files && e.target.files.length) { const file = e.target.files[0] const reader = new FileReader() reader.onload = () => { handleUpload((reader.result as string).split(newLine)) e.target.value = '' } reader.readAsText(file) } }} /> </Button> </Tooltip> <Tooltip title="clear any loaded content"> <Button onClick={clear}>clear</Button> </Tooltip> </Stack> <Button variant="contained" disabled={encrypt ? !password : false} onClick={setCoarseMap}> Set </Button> </DialogActions> </Dialog> </> ) } |