|
1 | | -from fastapi import FastAPI, Header, HTTPException, Query, Request |
| 1 | +from fastapi import FastAPI, HTTPException, Query, Request |
2 | 2 | from fastapi.middleware.cors import CORSMiddleware |
3 | 3 | from fastapi import BackgroundTasks |
4 | 4 | import traceback |
5 | 5 |
|
6 | | -from src.WikidataTextifier import WikidataEntity |
| 6 | +from src.Normalizer import TTLNormalizer, JSONNormalizer |
7 | 7 | from src.WikidataLabel import WikidataLabel |
8 | 8 | from src import utils |
9 | 9 |
|
@@ -82,51 +82,78 @@ async def get_textified_wd( |
82 | 82 | response = "ID is missing" |
83 | 83 | return HTTPException(status_code=422, detail=response) |
84 | 84 |
|
85 | | - filter_pids = None |
| 85 | + filter_pids = [] |
86 | 86 | if pid: |
87 | 87 | filter_pids = [p.strip() for p in pid.split(',')] |
88 | 88 |
|
89 | 89 | qids = [q.strip() for q in id.split(',')] |
90 | | - entity_dict = utils.get_wikidata_entities_by_ids(qids) |
91 | 90 |
|
92 | | - if not entity_dict: |
93 | | - response = "ID not found" |
94 | | - return HTTPException(status_code=404, detail=response) |
| 91 | + entities = {} |
| 92 | + if len(qids) == 1: |
| 93 | + # When one QID is requested, TTL is used |
| 94 | + entity_data = utils.get_wikidata_ttl_by_id(qids[0], lang=lang) |
| 95 | + if not entity_data: |
| 96 | + response = "ID not found" |
| 97 | + return HTTPException(status_code=404, detail=response) |
95 | 98 |
|
96 | | - return_data = {} |
97 | | - for id in qids: |
98 | | - if id in entity_dict: |
99 | | - entity = WikidataEntity.from_wd( |
100 | | - entity_dict[id], |
101 | | - id=id, |
102 | | - lang=lang, |
| 99 | + entity_data = TTLNormalizer( |
| 100 | + entity_id=qids[0], |
| 101 | + ttl_text=entity_data, |
| 102 | + lang=lang, |
| 103 | + fallback_lang=fallback_lang, |
| 104 | + debug=False, |
| 105 | + ) |
| 106 | + |
| 107 | + entities = { |
| 108 | + qids[0]: entity_data.normalize( |
103 | 109 | external_ids=external_ids, |
104 | 110 | all_ranks=all_ranks, |
105 | 111 | references=references, |
106 | | - filter_pids=filter_pids, |
107 | | - fallback_lang=fallback_lang |
| 112 | + filter_pids=filter_pids |
108 | 113 | ) |
| 114 | + } |
| 115 | + else: |
| 116 | + # JSON is used with Action API for bulk retrieval |
| 117 | + entity_data = utils.get_wikidata_json_by_ids(qids) |
| 118 | + if not entity_data: |
| 119 | + response = "IDs not found" |
| 120 | + return HTTPException(status_code=404, detail=response) |
109 | 121 |
|
110 | | - if not entity: |
111 | | - return_data[id] = None |
112 | | - continue |
113 | | - |
114 | | - if format == 'text': |
115 | | - results = str(entity) |
116 | | - elif format == 'triplet': |
117 | | - results = entity.to_triplet() |
118 | | - else: |
119 | | - results = entity.to_json() |
| 122 | + entity_data = { |
| 123 | + qid: JSONNormalizer( |
| 124 | + entity_id=qid, |
| 125 | + entity_json=entity_data[qid], |
| 126 | + lang=lang, |
| 127 | + fallback_lang=fallback_lang, |
| 128 | + debug=False, |
| 129 | + ) if entity_data.get(qid) else None |
| 130 | + for qid in qids |
| 131 | + } |
| 132 | + |
| 133 | + entities = { |
| 134 | + qid: entity.normalize( |
| 135 | + external_ids=external_ids, |
| 136 | + all_ranks=all_ranks, |
| 137 | + references=references, |
| 138 | + filter_pids=filter_pids |
| 139 | + ) if entity else None |
| 140 | + for qid, entity in entity_data.items() |
| 141 | + } |
120 | 142 |
|
121 | | - return_data[id] = results |
| 143 | + return_data = {} |
| 144 | + for qid, entity in entities.items(): |
| 145 | + if not entity: |
| 146 | + return_data[qid] = None |
| 147 | + continue |
| 148 | + |
| 149 | + if format == 'text': |
| 150 | + results = str(entity) |
| 151 | + elif format == 'triplet': |
| 152 | + results = entity.to_triplet() |
122 | 153 | else: |
123 | | - return_data[id] = None |
| 154 | + results = entity.to_json() |
124 | 155 |
|
125 | | - if len(qids) == 1: |
126 | | - return_data = return_data[qids[0]] |
127 | | - if not return_data: |
128 | | - response = "Item not found" |
129 | | - return HTTPException(status_code=404, detail=response) |
| 156 | + return_data[qid] = results |
130 | 157 |
|
131 | 158 | background_tasks.add_task(WikidataLabel.delete_old_labels) |
132 | 159 | return return_data |
|
0 commit comments