【问题标题】:Unable to import process_tweets from utils无法从 utils 导入 process_tweets
【发布时间】:2020-12-19 13:06:56
【问题描述】:

感谢您对此进行调查,我有一个 python 程序,我需要 process_tweetbuild_freqs 来完成一些 NLP 任务,nltk 已经安装并且 utils 还没有 所以我通过pip install utils 安装了它,但是上面提到的两个模块显然没有安装,我得到的错误是这里的标准错误,

ImportError: cannot import name 'process_tweet' from
'utils' (C:\Python\lib\site-packages\utils\__init__.py)

我做错了什么还是有什么遗漏? 我还提到了This stackoverflow answer,但没有帮助。

【问题讨论】:

  • 你是从哪里得到这个的?你必须从utils 导入process_tweet?你在看教程吗?

标签: python nlp nltk sentiment-analysis


【解决方案1】:

您可以使用 ?? 轻松访问任何源代码,例如在本例中:process_tweet?? (以上代码来自 deeplearning.ai NLP course custome utils library):

def process_tweet(tweet):
"""Process tweet function.
Input:
    tweet: a string containing a tweet
Output:
    tweets_clean: a list of words containing the processed tweet

"""
stemmer = PorterStemmer()
stopwords_english = stopwords.words('english')
# remove stock market tickers like $GE
tweet = re.sub(r'\$\w*', '', tweet)
# remove old style retweet text "RT"
tweet = re.sub(r'^RT[\s]+', '', tweet)
# remove hyperlinks
tweet = re.sub(r'https?:\/\/.*[\r\n]*', '', tweet)
# remove hashtags
# only removing the hash # sign from the word
tweet = re.sub(r'#', '', tweet)
# tokenize tweets
tokenizer = TweetTokenizer(preserve_case=False, strip_handles=True,
                           reduce_len=True)
tweet_tokens = tokenizer.tokenize(tweet)

tweets_clean = []
for word in tweet_tokens:
    if (word not in stopwords_english and  # remove stopwords
            word not in string.punctuation):  # remove punctuation
        # tweets_clean.append(word)
        stem_word = stemmer.stem(word)  # stemming word
        tweets_clean.append(stem_word)

【讨论】:

    【解决方案2】:
    试试这段代码,它应该可以工作:
    def process_tweet(tweet):
    stemmer = PorterStemmer()
    stopwords_english = stopwords.words('english')
    tweet = re.sub(r'\$\w*', '', tweet)
    tweet = re.sub(r'^RT[\s]+', '', tweet)
    tweet = re.sub(r'https?:\/\/.*[\r\n]*', '', tweet)
    tweet = re.sub(r'#', '', tweet)
    tokenizer = TweetTokenizer(preserve_case=False,        strip_handles=True,reduce_len=True)
    tweet_tokens = tokenizer.tokenize(tweet)
    
    tweets_clean = []
    for word in tweet_tokens:
        if (word not in stopwords_english and  
                word not in string.punctuation): 
            stem_word = stemmer.stem(word)  # stemming word
            tweets_clean.append(stem_word)
    
    return tweets_clean
    

    【讨论】:

    • 它确实有效,但是 utils 应该有这些功能,那为什么没有呢?
    【解决方案3】:

    如果您正在学习 deeplearning.ai 上的 NLP 课程,那么我相信 utils.py 文件是由该课程的讲师创建的,用于在实验室课程中使用,不应与通常的 utils 混淆。

    【讨论】:

    • 是的,确实,你有那个实现吗,我需要在我的本地机器上做。
    【解决方案4】:

    我猜你根本不需要使用process_tweet。课程中的代码只是总结你从开始到词干步骤所做的一切的捷径;因此,只需忽略该步骤,只需打印出 tweet_stem 即可查看原始文本和预处理文本之间的区别。

    【讨论】:

    • 能否请您链接到课程或代码,以便为您的答案提供更多背景信息。
    猜你喜欢
    • 2019-08-06
    • 1970-01-01
    • 2022-12-21
    • 2012-08-20
    • 2021-07-10
    • 2014-05-29
    • 1970-01-01
    • 2022-06-13
    • 2012-08-27
    相关资源
    最近更新 更多