1
1
from typing import List , Union
2
2
3
3
from pydantic .main import BaseModel
4
+ from pydantic .schema import schema
4
5
5
6
from ...annotation_types .annotation import ClassificationAnnotation
6
7
from ...annotation_types .classification import Checklist , ClassificationAnswer , Radio , Text , Dropdown
@@ -15,10 +16,8 @@ class LBV1ClassificationAnswer(LBV1Feature):
15
16
class LBV1Radio (LBV1Feature ):
16
17
answer : LBV1ClassificationAnswer
17
18
18
- def to_common (self ):
19
- return Radio (answer = ClassificationAnswer (
20
- feature_schema_id = self .answer .schema_id ,
21
- name = self .answer .title ,
19
+ def to_common (self ) -> Radio :
20
+ return Radio (answer = ClassificationAnswer (feature_schema_id = self .answer .schema_id , name = self .answer .title ,
22
21
extra = {
23
22
'feature_id' : self .answer .feature_id ,
24
23
'value' : self .answer .value
@@ -27,7 +26,8 @@ def to_common(self):
27
26
@classmethod
28
27
def from_common (cls , radio : Radio , feature_schema_id : Cuid ,
29
28
** extra ) -> "LBV1Radio" :
30
- return cls (schema_id = feature_schema_id ,
29
+ return cls (
30
+ schema_id = feature_schema_id ,
31
31
answer = LBV1ClassificationAnswer (
32
32
schema_id = radio .answer .feature_schema_id ,
33
33
title = radio .answer .name ,
@@ -39,8 +39,9 @@ def from_common(cls, radio: Radio, feature_schema_id: Cuid,
39
39
class LBV1Checklist (LBV1Feature ):
40
40
answers : List [LBV1ClassificationAnswer ]
41
41
42
- def to_common (self ):
43
- return Checklist (answer = [
42
+ def to_common (self ) -> Checklist :
43
+ return Checklist (
44
+ answer = [
44
45
ClassificationAnswer (feature_schema_id = answer .schema_id ,
45
46
name = answer .title ,
46
47
extra = {
@@ -64,6 +65,34 @@ def from_common(cls, checklist: Checklist, feature_schema_id: Cuid,
64
65
** extra )
65
66
66
67
68
+ class LBV1Dropdown (LBV1Feature ):
69
+ answer : List [LBV1ClassificationAnswer ]
70
+ def to_common (self ) -> Dropdown :
71
+ return Dropdown (
72
+ answer = [
73
+ ClassificationAnswer (feature_schema_id = answer .schema_id ,
74
+ name = answer .title ,
75
+ extra = {
76
+ 'feature_id' : answer .feature_id ,
77
+ 'value' : answer .value
78
+ }) for answer in self .answer
79
+ ])
80
+
81
+ @classmethod
82
+ def from_common (cls , dropdown : Dropdown , feature_schema_id : Cuid ,
83
+ ** extra ) -> "LBV1Dropdown" :
84
+ return cls (schema_id = feature_schema_id ,
85
+ answers = [
86
+ LBV1ClassificationAnswer (
87
+ schema_id = answer .feature_schema_id ,
88
+ title = answer .name ,
89
+ value = answer .extra .get ('value' ),
90
+ feature_id = answer .extra .get ('feature_id' ))
91
+ for answer in dropdown .answer
92
+ ],
93
+ ** extra )
94
+
95
+
67
96
class LBV1Text (LBV1Feature ):
68
97
answer : str
69
98
@@ -77,7 +106,7 @@ def from_common(cls, text: Text, feature_schema_id: Cuid,
77
106
78
107
79
108
class LBV1Classifications (BaseModel ):
80
- classifications : List [Union [LBV1Radio , LBV1Checklist , LBV1Text ]] = []
109
+ classifications : List [Union [LBV1Text , LBV1Radio , LBV1Dropdown , LBV1Checklist ]] = []
81
110
82
111
def to_common (self ) -> List [ClassificationAnnotation ]:
83
112
classifications = [
@@ -112,10 +141,10 @@ def from_common(
112
141
@staticmethod
113
142
def lookup_classification (
114
143
annotation : ClassificationAnnotation
115
- ) -> Union [LBV1Text , LBV1Checklist , LBV1Radio ]:
144
+ ) -> Union [LBV1Text , LBV1Checklist , LBV1Radio , LBV1Checklist ]:
116
145
return {
117
146
Text : LBV1Text ,
118
- Dropdown : LBV1Checklist ,
147
+ Dropdown : LBV1Dropdown ,
119
148
Checklist : LBV1Checklist ,
120
149
Radio : LBV1Radio
121
150
}.get (type (annotation .value ))
0 commit comments