เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

นำ child class ไปใช้งาน

ด้วยพลังของการสืบทอด (inheritance) คุณสามารถสร้างคลาส SocialMedia ที่มีฟีเจอร์ครบครันจากคลาสแม่ Document ได้ มาลองใช้ฟีเจอร์เหล่านั้นกันดูครับ

ด้านล่างคือนิยามเต็มของ SocialMedia สำหรับอ้างอิง นอกจากนี้ยังได้เพิ่ม SocialMedia เข้าไปใน __init__.py แล้วเพื่อความสะดวกในการใช้งาน

class SocialMedia(Document):
    def __init__(self, text):
        Document.__init__(self, text)
        self.hashtag_counts = self._count_hashtags()
        self.mention_counts = self._count_mentions()

    def _count_hashtags(self):
        # Filter attribute so only words starting with '#' remain
        return filter_word_counts(self.word_counts, first_char='#')      

    def _count_mentions(self):
        # Filter attribute so only words starting with '@' remain
        return filter_word_counts(self.word_counts, first_char='@')

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

หลักการวิศวกรรมซอฟต์แวร์ใน Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • import แพ็กเกจ text_analyzer ที่สร้างขึ้นเอง
  • กำหนดให้ dc_tweets เป็น instance ของ SocialMedia โดยใช้ออบเจ็กต์ datacamp_tweets ที่โหลดไว้แล้วเป็น text
  • print รายชื่อผู้ใช้ที่ถูก mention บ่อยที่สุด 5 อันดับแรกจากข้อมูล โดยใช้ attribute ที่เหมาะสมของ dc_tweets
  • ใช้เมธอด plot_counter() ของ text_analyzer เพื่อพล็อต hashtag ที่ถูกใช้มากที่สุดในข้อมูล โดยใช้ attribute ที่เหมาะสมของ dc_tweets

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Import custom text_analyzer package
import ____

# Create a SocialMedia instance with datacamp_tweets
dc_tweets = ____(text=datacamp_tweets)

# Print the top five most mentioned users
print(dc_tweets.____.most_common(5))

# Plot the most used hashtags
text_analyzer.____(dc_tweets.____)
แก้ไขและรันโค้ด