All files / app senseMapExport.tsx

77.77% Statements 49/63
72.88% Branches 43/59
66.66% Functions 8/12
76.66% Lines 46/60

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                                          32x 32x 32x 32x 32x 32x 32x 32x 32x 18x 18x 18x 18x 16x 16x 16x 16x 16x 16x 160x 160x 160x 136x 136x   136x     136x 136x     16x 156x 156x 156x 156x   156x 156x 156x 156x 20x 20x 20x 20x           2x 2x 2x       2x       18x   32x                                                                                                                                                                                
import {Close} from '@mui/icons-material'
import {
  Button,
  Dialog,
  DialogActions,
  DialogContent,
  DialogTitle,
  FormControl,
  FormControlLabel,
  FormLabel,
  IconButton,
  Stack,
  Switch,
  TextField,
  Typography,
  useTheme,
} from '@mui/material'
import {type ChangeEvent, useContext, useMemo, useState} from 'react'
import {ResourceContext} from './resources'
 
export function ExportCoarseSenseMap() {
  const theme = useTheme()
  const {senseMap, senseMapOptions, synsetInfo, sense_keys, SenseLookup, NLTKLookup} = useContext(ResourceContext)
  const senseMapRaw = senseMapOptions.rawMap
  const [menuOpen, setMenuOpen] = useState(false)
  const toggleMenu = () => setMenuOpen(!menuOpen)
  const [name, setName] = useState('coarse_sense_map')
  const [useNLTK, setUseNLTK] = useState(senseMapRaw ? senseMapRaw.NLTKLabels : false)
  const [originalFormat, setOriginalFormat] = useState(!!senseMapRaw)
  const content = useMemo(() => {
    const rows = [originalFormat && senseMapRaw ? senseMapRaw.header.join(',') : '"fine_sense","coarse_sense"']
    const fine_senses = Object.keys(senseMap)
    Eif (fine_senses.length && sense_keys && synsetInfo) {
      if (originalFormat && senseMapRaw) {
        const {header, selectedCols, rows: originalRows, NLTKLabels} = senseMapRaw
        const fine_col = header.indexOf(selectedCols[0])
        const coarse_col = header.indexOf(selectedCols[1])
        Eif (fine_col !== -1 && coarse_col !== -1) {
          const mapped: {[index: string]: boolean} = {}
          originalRows.forEach(row => {
            const fine = NLTKLabels ? sense_keys[NLTKLookup[row[fine_col]]] : row[fine_col]
            const coarse = row[coarse_col]
            if (fine in senseMap && senseMap[fine].indexOf(coarse) !== -1) {
              const newRow = [...row]
              Iif (NLTKLabels && !useNLTK) {
                newRow[fine_col] = sense_keys[NLTKLookup[row[fine_col]]]
              } else Iif (!NLTKLabels && useNLTK && fine in SenseLookup) {
                newRow[fine_col] = synsetInfo[SenseLookup[fine]].nltk_id
              }
              rows.push(newRow.join(','))
              mapped[newRow[fine_col] + coarse] = true
            }
          })
          fine_senses.forEach(fine => {
            const coarses = senseMap[fine]
            Eif (useNLTK && fine in SenseLookup) {
              const {nltk_id} = synsetInfo[SenseLookup[fine]]
              fine = nltk_id
            }
            const nCols = header.length
            coarses.forEach(coarse => {
              const key = fine + coarse
              if (fine && coarse && !(key in mapped)) {
                const row = new Array(nCols)
                row[fine_col] = fine
                row[coarse_col] = coarse
                rows.push(row.join(','))
              }
            })
          })
        }
      } else {
        fine_senses.forEach(fine => {
          const coarse = senseMap[fine]
          Iif (useNLTK && fine in SenseLookup) {
            const {nltk_id} = synsetInfo[SenseLookup[fine]]
            fine = nltk_id
          }
          Eif (fine && coarse) coarse.forEach(l => rows.push('"' + fine + '","' + l + '"'))
        })
      }
    }
    return rows.length > 1 ? rows.join('\n') : ''
  }, [useNLTK, originalFormat, NLTKLookup, SenseLookup, senseMap, senseMapRaw, sense_keys, synsetInfo])
  return (
    <>
      <Button variant="outlined" onClick={toggleMenu}>
        Export
      </Button>
      {menuOpen && (
        <Dialog open={menuOpen} onClose={toggleMenu}>
          <DialogTitle>Export Coarse Sense Map</DialogTitle>
          <IconButton
            aria-label="close export menu"
            onClick={toggleMenu}
            sx={{
              position: 'absolute',
              right: 8,
              top: 12,
            }}
            className="close-button"
          >
            <Close />
          </IconButton>
          <DialogContent sx={{p: 1}}>
            <Stack spacing={1}>
              <TextField
                size="small"
                label="Filename"
                value={name}
                onChange={(e: ChangeEvent<HTMLInputElement>) => {
                  setName(e.target.value)
                }}
              />
              <FormControl>
                <FormLabel sx={{fontSize: '.8em'}} htmlFor="export_content">
                  Export Content
                </FormLabel>
                <textarea
                  id="export_content"
                  style={{
                    backgroundColor: theme.palette.background.default,
                    color: theme.palette.text.primary,
                    whiteSpace: 'pre',
                    minWidth: '35em',
                    minHeight: '20em',
                  }}
                  value={content}
                  readOnly
                ></textarea>
              </FormControl>
            </Stack>
          </DialogContent>
          <DialogActions sx={{justifyContent: 'space-between'}}>
            <FormControlLabel
              control={<Switch size="small" checked={useNLTK} onChange={() => setUseNLTK(!useNLTK)}></Switch>}
              label={<Typography variant="caption">NLTK-Style Labels</Typography>}
              labelPlacement="top"
            />
            <FormControlLabel
              control={
                <Switch
                  size="small"
                  checked={originalFormat}
                  onChange={() => setOriginalFormat(!originalFormat)}
                  disabled={!senseMapRaw}
                ></Switch>
              }
              label={<Typography variant="caption">Match Import Format</Typography>}
              labelPlacement="top"
            />
            <Button
              variant="contained"
              onClick={() => {
                if (name && content) {
                  const a = document.createElement('a')
                  a.setAttribute('href', URL.createObjectURL(new Blob([content], {type: 'text/plain'})))
                  a.setAttribute('download', name + '.csv')
                  document.body.appendChild(a)
                  a.click()
                  document.body.removeChild(a)
                }
              }}
            >
              Download
            </Button>
          </DialogActions>
        </Dialog>
      )}
    </>
  )
}