Pysentiment调用ps.LM()报错无法连接LM词典 求助
Hey there, let's troubleshoot this LM dictionary problem step by step—since you're stuck with Python 2.7 and can't use pip for installs, we'll focus on manual fixes that fit your restricted setup.
1. Verify the LM Dictionary Files Exist
First, check if the Loughran-McDonald (LM) dictionary files are present in your manually downloaded pysentiment folder:
- Navigate to the
pysentimentdirectory you added to Python's path. - Look for a subfolder named
lm—it should contain at leastpositive.txtandnegative.txt(the core LM word lists). - If the
lmfolder is missing, create it. If the text files are missing, populate them:- Get the official Loughran-McDonald positive and negative word lists, then copy each word list into its respective text file, one word per line.
2. Double-Check Your Python Path Setup
It's easy to misconfigure the path when adding libraries manually. Let's confirm it's set correctly:
- Add this snippet at the top of your script to verify your path:
import sys # Print current Python path to check print(sys.path) - Make sure the parent folder of your
pysentimentdirectory is in the path list. For example, ifpysentimentlives inC:\school_libs\pysentiment, you needC:\school_libsinsys.path, not thepysentimentfolder itself. - If it's missing, add it explicitly:
sys.path.append("C:/school_libs") # Use forward slashes or double backslashes on Windows
3. Fix the LM Class's File Path
Sometimes the hardcoded path in the LM class doesn't match your manual setup. Here's how to adjust it:
- Open the
lm.pyfile inside yourpysentimentfolder (if it's in a different location, check the__init__.pyfile for where the LM class is defined). - Look for a line like this:
PATH = 'lm' - Replace the relative path with the absolute path to your
lmfolder. For example:PATH = 'C:/school_libs/pysentiment/lm'
This ensures the script can find the dictionary files regardless of where you run your code from.
4. Test with a Minimal Example
Once you've made these changes, run a simple test to confirm it works:
import sys sys.path.append("C:/school_libs") # Update this to your parent folder path import pysentiment as ps # Initialize LM dictionary lm = ps.LM() # Test with sample text test_text = "The company reported strong earnings and positive growth forecasts." scores = lm.get_score(test_text) print("Positive score:", scores['positive']) print("Negative score:", scores['negative'])
If you still get an error, note the exact error message (like file not found, import error, etc.)—that will help narrow down the remaining issue.
内容的提问来源于stack exchange,提问作者cek11lru

