bigdata-smart-batching 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bigdata_smart_batching/__init__.py +28 -0
- bigdata_smart_batching/output_converter.py +96 -0
- bigdata_smart_batching/search_function.py +1071 -0
- bigdata_smart_batching/smart_batching.py +1593 -0
- bigdata_smart_batching/smart_batching_config.py +39 -0
- bigdata_smart_batching-0.1.0.dist-info/METADATA +231 -0
- bigdata_smart_batching-0.1.0.dist-info/RECORD +8 -0
- bigdata_smart_batching-0.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Smart Batching module for BigData search operations.
|
|
3
|
+
|
|
4
|
+
This module provides:
|
|
5
|
+
- SmartBatchingPlanner: Intelligent search planning
|
|
6
|
+
- plan_search, execute_search: High-level search functions
|
|
7
|
+
- Helper functions for result processing
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from .output_converter import convert_to_dataframe
|
|
11
|
+
from .search_function import (
|
|
12
|
+
deduplicate_documents,
|
|
13
|
+
execute_search,
|
|
14
|
+
load_plan,
|
|
15
|
+
load_universe_from_csv,
|
|
16
|
+
plan_search,
|
|
17
|
+
save_plan,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"plan_search",
|
|
22
|
+
"execute_search",
|
|
23
|
+
"deduplicate_documents",
|
|
24
|
+
"save_plan",
|
|
25
|
+
"load_plan",
|
|
26
|
+
"load_universe_from_csv",
|
|
27
|
+
"convert_to_dataframe",
|
|
28
|
+
]
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def convert_to_dataframe(raw_results: list[dict]) -> pd.DataFrame:
|
|
5
|
+
"""
|
|
6
|
+
Convert Smart_batching output to a DataFrame exploded by chunk.
|
|
7
|
+
|
|
8
|
+
Each document is expanded into multiple rows, one per chunk.
|
|
9
|
+
|
|
10
|
+
Args:
|
|
11
|
+
raw_results: List of raw documents from Smart_batching execute_search()
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
DataFrame with one row per chunk, containing:
|
|
15
|
+
- Document columns: date, doc_id, headline, source_id, source_name, source_rank
|
|
16
|
+
- Chunk columns: chunk_index, chunk_text, chunk_relevance, chunk_sentiment
|
|
17
|
+
- Entity columns: entity_ids
|
|
18
|
+
- Final columns: url, reporting_entities
|
|
19
|
+
"""
|
|
20
|
+
rows = []
|
|
21
|
+
|
|
22
|
+
for raw_doc in raw_results:
|
|
23
|
+
# Extract document info
|
|
24
|
+
doc_id = raw_doc.get("id", "")
|
|
25
|
+
headline = raw_doc.get("headline", "")
|
|
26
|
+
timestamp = raw_doc.get("timestamp", "")
|
|
27
|
+
url = raw_doc.get("url", "")
|
|
28
|
+
reporting_entities = raw_doc.get("reporting_entities", [])
|
|
29
|
+
|
|
30
|
+
# Extract source info
|
|
31
|
+
raw_source = raw_doc.get("source", {})
|
|
32
|
+
source_id = raw_source.get("id", "")
|
|
33
|
+
source_name = raw_source.get("name", "")
|
|
34
|
+
source_rank = raw_source.get("rank", "")
|
|
35
|
+
|
|
36
|
+
# Explode chunks
|
|
37
|
+
chunks = raw_doc.get("chunks", [])
|
|
38
|
+
|
|
39
|
+
if not chunks:
|
|
40
|
+
# If no chunks, still create a row with empty values
|
|
41
|
+
rows.append(
|
|
42
|
+
{
|
|
43
|
+
# Document
|
|
44
|
+
"date": timestamp,
|
|
45
|
+
"doc_id": doc_id,
|
|
46
|
+
"headline": headline,
|
|
47
|
+
"source_id": source_id,
|
|
48
|
+
"source_name": source_name,
|
|
49
|
+
"source_rank": source_rank,
|
|
50
|
+
# Chunk (empty)
|
|
51
|
+
"chunk_index": None,
|
|
52
|
+
"chunk_text": "",
|
|
53
|
+
"chunk_relevance": None,
|
|
54
|
+
"chunk_sentiment": None,
|
|
55
|
+
# Entity
|
|
56
|
+
"entity_ids": [],
|
|
57
|
+
# Final
|
|
58
|
+
"url": url,
|
|
59
|
+
"reporting_entities": reporting_entities,
|
|
60
|
+
}
|
|
61
|
+
)
|
|
62
|
+
else:
|
|
63
|
+
for chunk in chunks:
|
|
64
|
+
# Entity IDs (added by execute_search)
|
|
65
|
+
entity_ids = chunk.get("entity_ids", [])
|
|
66
|
+
|
|
67
|
+
rows.append(
|
|
68
|
+
{
|
|
69
|
+
# Document
|
|
70
|
+
"date": timestamp,
|
|
71
|
+
"doc_id": doc_id,
|
|
72
|
+
"headline": headline,
|
|
73
|
+
"source_id": source_id,
|
|
74
|
+
"source_name": source_name,
|
|
75
|
+
"source_rank": source_rank,
|
|
76
|
+
# Chunk
|
|
77
|
+
"chunk_index": chunk.get("cnum"),
|
|
78
|
+
"chunk_text": chunk.get("text", ""),
|
|
79
|
+
"chunk_relevance": chunk.get("relevance"),
|
|
80
|
+
"chunk_sentiment": chunk.get("sentiment"),
|
|
81
|
+
# Entity
|
|
82
|
+
"entity_ids": entity_ids,
|
|
83
|
+
# Final
|
|
84
|
+
"url": url,
|
|
85
|
+
"reporting_entities": reporting_entities,
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# Create DataFrame
|
|
90
|
+
df = pd.DataFrame(rows)
|
|
91
|
+
|
|
92
|
+
# Convert date to Date (date only, no time)
|
|
93
|
+
if "date" in df.columns and not df.empty:
|
|
94
|
+
df["date"] = pd.to_datetime(df["date"], errors="coerce", utc=True).dt.date
|
|
95
|
+
|
|
96
|
+
return df
|