Home > OS >  How can I resolve ImportError: cannot import name 'SpacyDocReader' from 'pke.readers&
How can I resolve ImportError: cannot import name 'SpacyDocReader' from 'pke.readers&

Time:12-27

This is my first time posting on Stackoverflow. I'm having this Import Error when trying to execute this code snippet in my jupyter notebook:

import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
import string
import pke
import traceback

def get_nouns_multipartite(content):
    out=[]
    try:
        extractor = pke.unsupervised.MultipartiteRank()
        extractor.load_document(input=content)
        #    not contain punctuation marks or stopwords as candidates.
        pos = {'PROPN','NOUN'}
        #pos = {'PROPN','NOUN'}
        stoplist = list(string.punctuation)
        stoplist  = ['-lrb-', '-rrb-', '-lcb-', '-rcb-', '-lsb-', '-rsb-']
        stoplist  = stopwords.words('english')
        extractor.candidate_selection(pos=pos, stoplist=stoplist)
        # 4. build the Multipartite graph and rank candidates using random walk,
        #    alpha controls the weight adjustment mechanism, see TopicRank for
        #    threshold/method parameters.
        extractor.candidate_weighting(alpha=1.1,
                                      threshold=0.75,
                                      method='average')
        keyphrases = extractor.get_n_best(n=15)
        

        for val in keyphrases:
            out.append(val[0])
    except:
        out = []
        traceback.print_exc()

    return out

And I got the error: ImportError: cannot import name 'SpacyDocReader' from 'pke.readers'

I tried downgrading the version of pke (to 1.8.1) but then there was KeyError 'hinglish'.

---> 29 get_alpha_2 = lambda l: LANGUAGE_CODE_BY_NAME[l]
     30 
     31 lang_stopwords = {get_alpha_2(l): l for l in stopwords._fileids}

KeyError: 'hinglish'

I have never used the pke library before so I'm very confused. I would appreciate all the support! Thank you so much!

CodePudding user response:

It looks like you are trying to import the SpacyDocReader class from the pke library and encountering an ImportError stating that it cannot be imported. There are a few possible causes for this error:

Make sure that you have installed the pke library and that it is available in your Python environment. You can install it using pip install pke or conda install -c conda-forge pke.

Make sure that you have correctly imported the pke library and that you are using the correct syntax to import the SpacyDocReader class. Here is an example of how you can import it:

Copy code from pke.readers import SpacyDocReader Make sure that you have the required dependencies installed. pke relies on the spacy library for natural language processing, so make sure that you have it installed in your environment. You can install it using pip install spacy or conda install -c conda-forge spacy.

Make sure that you are using the correct version of the pke library. If you are using an older version of the library, the SpacyDocReader class may not be available.

Make sure that there are no syntax errors or typos in your code. Double-check your import statement and make sure that you are using the correct spelling and capitalization for the class name.

I hope this helps! If you continue to encounter issues, please provide more information about your setup and the error message you are seeing, and I will do my best to assist you further.

CodePudding user response:

To resolve the ImportError: cannot import name 'SpacyDocReader' from 'pke.readers' error, you can try the following solutions:

Make sure that the pke package is installed on your system. You can install the package using pip install pke. If the package is installed, but it is not in your Python path, you can add it to your Python path by modifying the PYTHONPATH environment variable or by specifying the path to the package in your Python code using the sys.path.append method. Check for typos in the import statement. Make sure that you have spelled the module and package names correctly. If you are using an older version of the pke package, the SpacyDocReader module may have been removed or renamed in a newer version. In this case, you may need to update your code to use the correct module name or upgrade to the latest version of the package.

import sys

Add the path to the pke package to the Python path

sys.path.append('/path/to/pke')

Import the SpacyDocReader module

from pke.readers import SpacyDocReader

CodePudding user response:

I have installed the pke follow the document instruction and it work. I attached the colab notebook here, you can check again. If you have problem, pls feedback for me, thanks

https://colab.research.google.com/drive/1dSZGTtjFGl-tNZIsr_5hThpUgTddem5a?usp=sharing

  • Related