2017-08-09 1792 views
-1

我面临这个错误,我的函数被说成是没有定义的尽管我已经正确定义并调用函数,这是我得到的错误,请帮助:在python中调用函数时出错“函数未定义”

文件“split_text.py”,行80,在 split_pun(字)#passing词的价值split_pun函数删除标点但是这给了我一个错误 NameError:名字“split_pun”没有定义

这里是代码:

""" 
Natural Language Toolkit: Urdu Language POS-Tagged (not yet) Corpus Reader 
""" 
import re 
from six import string_types 

from nltk.tag import str2tuple, map_tag 
import os.path 
from nltk.corpus.reader.util import * 
from nltk.corpus.reader.api import * 


class UrduCorpusReader(CorpusReader): 

    def words(self, fileids=None): #function for word tokenization 
     """ 
     List of words, one per line. Blank lines are ignored. 
     """ 
     words_list = [] 
     for filepath in self.abspaths(fileids=fileids): 
      # print(filepath) 
      data = open(filepath, 'r').read() 
      data = data.replace('\n',' ') 
      words_list = data.split(' ') 
      #print(words_list)  #printing the words after tokenization 

     return words_list 
    def split_pun(self,ifile): #function for punctuation removal 
     punctuations = [ 
     u'\u06D4', # arabic full stop 
     '.', 
     u'\u061F', # Arabic question mark 
     u'\u061B', #ARABIC SEMICOLON 
     u'\u066D', #ARABIC FIVE POINTED STAR 
     u'\u2018' ,#LEFT SINGLE QUOTATION MARK 
     u'\u2019' ,#Right Single Quotation Mark 
     u'\u0027' ,#APOSTROPHE 
     '/', 
     ':', 
     ';', 
     '-', 
     '*', 
     ')', 
     '(', 
     '/' 
    ] 
     f = open(ifile,'r') 
     text = f.readlines() 
     f.close() 
     for x in range(0,len(text)): 
      s1 = ''.join(ch for ch in s if ch not in punctuations) 
      print(s1) 
     return s1 



    def raw(self, fileids=None): 
     if fileids is None: 
      fileids = self._fileids 
     elif isinstance(fileids, string_types): 
      fileids = [fileids] 

     return concat([self.open(f).read() for f in fileids]) 




if '__main__' == __name__: 

    word = ' ' 
    corpus_root = os.path.abspath('../test_data') 
    wordlists = UrduCorpusReader(corpus_root, '.*') 
    print("Loaded corpus with file IDs: ") 
    print(wordlists.fileids()) 
    list1 = wordlists.fileids() 
    for infile in (wordlists.fileids()): 
     print(infile) 
     word = wordlists.words(infile) #calling the words function and the save its output 
     split_pun(word) #passing the value of words function in split_pun to remove punctuation but this gives me an error 

回答

1

由于split_punUrduCorpusReader类中的实例方法,因此需要从实例中调用它。

split_pun(word)应该是wordlists.split_pun(word)
(就像您使用wordlists.words(infile)上面的线一样)。

+0

@ user3778289这与您的原始错误无关。您应该使用正在运行的更新代码以及当前出现的错误来开启一个新问题。 – DeepSpace

+0

谢谢,它工作^ _ ^。然而,当我试图给出一个函数的输出时,我得到了这个错误:单词作为另一个函数的输入:split_pun善意帮助,下面是错误:给定Traceback(最近调用最后一个): 文件“split_text.py”, 80行, 单词列表。 split_pun(word) split_pun文件“split_text.py”,第48行, f = open(ifile,'r') TypeError:无效文件: – user3778289

+0

@ user3778289查看我以前的评论... – DeepSpace

相关问题