-1

I am facing this error where my function is said to be not defined Even though i have properly defined and called the function this is the error i am getting please help:

File "split_text.py", line 80, in split_pun(word) #passing the value of words function in split_pun to remove punctuation but this gives me an error NameError: name 'split_pun' is not defined

here is the code:

"""
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
tripleee
  • 175,061
  • 34
  • 275
  • 318
user3778289
  • 323
  • 4
  • 18
  • Possible duplicate of [Python call function within class](https://stackoverflow.com/questions/5615648/python-call-function-within-class) – tripleee Sep 02 '19 at 04:52

1 Answers1

1

Since split_pun is an instance method in the UrduCorpusReader class, you need to call it from an instance.

split_pun(word) should be wordlists.split_pun(word)
(just like you used wordlists.words(infile) the line above it).

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • @user3778289 This is not related to your original error. You should open a new question with the updated code you are running and the current error you are getting. – DeepSpace Aug 09 '17 at 11:45
  • Thank you, it worked ^_^. however i am getting this error when trying to give the output of one function: words as the input of another function:split_pun kindly help, below is the error: given Traceback (most recent call last): File "split_text.py", line 80, in wordlists. split_pun(word) File "split_text.py", line 48, in split_pun f = open(ifile,'r') TypeError: invalid file: – user3778289 Aug 09 '17 at 11:51