Skip to content

Commit 8618b87

Browse files
added escape_backlashes function with test (#858)
1 parent c07f2a8 commit 8618b87

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

paperqa/core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ def llm_parse_json(text: str) -> dict:
2020
def escape_newlines(match: re.Match) -> str:
2121
return match.group(0).replace("\n", "\\n")
2222

23+
def escape_double_backslashes(match: re.Match) -> str:
24+
return match.group(0).replace("\\", "\\\\")
25+
2326
# Match anything between double quotes
2427
# including escaped quotes and other escaped characters.
2528
# https://regex101.com/r/VFcDmB/1
2629
pattern = r'"(?:[^"\\]|\\.)*"'
30+
ptext = re.sub(pattern, escape_double_backslashes, ptext)
2731
ptext = re.sub(pattern, escape_newlines, ptext)
2832
try:
2933
return json.loads(ptext)

tests/test_paperqa.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,35 +376,44 @@ def test_extract_score() -> None:
376376

377377

378378
@pytest.mark.parametrize(
379-
"example",
379+
("example", "expected"),
380380
[
381-
"""Sure here is the json you asked for!
381+
(
382+
"""Sure here is the json you asked for!
382383
383384
{
384385
"example": "json"
385386
}
386387
387388
Did you like it?""",
388-
'{"example": "json"}',
389-
"""
389+
{"example": "json"},
390+
),
391+
(
392+
"""
390393
```json
391394
{
392395
"example": "json"
393396
}
394397
```
395398
396399
I have written the json you asked for.""",
397-
"""
400+
{"example": "json"},
401+
),
402+
(
403+
"""
398404
399405
{
400406
"example": "json"
401407
}
402408
403409
""",
410+
{"example": "json"},
411+
),
412+
('{"example": "\\json"}', {"example": "\\json"}),
404413
],
405414
)
406-
def test_llm_parse_json(example: str) -> None:
407-
assert llm_parse_json(example) == {"example": "json"}
415+
def test_llm_parse_json(example: str, expected: dict) -> None:
416+
assert llm_parse_json(example) == expected
408417

409418

410419
def test_llm_parse_json_newlines() -> None:

0 commit comments

Comments
 (0)