menu

Questions & Answers

A question about how to use class function in python

I am wonder whether I can import my class function into my work. I have put a ipynb file (...\Ipynb Files\job.ipynb) and a py file (...\Ipynb Files\text_modification.py) which contains all functions into a same file path (...\Ipynb Files)

Here is my class

class text_modification():
    def __init__(self,text):
        self.text=text
    #Remove all the emoji used in the text
    def emoji_free_text(text):
        pattern = re.compile(pattern = 
        '[' u'\U0001F600-\U0001F64F'  # emoticons
            u'\U0001F300-\U0001F5FF'  # symbols & pictographs
            u'\U0001F680-\U0001F6FC'  # transport & map symbols
            u'\U0001F1E6-\U0001F1FF'  # flags (iOS)
            u'\U00002500-\U00002BEF'  # chinese char
            u'\U00002702-\U000027B0'
            u'\U000024C2-\U0001F251'
            u'\U0001f926-\U0001f937'
            u'\U00010000-\U0010ffff'
            u'\u2640-\u2642'
            u'\u2600-\u2B55'
            u'\u23cf'
            u'\u23e9'
            u'\u231a'
            u'\u3030'
            u'\ufe0f'
            u'\u200a-\u200f'']+', 
            flags = re.UNICODE)
        return pattern.sub(r'',self.text)

    # text modification
    def delete_repetition(emoji_content, max_times=2):
        emoji=list(emoji_content)
        emotion=' '.join(emoji_content)
        checklist = [lab for lab in dict.fromkeys(emoji) if emoji.count(lab) > 1]
        for i in range(len(checklist)):
            while(emoji.count(checklist[i]) > max_times):
                emoji.remove(checklist[i])
                emotion=''.join(emoji)
        return emotion

    def remove_duplicate_emoji(self,text):
        pure_text=emojiFreeText(text)
        duplicate_emoji = []
        for emo in text:
            if emo in (emoji.UNICODE_EMOJI['en'] or emoji.UNICODE_EMOJI['es'] or emoji.UNICODE_EMOJI['pt'] or emoji.UNICODE_EMOJI['it']):
                duplicate_emoji.append(emo)
        twice_maximum=delete_repetition(duplicate_emoji)
        text=pure_text+twice_maximum
        return self.text

    def free_contact(text):
        text=' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",text).split())
        return text

     def data_processing_auto(text):
                            emoji_free_text(
                                remove_duplicate_emoji(text))

And then is my ipynb file

raw_data=pd.read_csv('result.csv',sep=',',header=0,encoding='unicode_escape')
raw_data=raw_data.drop(columns=['Unnamed: 0','source'])
raw_data=raw_data[raw_data['text']!='text'].sort_values(by='created at').reset_index(drop=True)
from text_modification import *
for text in raw_data['text']:
    print(text_modification(text).data_processing_auto())

But there is always an error showing that

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[9], line 9
      6 from text_modification import *
      8 for text in raw_data['text']:
----> 9     print(text_modification(text).emoji_free_text(text))

AttributeError: 'text_modification' object has no attribute 'emoji_free_text'

I have not ideas why the class mentions about raw_data since it never shows within the line, thanks for anyone who can solve this problem

Comments:
2023-01-23 00:30:11
post a minimal reproducible example. If you are getting an error, post the full error message including the stack trace
2023-01-23 00:30:11
Since you're not using self, you should just define emoji_free_text as a standalone function. You've over-engineered things with classes.
2023-01-23 00:30:11
Thanks for 9769953's comments, but what if I have multiple functions in a class?
2023-01-23 00:30:11
Then probably those other methods have the same problem, or you need to explain why you want this static method to be in the class. Maybe simply adding the @staticmethod decorator will solve that, though this is completely tangential to your actual question anyway. What exactly does your import statement look like?
2023-01-23 00:30:11
@tripleee I want to to the class function to realise coding automation, rather than using function combination since they are too long to read in a jupyternotebook, and being difficult to check error. I have no ideas about why not adding 'self' into the class, wish you can explain a litte bit, Cheers
Answers(0) :