1010import re
1111import enchant
1212from sentibank import archive
13+ import subprocess
14+ import sys
1315
1416load = archive .load ()
1517
@@ -26,10 +28,37 @@ def __init__(self):
2628 """
2729 Initializes the Analysis class by loading the Spacy NLP pipeline with emoji detection.
2830 """
29- self .spacy_nlp = spacy .load (
30- "en_core_web_sm" ,
31- exclude = ["parser" , "senter" , "attribute_ruler" , "lemmatizer" , "ner" ],
32- )
31+ try :
32+ self .spacy_nlp = spacy .load (
33+ "en_core_web_sm" ,
34+ exclude = ["parser" , "senter" , "attribute_ruler" , "lemmatizer" , "ner" ],
35+ )
36+ except OSError :
37+ # Model not found, attempt to download it
38+ import subprocess
39+ import sys
40+
41+ print ("SpaCy model 'en_core_web_sm' not found. Downloading..." )
42+ try :
43+ subprocess .check_call (
44+ [sys .executable , "-m" , "spacy" , "download" , "en_core_web_sm" ],
45+ stdout = subprocess .DEVNULL , # Suppress output for cleaner experience
46+ stderr = subprocess .STDOUT
47+ )
48+ # Try loading again after download
49+ self .spacy_nlp = spacy .load (
50+ "en_core_web_sm" ,
51+ exclude = ["parser" , "senter" , "attribute_ruler" , "lemmatizer" , "ner" ],
52+ )
53+ print ("Successfully downloaded and loaded en_core_web_sm" )
54+ except subprocess .CalledProcessError :
55+ raise RuntimeError (
56+ "Failed to download spaCy model automatically.\n "
57+ "Please install it manually by running:\n "
58+ " python -m spacy download en_core_web_sm"
59+ )
60+
61+ # Add emoji detection pipeline (this stays the same)
3362 # self.spacy_nlp.add_pipe("sentencizer")
3463 self .spacy_nlp .add_pipe ("emoji" , first = True )
3564
0 commit comments