Spaces:
Runtime error
Runtime error
Marcelo commited on
Commit ·
b0c2bc6
1
Parent(s): 2c194ba
add parse function
Browse files- src/hardcoded_data.py +32 -1
src/hardcoded_data.py
CHANGED
|
@@ -45,7 +45,6 @@ articles = [
|
|
| 45 |
"category": "planetary science",
|
| 46 |
"articles": planetary_science_articles
|
| 47 |
},
|
| 48 |
-
|
| 49 |
]
|
| 50 |
|
| 51 |
[
|
|
@@ -56,3 +55,35 @@ articles = [
|
|
| 56 |
"tldr",
|
| 57 |
},
|
| 58 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
"category": "planetary science",
|
| 46 |
"articles": planetary_science_articles
|
| 47 |
},
|
|
|
|
| 48 |
]
|
| 49 |
|
| 50 |
[
|
|
|
|
| 55 |
"tldr",
|
| 56 |
},
|
| 57 |
]
|
| 58 |
+
|
| 59 |
+
# [category1[articles[article1['title1', 'abstract1'], article2['title2', 'abstract2']]], category2[...], ...]
|
| 60 |
+
|
| 61 |
+
def parse_list_of_lists(input_list, category_names):
|
| 62 |
+
articles_list = []
|
| 63 |
+
|
| 64 |
+
for idx, item in enumerate(input_list):
|
| 65 |
+
category_name = category_names[idx]
|
| 66 |
+
category_articles = []
|
| 67 |
+
|
| 68 |
+
for sub_item in item:
|
| 69 |
+
if isinstance(sub_item, list) and len(sub_item) == 2:
|
| 70 |
+
article_title, article_abstract = sub_item
|
| 71 |
+
category_articles.append({"title": article_title, "abstract": article_abstract})
|
| 72 |
+
else:
|
| 73 |
+
raise ValueError("Each sub-item should be a list containing [title, abstract].")
|
| 74 |
+
|
| 75 |
+
articles_list.append({"category": category_name, "articles": category_articles})
|
| 76 |
+
|
| 77 |
+
return articles_list
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
input_list = [
|
| 81 |
+
[["article1 title", "article1 abstract"], ["article2 title", "article2 abstract"]],
|
| 82 |
+
[["article3 title", "article3 abstract"], ["article4 title", "article4 abstract"]]
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
category_names = ["machine learning", "planetary science"]
|
| 86 |
+
|
| 87 |
+
output_list_of_dicts = parse_list_of_lists(input_list, category_names)
|
| 88 |
+
print(output_list_of_dicts)
|
| 89 |
+
|