All files / app addedTerms.tsx

83.33% Statements 35/42
55.55% Branches 15/27
77.77% Functions 14/18
89.18% Lines 33/37

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                        88x         324x             2104x 2104x 2104x 2104x 2104x 2104x   618x   828x         2104x 344x               2018x                                   2194x                 40x                                                         344x 228x         10x 10x 10x   10x       344x   2104x 2104x 2104x 1522x       2104x 717x 717x 452x 452x       2104x 2104x                                                            
import {Backdrop, Box, IconButton, LinearProgress, Stack, Typography} from '@mui/material'
import {useContext, useMemo, useState, useEffect, useCallback} from 'react'
import {RemoveCircleOutline} from '@mui/icons-material'
import {TermLink, TermSenseEdit} from './term'
import {ResourceContext} from './resources'
import {AllCategories, BuildContext, BuildEditContext} from './building'
import type {GridColDef, GridRenderEditCellParams} from '@mui/x-data-grid'
import {makeRows} from './processTerms'
import {CategoryEditor} from './categoryEditor'
import {type GridRow, Table, type GridCell} from './table'
import {ImportMenu} from './import'
 
export const timers: {dictionary: number | NodeJS.Timeout; comparisons: number | NodeJS.Timeout} = {
  dictionary: 0,
  comparisons: 0,
}
function byTime(a: GridRow, b: GridRow) {
  return b.dictEntry.added - a.dictEntry.added
}
export default function AddedTerms({
  editFromEvent,
}: {
  editFromEvent: (value: number | string, params: GridCell) => void
}) {
  const Data = useContext(ResourceContext)
  const Dict = useContext(BuildContext)
  const Cats = useContext(AllCategories)
  const isCategory = useCallback((name: string) => Cats.includes(name), [Cats])
  const editDictionary = useContext(BuildEditContext)
  const dictTerms = useMemo(
    () =>
      Object.freeze(
        Object.keys(Dict)
          .map(id => Dict[id].term || id)
          .sort()
      ),
    [Dict]
  )
  const cols = useMemo(() => {
    const out: GridColDef[] = [
      {
        field: 'remove',
        headerName: '',
        width: 1,
        sortable: false,
        disableColumnMenu: true,
        renderCell: (params: GridRenderEditCellParams) => {
          return (
            <IconButton
              sx={{opacity: 0.4, cursor: 'not-allowed'}}
              size="small"
              aria-label="remove term"
              onClick={() => {
                editDictionary({type: 'remove', term_id: params.id as string})
              }}
            >
              <RemoveCircleOutline sx={{fontSize: '.9em'}} />
            </IconButton>
          )
        },
      },
      {
        field: 'term',
        headerName: 'Term',
        renderCell: (params: GridRenderEditCellParams) => {
          return <TermLink term={params.value} />
        },
      },
      {
        field: 'sense',
        headerName: 'Sense',
        description: 'assigned sense key',
        editable: true,
        renderEditCell: (params: GridRenderEditCellParams) => {
          return (
            <TermSenseEdit
              id={params.id as string}
              field={params.field}
              processed={params.row.processed}
              editCell={params.api.setEditCellValue}
            />
          )
        },
      },
      {
        field: 'frequency',
        headerName: 'Frequency',
        description:
          'relative frequency; 100 - index / n terms * 100; terms are loosely sorted by frequency and space coverage',
      },
      {field: 'matches', headerName: 'Matches', description: 'number of matches found in the full term list'},
      {field: 'senses', headerName: 'Senses', description: 'number of directly associated senses'},
      {
        field: 'related',
        headerName: 'Related',
        description: 'number of similar terms on record, based on agreement between embeddings spaces',
      },
      {
        field: 'ncats',
        headerName: 'Categories',
        description: 'number of categories the term has weight in',
      },
    ]
    Cats.forEach(cat =>
      out.push({
        field: 'category_' + cat,
        headerName: cat,
        editable: true,
        valueParser: (value: string | number, row: GridRow, params) => {
          const parsed = +value || ''
          Eif (params) {
            editFromEvent(parsed, {...row, field: params.field})
          }
          return parsed
        },
      })
    )
    return out
  }, [Cats, editFromEvent, editDictionary])
  const [rows, setRows] = useState<GridRow[]>([])
  const [progress, setProgress] = useState(0)
  const processing = useMemo(() => {
    Eif (dictTerms.length < 1000) return false
    const rowTerms = rows.map(({id}) => id).sort()
    return Math.abs(rowTerms.length - dictTerms.length) < 2 ? false : rowTerms.join() !== dictTerms.join()
  }, [rows, dictTerms])
  useEffect(() => {
    Eif (Data.termAssociations && Data.synsetInfo) {
      makeRows(Dict, Data, processing ? setProgress : undefined).then(res => {
        setRows(res.sort(byTime))
        Iif (processing) setProgress(0)
      })
    }
  }, [Dict, Data, processing])
  const [editCategory, setEditCategory] = useState('')
  return (
    <Box component="main" sx={{height: '100%'}}>
      {!dictTerms.length || (!processing && !rows.length) ? (
        <Stack sx={{p: 3}}>
          <Typography align="center">Add terms, or import an existing dictionary.</Typography>
          <Box sx={{m: 'auto', p: 1}}>
            <ImportMenu>Import</ImportMenu>
          </Box>
        </Stack>
      ) : processing ? (
        <Backdrop open={processing} sx={{alignItems: 'baseline'}}>
          <Stack sx={{textAlign: 'center', mt: 12}}>
            <Typography variant="h4">Processing Dictionary</Typography>
            <LinearProgress variant="determinate" value={progress * 100} />
            <Typography variant="caption">{progress ? Math.round(progress * 100) + '%' : 'preparing...'}</Typography>
          </Stack>
        </Backdrop>
      ) : (
        <Table
          rows={rows}
          columns={cols}
          isCategory={isCategory}
          setEditCategory={setEditCategory}
          editFromEvent={editFromEvent}
        />
      )}
      <CategoryEditor category={editCategory} onClose={() => setEditCategory('')} />
    </Box>
  )
}