|
1 | | -import { |
2 | | - InvalidIndexNameError, |
3 | | - MultipleTextIDsWithoutSpecifiedTextIDError |
4 | | -} from '../util/errors'; |
5 | | -import type { |
6 | | - ImportAnnotation |
7 | | -} from '@/types/import-annotation'; |
8 | | - |
9 | | -export const parseAlgorithmAnnotationsCSV = ( |
10 | | - text: string, |
11 | | - title: string, |
12 | | - textId?: string, |
13 | | - textName: string = 'text', |
14 | | - algorithm_id: string = 'algorithm_id', |
15 | | - fixationName: string = 'fix_uid', |
16 | | - boxName: string = 'char_uid', |
17 | | - dGeomName: string = 'D_geom', |
18 | | - pShareName: string = 'P_share' |
19 | | -): ImportAnnotation => { |
20 | | - const lines = text.split( /\r?\n/ ).filter( l => l.trim() !== '' ); |
21 | | - const header = lines.shift()!.split( ',' ) |
22 | | - .map( h => h.trim() ); |
23 | | - const algorithmName = header.indexOf( algorithm_id ); |
24 | | - const textIndex = header.indexOf( textName ); |
25 | | - const fixIndex = header.indexOf( fixationName ); |
26 | | - const boxIndex = header.indexOf( boxName ); |
27 | | - const dGeomIndex = header.indexOf( dGeomName ); |
28 | | - const pShareIndex = header.indexOf( pShareName ); |
29 | | - |
30 | | - if ( fixIndex < 0 ) |
31 | | - throw new InvalidIndexNameError( 'X coordinate' ); |
32 | | - else if ( boxIndex < 0 ) |
33 | | - throw new InvalidIndexNameError( 'Y coordinate' ); |
34 | | - else if ( algorithmName < 0 ) |
35 | | - throw new InvalidIndexNameError( 'algorithm ID' ); |
36 | | - else if ( textIndex < 0 ) |
37 | | - throw new InvalidIndexNameError( 'text ID' ); |
38 | | - |
39 | | - const firstCols = lines[0]!.split( ',' ); |
40 | | - const firstEncounteredTextID = firstCols[ textIndex ]; |
41 | | - // First index is text, second id is reader |
42 | | - const annotations: ImportAnnotation = {}; |
43 | | - |
44 | | - for ( let i = 0; i < lines.length; i++ ) { |
45 | | - const cols = lines[i]!.split( ',' ); |
46 | | - |
47 | | - if ( firstEncounteredTextID !== cols[ textIndex ] && !textId ) { |
48 | | - throw new MultipleTextIDsWithoutSpecifiedTextIDError(); |
49 | | - } |
50 | | - |
51 | | - if ( textId === undefined ) { |
52 | | - const algorithm = cols[ algorithmName ]!; |
53 | | - |
54 | | - if ( !annotations[ algorithm ] ) { |
55 | | - annotations[ algorithm ] = { |
56 | | - 'title': title, |
57 | | - 'annotations': [] |
58 | | - }; |
59 | | - } |
60 | | - |
61 | | - const preAnnotation = { |
62 | | - 'foreignFixationId': parseInt( cols[ fixIndex ]! ), |
63 | | - 'foreignCharacterBoxId': parseInt( cols[ boxIndex ]! ), |
| 1 | +import { |
| 2 | + InvalidIndexNameError, |
| 3 | + MultipleTextIDsWithoutSpecifiedTextIDError |
| 4 | +} from '../util/errors'; |
| 5 | +import type { |
| 6 | + ImportAnnotation |
| 7 | +} from '@/types/import-annotation'; |
| 8 | +import type { |
| 9 | + PreAnnotationValueDto |
| 10 | +} from '@/types/dtos/PreAnnotationValueDto'; |
| 11 | + |
| 12 | +export const parseAlgorithmAnnotationsCSV = ( |
| 13 | + text: string, |
| 14 | + title: string, |
| 15 | + textFilter: undefined | string | [number, number] | number[], |
| 16 | + textName: string = 'text', |
| 17 | + algorithmName: string = 'algorithm_id', |
| 18 | + fixationName: string = 'fix_uid', |
| 19 | + boxName: string = 'char_uid', |
| 20 | + dGeomName: string = 'D_geom', |
| 21 | + pShareName: string = 'P_share' |
| 22 | +): ImportAnnotation => { |
| 23 | + const lines = text.split( /\r?\n/ ).filter( l => l.trim() !== '' ); |
| 24 | + const header = lines.shift()!.split( ',' ) |
| 25 | + .map( h => h.trim() ); |
| 26 | + const algorithmIndex = header.indexOf( algorithmName ); |
| 27 | + const textIndex = header.indexOf( textName ); |
| 28 | + const fixIndex = header.indexOf( fixationName ); |
| 29 | + const boxIndex = header.indexOf( boxName ); |
| 30 | + const dGeomIndex = header.indexOf( dGeomName ); |
| 31 | + const pShareIndex = header.indexOf( pShareName ); |
| 32 | + |
| 33 | + if ( fixIndex < 0 ) |
| 34 | + throw new InvalidIndexNameError( 'X coordinate' ); |
| 35 | + else if ( boxIndex < 0 ) |
| 36 | + throw new InvalidIndexNameError( 'Y coordinate' ); |
| 37 | + else if ( algorithmIndex < 0 ) |
| 38 | + throw new InvalidIndexNameError( 'algorithm ID' ); |
| 39 | + else if ( textIndex < 0 ) |
| 40 | + throw new InvalidIndexNameError( 'text ID' ); |
| 41 | + |
| 42 | + const firstCols = lines[0]!.split( ',' ); |
| 43 | + const firstEncounteredTextID = firstCols[ textIndex ]; |
| 44 | + // First index is text, second id is reader |
| 45 | + const annotations: ImportAnnotation = {}; |
| 46 | + |
| 47 | + for ( let i = 0; i < lines.length; i++ ) { |
| 48 | + const cols = lines[i]!.split( ',' ); |
| 49 | + |
| 50 | + if ( firstEncounteredTextID !== cols[ textIndex ] && textFilter === undefined ) { |
| 51 | + throw new MultipleTextIDsWithoutSpecifiedTextIDError(); |
| 52 | + } |
| 53 | + |
| 54 | + const addData = ( cols: string[] ) => { |
| 55 | + const algorithm = cols[ algorithmIndex ]!; |
| 56 | + |
| 57 | + if ( !annotations[ algorithm ] ) { |
| 58 | + annotations[ algorithm ] = { |
| 59 | + 'title': title, |
| 60 | + 'annotations': [] |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + const preAnnotation: PreAnnotationValueDto = { |
| 65 | + 'foreignFixationId': parseInt( cols[ fixIndex ]! ), |
| 66 | + 'foreignCharacterBoxId': parseInt( cols[ boxIndex ]! ), |
64 | 67 | ...dGeomIndex >= 0 && cols[ dGeomIndex ] && { |
65 | 68 | 'dGeom': parseFloat( cols[ dGeomIndex ] ) |
66 | | - }, |
| 69 | + }, |
67 | 70 | ...pShareIndex >= 0 && cols[ pShareIndex ] && { |
68 | 71 | 'pShare': parseFloat( cols[ pShareIndex ] ) |
69 | | - } |
| 72 | + } |
70 | 73 | }; |
71 | | - |
72 | | - annotations[ algorithm ]!.annotations!.push( preAnnotation ); |
73 | | - } else if ( cols[ textIndex ] === textId ) { |
74 | | - const algorithm = cols[ algorithmName ]!; |
75 | | - |
76 | | - if ( !annotations[ algorithm ] ) { |
77 | | - annotations[ algorithm ] = { |
78 | | - 'title': title, |
79 | | - 'annotations': [] |
80 | | - }; |
81 | | - } |
82 | | - |
83 | | - const preAnnotation = { |
84 | | - 'foreignFixationId': parseInt( cols[ fixIndex ]! ), |
85 | | - 'foreignCharacterBoxId': parseInt( cols[ boxIndex ]! ), |
86 | | - ...dGeomIndex >= 0 && cols[ dGeomIndex ] && { |
87 | | - 'dGeom': parseFloat( cols[ dGeomIndex ] ) |
88 | | - }, |
89 | | - ...pShareIndex >= 0 && cols[ pShareIndex ] && { |
90 | | - 'pShare': parseFloat( cols[ pShareIndex ] ) |
91 | | - } |
92 | | - }; |
93 | | - |
94 | | - annotations[ algorithm ]!.annotations!.push( preAnnotation ); |
95 | | - } |
96 | | - } |
97 | | - |
98 | | - return annotations; |
99 | | -}; |
| 74 | + |
| 75 | + annotations[ algorithm ]!.annotations!.push( preAnnotation ); |
| 76 | + }; |
| 77 | + |
| 78 | + if ( textFilter === undefined ) { |
| 79 | + addData( cols ); |
| 80 | + } else if ( typeof textFilter === 'string' && cols[ textIndex ] === textFilter ) { |
| 81 | + addData( cols ); |
| 82 | + } else if ( typeof textFilter === 'object' ) { |
| 83 | + const text = Number( cols[ textIndex ] ); |
| 84 | + |
| 85 | + if ( textFilter.length === 2 ) { |
| 86 | + if ( text < textFilter[1] && text > textFilter[0] ) |
| 87 | + addData( cols ); |
| 88 | + } else if ( textFilter.includes( text ) ) { |
| 89 | + addData( cols ); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return annotations; |
| 95 | +}; |
0 commit comments