2017-08-09 155 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

相關問題