|
18 | 18 |
|
19 | 19 |
|
20 | 20 | def fix_gql_syntax(query: str) -> str:
|
21 |
| - """Fixes the syntax of a GQL query. |
22 |
| -
|
23 |
| - Example 1: |
24 |
| -
|
25 |
| - Input: |
26 |
| - MATCH (p:paper {id: 0})-[c:cites*8]->(p2:paper) |
27 |
| - Output: |
28 |
| - MATCH (p:paper {id: 0})-[c:cites]->{8}(p2:paper) |
29 |
| - Example 2: |
30 |
| - Input: |
31 |
| - MATCH (p:paper {id: 0})-[c:cites*1..8]->(p2:paper) |
32 |
| - Output: |
33 |
| - MATCH (p:paper {id: 0})-[c:cites]->{1:8}(p2:paper) |
34 |
| -
|
35 |
| - Args: |
36 |
| - query: The input GQL query. |
37 |
| -
|
38 |
| - Returns: |
39 |
| - Possibly modified GQL query. |
40 |
| - """ |
41 |
| - |
42 |
| - query = re.sub( |
43 |
| - r"-\[(.*?):(\w+)\*(\d+)\.\.(\d+)\]->", r"-[\1:\2]->{\3,\4}", query |
44 |
| - ) |
45 |
| - query = re.sub(r"-\[(.*?):(\w+)\*(\d+)\]->", r"-[\1:\2]->{\3}", query) |
46 |
| - query = re.sub( |
47 |
| - r"<-\[(.*?):(\w+)\*(\d+)\.\.(\d+)\]-", r"<-[\1:\2]-{\3,\4}", query |
48 |
| - ) |
49 |
| - query = re.sub(r"<-\[(.*?):(\w+)\*(\d+)\]-", r"<-[\1:\2]-{\3}", query) |
50 |
| - query = re.sub( |
51 |
| - r"-\[(.*?):(\w+)\*(\d+)\.\.(\d+)\]-", r"-[\1:\2]-{\3,\4}", query |
52 |
| - ) |
53 |
| - query = re.sub(r"-\[(.*?):(\w+)\*(\d+)\]-", r"-[\1:\2]-{\3}", query) |
54 |
| - # print("Fixed query: ", query) |
55 |
| - return query |
| 21 | + """Fixes the syntax of a GQL query. |
| 22 | +
|
| 23 | + Example 1: |
| 24 | +
|
| 25 | + Input: |
| 26 | + MATCH (p:paper {id: 0})-[c:cites*8]->(p2:paper) |
| 27 | + Output: |
| 28 | + MATCH (p:paper {id: 0})-[c:cites]->{8}(p2:paper) |
| 29 | + Example 2: |
| 30 | + Input: |
| 31 | + MATCH (p:paper {id: 0})-[c:cites*1..8]->(p2:paper) |
| 32 | + Output: |
| 33 | + MATCH (p:paper {id: 0})-[c:cites]->{1:8}(p2:paper) |
| 34 | +
|
| 35 | + Args: |
| 36 | + query: The input GQL query. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + Possibly modified GQL query. |
| 40 | + """ |
| 41 | + |
| 42 | + query = re.sub(r"-\[(.*?):(\w+)\*(\d+)\.\.(\d+)\]->", r"-[\1:\2]->{\3,\4}", query) |
| 43 | + query = re.sub(r"-\[(.*?):(\w+)\*(\d+)\]->", r"-[\1:\2]->{\3}", query) |
| 44 | + query = re.sub(r"<-\[(.*?):(\w+)\*(\d+)\.\.(\d+)\]-", r"<-[\1:\2]-{\3,\4}", query) |
| 45 | + query = re.sub(r"<-\[(.*?):(\w+)\*(\d+)\]-", r"<-[\1:\2]-{\3}", query) |
| 46 | + query = re.sub(r"-\[(.*?):(\w+)\*(\d+)\.\.(\d+)\]-", r"-[\1:\2]-{\3,\4}", query) |
| 47 | + query = re.sub(r"-\[(.*?):(\w+)\*(\d+)\]-", r"-[\1:\2]-{\3}", query) |
| 48 | + # print("Fixed query: ", query) |
| 49 | + return query |
56 | 50 |
|
57 | 51 |
|
58 | 52 | def extract_gql(text: str) -> str:
|
59 |
| - """Extract GQL query from a text. |
60 |
| -
|
61 |
| - Args: |
62 |
| - text: Text to extract GQL query from. |
63 |
| -
|
64 |
| - Returns: |
65 |
| - GQL query extracted from the text. |
66 |
| - """ |
67 |
| - pattern = r"```(.*?)```" |
68 |
| - matches = re.findall(pattern, text, re.DOTALL) |
69 |
| - query = matches[0] if matches else text |
70 |
| - return fix_gql_syntax(query) |
| 53 | + """Extract GQL query from a text. |
| 54 | +
|
| 55 | + Args: |
| 56 | + text: Text to extract GQL query from. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + GQL query extracted from the text. |
| 60 | + """ |
| 61 | + pattern = r"```(.*?)```" |
| 62 | + matches = re.findall(pattern, text, re.DOTALL) |
| 63 | + query = matches[0] if matches else text |
| 64 | + return fix_gql_syntax(query) |
0 commit comments