1+ import re
12import json
2- from graphrag_sdk .fixtures .regex import *
33import logging
4- import re
4+ from graphrag_sdk . fixtures . regex import *
55
66logger = logging .getLogger (__name__ )
77
@@ -15,6 +15,23 @@ class AttributeType:
1515 NUMBER = "number"
1616 BOOLEAN = "boolean"
1717 LIST = "list"
18+ POINT = "point"
19+ MAP = "map"
20+ VECTOR = "vectorf32"
21+
22+
23+ # Synonyms for attribute types
24+ _SYNONYMS = {
25+ "string" : STRING ,
26+ "integer" : NUMBER ,
27+ "float" : NUMBER ,
28+ "number" : NUMBER ,
29+ "boolean" : BOOLEAN ,
30+ "list" : LIST ,
31+ "point" : POINT ,
32+ "map" : MAP ,
33+ "vectorf32" : VECTOR ,
34+ }
1835
1936 @staticmethod
2037 def from_string (txt : str ):
@@ -25,21 +42,19 @@ def from_string(txt: str):
2542 txt (str): The string representation of the attribute type.
2643
2744 Returns:
28- AttributeType : The corresponding AttributeType value.
45+ str : The corresponding AttributeType value.
2946
3047 Raises:
31- Exception : If the provided attribute type is invalid.
48+ ValueError : If the provided attribute type is invalid.
3249 """
33- if txt .lower () == AttributeType .STRING :
34- return AttributeType .STRING
35- if txt .lower () == AttributeType .NUMBER :
36- return AttributeType .NUMBER
37- if txt .lower () == AttributeType .BOOLEAN :
38- return AttributeType .BOOLEAN
39- if txt .lower () == AttributeType .LIST :
40- return AttributeType .LIST
41- raise Exception (f"Invalid attribute type: { txt } " )
42-
50+ # Graph representation of the attribute type
51+ normalized_txt = txt .lower ()
52+
53+ # Find the matching attribute type
54+ if normalized_txt in AttributeType ._SYNONYMS :
55+ return AttributeType ._SYNONYMS [normalized_txt ]
56+
57+ raise ValueError (f"Invalid attribute type: { txt } " )
4358
4459class Attribute :
4560 """ Represents an attribute of an entity or relation in the ontology.
@@ -57,15 +72,15 @@ class Attribute:
5772 """
5873
5974 def __init__ (
60- self , name : str , attr_type : AttributeType , unique : bool , required : bool = False
75+ self , name : str , attr_type : AttributeType , unique : bool = False , required : bool = False
6176 ):
6277 """
6378 Initialize a new Attribute object.
6479
6580 Args:
6681 name (str): The name of the attribute.
6782 attr_type (AttributeType): The type of the attribute.
68- unique (bool): Indicates whether the attribute should be unique.
83+ unique (bool, optional ): Indicates whether the attribute should be unique. Defaults to False .
6984 required (bool, optional): Indicates whether the attribute is required. Defaults to False.
7085 """
7186 self .name = re .sub (r"([^a-zA-Z0-9_])" , "_" , name )
@@ -120,14 +135,6 @@ def from_string(txt: str):
120135 unique = "!" in txt
121136 required = "*" in txt
122137
123- if attr_type not in [
124- AttributeType .STRING ,
125- AttributeType .NUMBER ,
126- AttributeType .BOOLEAN ,
127- AttributeType .LIST ,
128- ]:
129- raise Exception (f"Invalid attribute type: { attr_type } " )
130-
131138 return Attribute (name , AttributeType .from_string (attr_type ), unique , required )
132139
133140 def to_json (self ):
@@ -142,13 +149,15 @@ def to_json(self):
142149 - "unique": A boolean indicating whether the attribute is unique.
143150 - "required": A boolean indicating whether the attribute is required.
144151 """
145- return {
152+ json_data = {
146153 "name" : self .name ,
147154 "type" : self .type ,
148155 "unique" : self .unique ,
149156 "required" : self .required ,
150157 }
151158
159+ return json_data
160+
152161 def __str__ (self ) -> str :
153162 """
154163 Returns a string representation of the Attribute object.
0 commit comments