Spaces:
Sleeping
Sleeping
Update StockSentimentAnalyser.py
Browse files- StockSentimentAnalyser.py +34 -34
StockSentimentAnalyser.py
CHANGED
|
@@ -457,41 +457,41 @@ class StockSentimentAnalyzer:
|
|
| 457 |
# If company name not provided, try to extract from symbol
|
| 458 |
self.symbol = symbol
|
| 459 |
# If company name not provided, try to extract from symbol
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
# Get company info from yfinance
|
| 469 |
-
ticker = yf.Ticker(symbol_with_suffix)
|
| 470 |
-
info = ticker.info
|
| 471 |
-
|
| 472 |
-
# Extract company name with multiple fallbacks
|
| 473 |
-
company_name = (
|
| 474 |
-
info.get('longName') or
|
| 475 |
-
info.get('shortName') or
|
| 476 |
-
info.get('name') or
|
| 477 |
-
symbol # Final fallback to symbol
|
| 478 |
-
)
|
| 479 |
-
|
| 480 |
-
# Validate the extracted name
|
| 481 |
-
if company_name:
|
| 482 |
-
# Remove special characters and check if meaningful
|
| 483 |
-
cleaned_name = ''.join(c for c in company_name if c.isalnum() or c in (' ', '-', '&'))
|
| 484 |
-
if (len(cleaned_name.strip()) < 2 or # Too short
|
| 485 |
-
cleaned_name.strip() == symbol or # Same as symbol
|
| 486 |
-
any(x in cleaned_name for x in ['-', ' - ']) or # Contains dashes (likely placeholder)
|
| 487 |
-
cleaned_name.isnumeric()): # Just numbers
|
| 488 |
-
company_name = symbol # Fallback to symbol if name is invalid
|
| 489 |
-
else:
|
| 490 |
-
company_name = symbol
|
| 491 |
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
|
| 496 |
# Ensure we have at least the symbol as name
|
| 497 |
company_name = company_name or symbol
|
|
|
|
| 457 |
# If company name not provided, try to extract from symbol
|
| 458 |
self.symbol = symbol
|
| 459 |
# If company name not provided, try to extract from symbol
|
| 460 |
+
if not company_name:
|
| 461 |
+
try:
|
| 462 |
+
# Add .NS for NSE stocks if not present
|
| 463 |
+
if not symbol.endswith('.NS') and not symbol.endswith('.BO'):
|
| 464 |
+
symbol_with_suffix = symbol + '.NS'
|
| 465 |
+
else:
|
| 466 |
+
symbol_with_suffix = symbol
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
|
| 468 |
+
# Get company info from yfinance
|
| 469 |
+
ticker = yf.Ticker(symbol_with_suffix)
|
| 470 |
+
info = ticker.info
|
| 471 |
+
|
| 472 |
+
# Extract company name with multiple fallbacks
|
| 473 |
+
company_name = (
|
| 474 |
+
info.get('longName') or
|
| 475 |
+
info.get('shortName') or
|
| 476 |
+
info.get('name') or
|
| 477 |
+
symbol # Final fallback to symbol
|
| 478 |
+
)
|
| 479 |
+
|
| 480 |
+
# Validate the extracted name
|
| 481 |
+
if company_name:
|
| 482 |
+
# Remove special characters and check if meaningful
|
| 483 |
+
cleaned_name = ''.join(c for c in company_name if c.isalnum() or c in (' ', '-', '&'))
|
| 484 |
+
if (len(cleaned_name.strip()) < 2 or # Too short
|
| 485 |
+
cleaned_name.strip() == symbol or # Same as symbol
|
| 486 |
+
any(x in cleaned_name for x in ['-', ' - ']) or # Contains dashes (likely placeholder)
|
| 487 |
+
cleaned_name.isnumeric()): # Just numbers
|
| 488 |
+
company_name = symbol # Fallback to symbol if name is invalid
|
| 489 |
+
else:
|
| 490 |
+
company_name = symbol
|
| 491 |
+
|
| 492 |
+
except Exception as e:
|
| 493 |
+
print(f"⚠️ Could not fetch company name for {symbol}: {str(e)}")
|
| 494 |
+
company_name = symbol
|
| 495 |
|
| 496 |
# Ensure we have at least the symbol as name
|
| 497 |
company_name = company_name or symbol
|