这是一个包含两个函数的类,尝试使用类内的函数返回NameError消息。我调用了postprocess函数上的tokenize函数,代码行如下所示自我数据['review\u text'].progress\u map(tokenize)”但是它返回了一个名称错误消息。你知道吗
class preprocess():
df = data
def tokenize(tweet):
try:
token = unicodedata.normalize("NFKD",
tweet).encode("ascii",
"ignore").decode("utf8") # converts 'ueber' to 'uber'
token = re.sub('\ |\?|\.|\!|\/|\;|\:|\<|\>|\+|\$|\*|\)|\
(|\&|\=|\%|\-|\'|\"|\%{', ' ', token)# Lets pass only
meaningful characters
if '\n\n' in token:# remove header
token = token[token.index('\n\n'):]
token = re.sub(r'([^a-zA-Z0-9 \-\_%])', '', tweet)# Lets
pass
only meaningful characters
token = re.sub(r'((\.\s*){2,})', '', token)# removes
multiple
dots with optional whitespaces in between
token = re.sub(r'(\s{2,})', ' ', token) # Removes multiple
whitespaces
token = token.lower()# lower cases everything
#token = re.sub(r'(?<=\s)[\w?!%,.;:\/]{1,3}(?=\s|\Z)', '',
token)# removes tokens shorter than minLen
token = re.sub(r'\b(?!(\D\S*|[12][0-9]{3})\b)\S+\b', '',
token) # removes all digits except digits that represent
years
token = re.sub(r'<.*?>', '', token)# remove html
token = re.sub(r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-
9-.]+', '', token)# remove email addresses
token = re.sub(r'["\']', '', token )# remove quotes
token = token.replace('\n', ' ')# replace newlines
tokens = tokenizer.tokenize(token)
return tokens
except:
return 'NC'
def postprocess(self):
self.data = self.df.head(58)
self.data['tokens'] =
self.data['review_text'].progress_map(tokenize) ## progress_map
is a variant of the map function plus a progress bar. Handy to
monitor DataFrame creations.
self.data = self.data[self.data.tokens != 'NC']
self.data.reset_index(inplace=True)
self.data.drop('index', inplace=True, axis=1)
self.data.drop(['review_text'],inplace=True, axis=1)
return self.data
我这样调用函数 hei=预处理() 数据=后置处理()
错误消息:
NameError Traceback (most recent call
last)
<ipython-input-48-0806f2cc8d73> in <module>()
1 hei = preprocess()
----> 2 data=hei.postprocess()
3
<ipython-input-47-57d07abbfeec> in postprocess(self)
30 def postprocess(self):
31 self.data = self.df.head(58)
---> 32 self.data['tokens'] =
self.data['review_text'].progress_map(tokenize) ## progress_map is a
variant of the map function plus a progress bar. Handy to monitor
DataFrame creations.
33 self.data = self.data[self.data.tokens != 'NC']
34 self.data.reset_index(inplace=True)
NameError:未定义名称“tokenize”
我不太清楚你是在哪里遇到错误的(你没有发现),但我认为是在这里:
你在那里这样调用buildWordVector-这似乎是问题所在,因为函数buildWordVector是在类中,应该这样调用:
你应该试试。希望我能帮忙(如果还有疑问,请评论)
示例(没有完整代码):
编辑: 你说你需要帮助从一个类的另一个函数运行一个类的函数。假设你有这样一节课:
假设您想在调用spam()的类中创建一个函数foo()。您必须确保在定义spam()时,它(以及调用它的函数)具有神奇的自变量(spam(self))。然后,从另一个函数调用它,您所要做的就是自我垃圾邮件(),它将被调用。这就是我要说的:
另外,参数:
所以,你就是这样做的。但是,由于这个神奇的自变量,您必须以不同的方式调用类中的函数—首先生成testClass()的实例。如何操作:
假设你已经完成了上面的课程。首先,创建类的实例。你知道吗
然后使用testClassInstance调用函数,如下所示:
所以这就是你要做的,希望你能理解。如果仍有疑问,请发表评论。你知道吗
相关问题 更多 >
编程相关推荐