โมดูล scikits. timeseries ไม่มีการพัฒนาที่ใช้งานอยู่แล้ว มีรายการข้อบกพร่องที่ไม่น่าจะแก้ไขได้ แผนคือการใช้งานหลักของโมดูลนี้ใน pandas ถ้าคุณต้องการเห็นโมดูลนี้ใช้ชีวิตได้อย่างอิสระจากหมีแพนด้าคุณสามารถแยกโค้ดออกและนำไปใช้งานได้ โมดูล scikits. timeseries มีชั้นเรียนและฟังก์ชันสำหรับจัดการรายงานและวางแผนชุดเวลาของความถี่ต่างๆ โฟกัสอยู่ในการเข้าถึงข้อมูลที่สะดวกและการจัดการในขณะที่ใช้ประโยชน์จากฟังก์ชันการทำงานทางคณิตศาสตร์ที่มีอยู่ใน numpy และ scipy หากสถานการณ์ต่อไปนี้คุ้นเคยกับคุณแล้วคุณอาจพบโมดูล scikits. timeseries ที่เป็นประโยชน์: เปรียบเทียบชุดเวลากับช่วงข้อมูลที่แตกต่างกัน (เช่นราคาหุ้น) สร้างชุดลำดับเวลาที่มีป้ายแกนที่เว้นระยะห่างอย่างชาญฉลาดแปลงชุดเวลารายวัน เป็นรายเดือนโดยการคำนวณค่าเฉลี่ยในแต่ละเดือนทำงานกับข้อมูลที่ไม่มีค่ากำหนดวันทำการสุดท้ายของเดือนก่อน ๆ สำหรับวัตถุประสงค์ในการรายงานคำนวณค่าเบี่ยงเบนมาตรฐานเคลื่อนที่ได้อย่างมีประสิทธิภาพนี่เป็นเพียงสถานการณ์บางอย่างที่ทำได้ง่ายมากด้วย scikits โมดูล. timeseries Documentation1 การประมวลผลภาษาและงูหลามเป็นเรื่องง่ายที่จะได้รับในมือของเราในล้านคำของข้อความ เราสามารถทำอะไรได้บ้างสมมติว่าเราสามารถเขียนโปรแกรมง่ายๆได้ในบทนี้จะกล่าวถึงคำถามต่อไปนี้: สิ่งที่เราสามารถบรรลุได้โดยใช้เทคนิคการเขียนโปรแกรมแบบง่ายๆกับข้อความจำนวนมากเราสามารถแยกคำและวลีที่สำคัญ ๆ ลักษณะและเนื้อหาของข้อความเครื่องมือและเทคนิคแบบใดที่ภาษาโปรแกรม Python มีไว้สำหรับการทำงานดังกล่าวความท้าทายที่น่าสนใจบางประการของการประมวลผลภาษาธรรมชาติบทนี้แบ่งออกเป็นส่วนที่ข้ามระหว่างรูปแบบที่แตกต่างกันไปสองแบบ ในการคำนวณด้วยภาษาที่เราจะใช้ในการเขียนโปรแกรมด้านภาษาศาสตร์โดยไม่จำเป็นต้องอธิบายถึงวิธีการทำงาน เราจะตรวจสอบแนวคิดการเขียนโปรแกรมที่สำคัญ ๆ ในรูปแบบปริยายของ Pythonquot ดีธงทั้งสองรูปแบบในส่วนชื่อ แต่บทต่อมาจะผสมผสานทั้งสองรูปแบบโดยไม่ต้องขึ้นด้านหน้าเกี่ยวกับเรื่องนี้ เราหวังว่าการแนะนำแบบนี้จะทำให้คุณได้ลิ้มรสความจริงในสิ่งที่จะเกิดขึ้นในภายหลังในขณะที่ครอบคลุมแนวคิดพื้นฐานทางภาษาศาสตร์และวิทยาการคอมพิวเตอร์ หากคุณมีพื้นฐานความคุ้นเคยกับทั้งสองด้านคุณสามารถข้ามไปที่ 5 เราจะทำซ้ำประเด็นสำคัญ ๆ ในบทต่อ ๆ ไปและถ้าคุณพลาดอะไรคุณสามารถอ่านเอกสารอ้างอิงออนไลน์ได้ที่ nltk. org หากเนื้อหานี้เป็นเนื้อหาใหม่แก่คุณบทนี้จะมีคำถามมากกว่าที่จะตอบคำถามที่ระบุไว้ในส่วนที่เหลือของหนังสือเล่มนี้ 1 การคำนวณด้วยภาษา: ข้อความและคำทุกคนคุ้นเคยกับข้อความมากเนื่องจากเราอ่านและเขียนทุกวัน ที่นี่เราจะถือว่าข้อความเป็นข้อมูลดิบสำหรับโปรแกรมที่เราเขียนโปรแกรมที่จัดการและวิเคราะห์ในหลายวิธีที่น่าสนใจ แต่ก่อนที่เราจะทำเช่นนี้เราต้องเริ่มต้นด้วยล่าม Python 1.1 การเริ่มต้นใช้ Python สิ่งหนึ่งที่เป็นมิตรเกี่ยวกับ Python คือช่วยให้คุณสามารถพิมพ์โดยตรงลงในโปรแกรมแปลภาษาแบบโต้ตอบ 8212 ซึ่งจะรันโปรแกรม Python ของคุณ คุณสามารถเข้าถึงล่าม Python โดยใช้อินเทอร์เฟซแบบกราฟิกที่เรียกว่า Interactive DeveLopment Environment (IDLE) ใน Mac คุณสามารถค้นหาได้จาก Applications 8594 MacPython และบน Windows ภายใต้โปรแกรมทั้งหมด 8594 Python ภายใต้ Unix คุณสามารถเรียกใช้งูหลามจากเปลือกโดยการพิมพ์ไม่ได้ใช้งาน (ถ้ายังไม่ได้ติดตั้งให้ลองพิมพ์หลาม) ล่ามจะพิมพ์คำประกาศแจ้งเกี่ยวกับรุ่น Python ของคุณเพียงตรวจสอบว่าคุณใช้ Python 3.2 หรือสูงกว่า (นี่คือ 3.4.2): ครั้งแรกที่คุณใช้ความสอดคล้องกันในข้อความใดข้อความหนึ่งจะใช้เวลาเพียงไม่กี่วินาทีในการสร้าง ดัชนีเพื่อให้การค้นหาที่ตามมารวดเร็ว การเปิดของคุณ: ลองค้นหาคำอื่น ๆ เพื่อบันทึกการพิมพ์ใหม่คุณอาจสามารถใช้ลูกศรชี้ขึ้น Ctrl-up-arrow หรือ Alt-p เพื่อเข้าถึงคำสั่งก่อนหน้าและแก้ไขคำที่กำลังค้นหาได้ นอกจากนี้คุณยังสามารถลองค้นหาข้อความอื่น ๆ ที่เราได้รวมไว้ ตัวอย่างเช่นค้นหา Sense และ Sensibility สำหรับคำรัก ใช้ text2.concordance (quotaffectionquot) ค้นหาหนังสือปฐมกาลเพื่อดูว่าคนบางคนอาศัยอยู่นานแค่ไหนโดยใช้ text3.concordance (quotlivedquot) คุณสามารถดู text4 ได้ ที่อยู่พิธีเปิด เพื่อดูตัวอย่างภาษาอังกฤษย้อนหลังไปถึงปี ค. ศ. 1789 และค้นหาคำต่างๆเช่นประเทศชาติ ความหวาดกลัว พระเจ้าเพื่อดูว่าคำเหล่านี้ถูกนำมาใช้แตกต่างกันอย่างไรเมื่อเวลาผ่านไป ใดก็ตามเรายังรวม text5 คอร์ป NPS Chat ค้นหาคำไม่สุภาพเช่น im ur ฮ่า ๆ. (โปรดทราบว่าคลังข้อมูลนี้ถูกวาบขึ้นมา) เมื่อคุณใช้เวลาตรวจสอบข้อความเหล่านี้เพียงเล็กน้อยแล้วเราหวังว่าคุณจะรู้สึกถึงความมีชีวิตชีวาและความหลากหลายของภาษาใหม่ ในบทถัดไปคุณจะได้เรียนรู้วิธีเข้าถึงข้อความในรูปแบบกว้าง ๆ รวมถึงข้อความในภาษาอื่นที่ไม่ใช่ภาษาอังกฤษ ความสอดคล้องช่วยให้เราเห็นคำในบริบทได้ ตัวอย่างเช่นเราเห็นว่าสิ่งมหึมาเกิดขึ้นในบริบทเช่นภาพและขนาด คำอื่น ๆ ที่ปรากฏในช่วงของบริบทที่คล้ายคลึงกันเราสามารถหาคำตอบได้โดยการเติมคำที่คล้ายกับชื่อของข้อความที่ต้องการแล้วแทรกคำที่เกี่ยวข้องในวงเล็บ 2 ดู Python ที่ใกล้ชิดกว่า: Texts เป็นรายการคำหลังจาก prompt weeve รับชื่อที่เราสร้างขึ้น, sent1 ตามด้วยเครื่องหมายเท่ากับและจากนั้นบางคำที่ยกมาคั่นด้วยเครื่องหมายจุลภาคและล้อมรอบด้วยวงเล็บ สิ่งนี้ถูกเรียกว่ารายการในงูหลาม: เป็นวิธีที่เราจัดเก็บข้อความ เราสามารถตรวจสอบได้โดยการพิมพ์ชื่อ เราสามารถขอความยาวได้ เราสามารถใช้ฟังก์ชัน lexicaldiversity () ของเราเองได้ ขอให้สังเกตว่าดัชนีของเราเริ่มต้นจากศูนย์: ส่งศูนย์องค์ประกอบที่เขียนส่ง 0 เป็นคำแรก word1 ในขณะที่องค์ประกอบที่ส่งมา 9 คือ word10 เหตุผลก็คือตอนนี้ Python เข้าถึงเนื้อหาของรายการจากหน่วยความจำของคอมพิวเตอร์ซึ่งเป็นองค์ประกอบแรกที่เราต้องบอกว่ามีกี่องค์ประกอบที่จะไปข้างหน้า ดังนั้นศูนย์ก้าวไปข้างหน้าทิ้งไว้ที่องค์ประกอบแรก การปฏิบัติของการนับจากศูนย์เป็นครั้งแรกสับสน แต่โดยทั่วไปของภาษาโปรแกรมสมัยใหม่ คุณจะได้รับการแขวนของมันถ้า youve เข้าใจระบบนับศตวรรษที่ 19XY เป็นปีในศตวรรษที่ 20 หรือถ้าคุณอาศัยอยู่ในประเทศที่ชั้นของอาคารที่มีหมายเลขจาก 1 และเพื่อเดินขึ้น n-1 เที่ยวบินของบันไดจะนำคุณไปถึงระดับ n ตอนนี้ถ้าเราตั้งใจใช้ดัชนีที่มีขนาดใหญ่เกินไปเราจะได้รับข้อผิดพลาด: เวลานี้ไม่ใช่ข้อผิดพลาดทางไวยากรณ์เนื่องจากส่วนของโปรแกรมถูกต้องตามหลักไวยากรณ์ แต่เป็นข้อผิดพลาดในการรันไทม์ และจะสร้างข้อความ Traceback ที่แสดงบริบทของข้อผิดพลาดตามด้วยชื่อข้อผิดพลาด IndexError และคำอธิบายสั้น ๆ ให้เราพิจารณาดูคร่าวๆโดยใช้ประโยคประดิษฐ์ของเราอีกครั้ง ที่นี่เราตรวจสอบว่าชิ้น 5: 8 ประกอบด้วยองค์ประกอบที่ส่งที่ดัชนี 5, 6 และ 7: เราสามารถปรับเปลี่ยนองค์ประกอบของรายการได้โดยการกำหนดค่าดัชนีของค่าใดค่าหนึ่ง ในตัวอย่างต่อไปเราจะส่ง 0 ทางด้านซ้ายของเครื่องหมายเท่ากับ นอกจากนี้เรายังสามารถเปลี่ยนชิ้นส่วนทั้งหมดด้วยวัสดุใหม่ได้ ผลสุดท้ายของการเปลี่ยนแปลงครั้งนี้คือรายการมีเพียง 4 องค์ประกอบเท่านั้นและการเข้าถึงค่าที่เกิดขึ้นในภายหลังก่อให้เกิดข้อผิดพลาด การเปิดของคุณ: ใช้เวลาสักครู่เพื่อกำหนดประโยคของคุณเองและแก้ไขคำแต่ละคำและกลุ่มของคำ (ชิ้น) โดยใช้วิธีการเดียวกันกับที่ใช้ก่อนหน้านี้ ตรวจสอบความเข้าใจของคุณโดยลองใช้แบบฝึกหัดในรายการที่ท้ายบทนี้ 2.3 ตัวแปรตั้งแต่เริ่มต้นของ 1 คุณได้เข้าถึงข้อความที่เรียกว่า text1 text2 และอื่น ๆ บันทึกจำนวนมากในการพิมพ์เพื่อให้สามารถอ้างอิงถึงหนังสือที่มีคำว่า 250,000 คำที่มีชื่อสั้น ๆ เช่นนี้โดยทั่วไปเราสามารถสร้างชื่อสำหรับสิ่งที่เราสนใจในการคำนวณได้ เราทำสิ่งนี้เองในส่วนก่อนหน้าเช่น กำหนดตัวแปร send1 บรรทัดต่อไปนี้มีรูปแบบ: นิพจน์ตัวแปร งูหลามจะประเมินนิพจน์และบันทึกผลลัพธ์ของตัวแปร กระบวนการนี้เรียกว่างาน ไม่สร้างผลลัพธ์ใด ๆ ที่คุณต้องพิมพ์ตัวแปรในบรรทัดของตนเองเพื่อตรวจสอบเนื้อหา เครื่องหมายเท่ากับเป็นการทำให้เข้าใจผิดเล็กน้อยเนื่องจากข้อมูลกำลังเคลื่อนที่จากด้านขวาไปทางซ้าย อาจช่วยให้คิดว่าเป็นลูกศรซ้าย ชื่อของตัวแปรสามารถเป็นได้ทุกอย่างที่คุณต้องการเช่น ใจแคบ ประโยค xyzzy จะต้องเริ่มต้นด้วยตัวอักษรและสามารถรวมตัวเลขและเครื่องหมายขีดล่าง นี่คือตัวอย่างบางส่วนของตัวแปรและงานที่ได้รับมอบหมาย: โปรดจำไว้ว่าคำที่เป็นตัวพิมพ์ใหญ่จะปรากฏก่อนคำที่เป็นตัวพิมพ์เล็กในรายการเรียง ขอให้สังเกตในตัวอย่างก่อนหน้านี้ว่าเราแบ่งนิยามของ mysent มากกว่าสองบรรทัด นิพจน์ของ Python สามารถแบ่งได้หลายเส้นตราบเท่าที่สิ่งนี้เกิดขึ้นภายในวงเล็บชนิดใด ๆ Python ใช้เครื่องหมายคำพูด quot prompt เพื่อระบุว่าต้องการข้อมูลเพิ่มเติม ไม่สำคัญว่าจะใช้การเยื้องเป็นจำนวนเท่าใดในบรรทัดต่อเนื่อง แต่การเยื้องบางส่วนจะทำให้อ่านง่ายขึ้น เป็นการดีที่จะเลือกชื่อตัวแปรที่มีความหมายเพื่อเตือนคุณ 8212 และเพื่อช่วยให้ทุกคนที่อ่านรหัส Python ของคุณ 8212 ว่าโค้ดของคุณมีความหมายอะไรบ้าง งูหลามไม่พยายามทำความเข้าใจกับชื่อที่สุ่มสี่สุ่มห้าปฏิบัติตามคำแนะนำของคุณและไม่คัดค้านถ้าคุณทำสิ่งที่สับสนเช่นหนึ่งสองหรือสองสาม ข้อ จำกัด เพียงอย่างเดียวคือชื่อตัวแปรไม่สามารถเป็นคำสงวนใด ๆ ของ Pythons ได้เช่น def ถ้า ไม่ใช่ และนำเข้า ถ้าคุณใช้คำสงวน Python จะสร้างข้อผิดพลาดทางไวยากรณ์: ดูแลด้วยตัวเลือกชื่อ (หรือตัวระบุ) สำหรับตัวแปร Python ขั้นแรกคุณควรเริ่มต้นชื่อด้วยตัวอักษรตามด้วยตัวเลข (0 ถึง 9) หรือตัวอักษร ดังนั้น abc23 จะปรับ แต่ 23abc จะทำให้เกิดข้อผิดพลาดทางไวยากรณ์ ชื่อเป็นกรณี ๆ ไปซึ่งหมายความว่า myVar และ myvar เป็นตัวแปรที่แตกต่างกัน ชื่อตัวแปรไม่สามารถมีช่องว่างได้ แต่คุณสามารถแยกคำโดยใช้เครื่องหมายขีดล่างได้เช่น myvar ระวังอย่าใส่ยัติภังค์แทนการขีดล่าง: my-var ไม่ถูกต้องเนื่องจาก Python แปลคำว่า quot - quot เป็นเครื่องหมายลบ 2.4 Strings วิธีการบางอย่างที่เราใช้ในการเข้าถึงองค์ประกอบของรายการสามารถทำงานร่วมกับแต่ละคำหรือสตริง ตัวอย่างเช่นเราสามารถกำหนดสตริงให้กับตัวแปร จัดทำดัชนีสตริง และเชือกสตริง: เรายังสามารถดำเนินการคูณและบวกกับสตริง: เราสามารถรวมคำของรายการเพื่อสร้างสตริงเดี่ยวหรือแยกสตริงลงในรายการได้ดังนี้: เราจะกลับมาที่หัวข้อของสตริงใน 3. ในขณะนี้เรามีบล็อคที่สำคัญสองรายการ 8212 รายการและสตริง 8212 และพร้อมที่จะกลับไปทำการวิเคราะห์ภาษา 3 การคำนวณด้วยภาษา: สถิติที่เรียบง่ายช่วยให้เรากลับไปหาวิธีที่เราสามารถนำแหล่งข้อมูลด้านคอมพิวเตอร์ของเราไปใช้กับข้อความจำนวนมากได้ เราได้เริ่มการสนทนานี้ในหัวข้อ 1. และได้เรียนรู้วิธีการค้นหาคำในบริบทวิธีการรวบรวมคำศัพท์ของข้อความวิธีการสร้างข้อความแบบสุ่มในลักษณะเดียวกันและอื่น ๆ ในส่วนนี้เราจะรับคำถามเกี่ยวกับสิ่งที่ทำให้ข้อความแตกต่างไปจากนี้และใช้วิธีอัตโนมัติในการค้นหาคำและสำนวนที่มีลักษณะเฉพาะของข้อความ เช่นเดียวกับใน 1. คุณสามารถลองใช้คุณลักษณะใหม่ ๆ ของภาษา Python โดยคัดลอกลงในล่ามและคุณจะได้เรียนรู้เกี่ยวกับคุณลักษณะเหล่านี้อย่างเป็นระบบในส่วนต่อไปนี้ ก่อนที่จะดำเนินการต่อไปคุณอาจต้องการตรวจสอบความเข้าใจของส่วนสุดท้ายด้วยการคาดการณ์ผลลัพธ์ของรหัสต่อไปนี้ คุณสามารถใช้ล่ามเพื่อตรวจสอบว่าคุณทำถูกต้องหรือไม่ หากคุณไม่แน่ใจว่าจะทำสิ่งนี้ได้อย่างไรควรอ่านบทก่อนหน้านี้ก่อนดำเนินการต่อ 3.1 การแจกแจงความถี่เราจะระบุคำพูดของข้อความที่ให้ข้อมูลมากที่สุดเกี่ยวกับหัวข้อและประเภทของข้อความได้อย่างไรโดยอัตโนมัติลองจินตนาการดูว่าคุณจะหาคำที่ใช้บ่อยที่สุด 50 คำในหนังสือได้อย่างไร วิธีหนึ่งก็คือการเก็บคะแนนให้กับแต่ละรายการคำศัพท์อย่างที่แสดงไว้ใน 3.1 จำนวนจะต้องนับพันแถวและเป็นกระบวนการที่ลำบากมาก 8212 เพื่อให้เราลำบากที่จะมอบหมายงานให้กับเครื่อง รูปที่ 3.1 การนับคำที่ปรากฏในข้อความ (การแจกแจงความถี่) ตารางใน 3.1 เรียกว่าการแจกแจงความถี่ และบอกความถี่ของแต่ละรายการคำศัพท์ในข้อความ (โดยทั่วไปอาจนับชนิดของเหตุการณ์ที่สังเกตได้) เป็นตัวเลขที่แจกแจงเพราะบอกเราว่าจำนวนคำศัพท์ทั้งหมดในข้อความกระจายอยู่ในรายการคำศัพท์อย่างไร เนื่องจากเรามักต้องการการแจกแจงความถี่ในการประมวลผลภาษา NLTK จึงมีการสนับสนุนในตัว ให้ใช้คำว่า FreqDist เพื่อหาคำที่ใช้บ่อยที่สุด 50 คำของ Moby Dick: เมื่อเราเรียกใช้ FreqDist ครั้งแรก เราผ่านชื่อของข้อความเป็นอาร์กิวเมนต์ เราสามารถตรวจสอบจำนวนคำ (quotoutcomesquot) ทั้งหมดที่นับได้ 8212 260,819 ในกรณีของ Moby Dick นิพจน์ mostcommon (50) ให้รายชื่อของ 50 ประเภทที่เกิดขึ้นบ่อยที่สุดในข้อความ Turn ของคุณ: ลองใช้ตัวอย่างการกระจายความถี่ก่อนหน้านี้ด้วยตัวคุณเองสำหรับ text2 โปรดใช้วงเล็บและตัวพิมพ์ใหญ่ที่ถูกต้อง ถ้าคุณได้รับข้อความข้อผิดพลาด NameError: ไม่ได้กำหนดชื่อ FreqDist คุณจำเป็นต้องเริ่มต้นการทำงานของคุณด้วยการนำเข้า nltk. book คำใด ๆ ที่สร้างขึ้นในตัวอย่างสุดท้ายช่วยให้เราเข้าใจหัวข้อหรือประเภทของข้อความนี้ได้เพียงคำเดียวปลาวาฬ เป็นข้อมูลเล็กน้อยมันเกิดขึ้นมากกว่า 900 ครั้ง ส่วนที่เหลือของคำบอกอะไรเราเกี่ยวกับข้อความพวกเขาก็แค่ภาษาอังกฤษ quotertumbing. quot สัดส่วนของข้อความที่ถูกยึดด้วยคำดังกล่าวเราสามารถสร้างพล็อตความถี่สะสมสำหรับคำเหล่านี้ได้โดยใช้ fdist1.plot (50, cumulativeTrue) เพื่อผลิตกราฟในข้อ 3.2 คำศัพท์ 50 คำนี้ใช้เวลาเกือบครึ่งหนึ่งของหนังสือเล่มนี้รูปที่ 3.2 พล็อตสะสมสำหรับ 50 คำที่พบบ่อยที่สุดใน Moby Dick บัญชีเหล่านี้เป็นเวลาเกือบครึ่งหนึ่งของโทเคน หากคำพูดบ่อยๆไม่ช่วยเราได้คำที่เกิดขึ้นเพียงครั้งเดียวสิ่งที่เรียกว่า hapaxes ดูได้โดยพิมพ์ fdist1.hapaxes () รายการนี้ประกอบด้วยศัพท์บัญญัติ cetological ของเถื่อน expostulations และอีกประมาณ 9,000 คน ดูเหมือนว่ามีคำที่หายากมากและไม่เห็นบริบทเราอาจจะคาดเดาได้ว่าครึ่งหนึ่งของ hapaxes มีความหมายอย่างไรในกรณีใด ๆ เนื่องจากคำที่ไม่บ่อยหรือไม่บ่อยๆช่วยเราต้องลองทำอย่างอื่น 3.2 การเลือกคำที่ละเอียดละเอียดถัดไปให้ดูคำยาวของข้อความอาจมีลักษณะและข้อมูลมากขึ้น สำหรับเรื่องนี้เราปรับเปลี่ยนสัญกรณ์บางส่วนจากทฤษฎีเซท เราต้องการหาคำจากคำศัพท์ของข้อความที่มีความยาวมากกว่า 15 ตัว ให้เรียกใช้คุณสมบัตินี้ P เพื่อให้ P (w) เป็นจริงถ้าและเฉพาะเมื่อ w มีความยาวมากกว่า 15 ตัว ตอนนี้เราสามารถแสดงคำที่น่าสนใจโดยใช้สัญกรณ์ชุดคณิตศาสตร์ได้ดังแสดงใน (1a) นี่หมายความว่าชุดของทุก w ที่ w เป็นองค์ประกอบของ V (คำศัพท์) และ w มีคุณสมบัติ P quot. จากนี้เราจะเห็นว่าความยาวของคำที่ใช้กันมากที่สุดคือ 3 และคำที่มีความยาว 3 จะมีความยาวประมาณ 50,000 (หรือ 20) ของคำที่ทำให้เป็นหนังสือ แม้ว่าเราจะไม่ติดตามที่นี่ แต่การวิเคราะห์ความยาวของคำอาจช่วยให้เราเข้าใจถึงความแตกต่างระหว่างผู้เขียนแนวเพลงหรือภาษาต่างๆ 3.1 สรุปฟังก์ชันที่กำหนดไว้ในการกระจายความถี่ 4 กลับไปที่ Python: การตัดสินใจและการควบคุมการแสดงออกเหล่านี้มีรูป f (w) for หรือ w. f () สำหรับ . ที่ f คือฟังก์ชันที่ทำงานกับคำเพื่อคำนวณความยาวหรือแปลงเป็นตัวพิมพ์ใหญ่ ตอนนี้คุณไม่จำเป็นต้องเข้าใจความแตกต่างระหว่างสัญกรณ์ f (w) และ w. f () แทนเพียงแค่เรียนรู้สำนวนภาษา Python ซึ่งจะดำเนินการเดียวกันกับองค์ประกอบของทุกรายการ ในตัวอย่างก่อนหน้านี้จะใช้คำแต่ละคำใน text1 กำหนดให้แต่ละอันขึ้นอยู่กับตัวแปร w และดำเนินการตามที่ระบุในตัวแปร สัญกรณ์ที่อธิบายไว้เพียงแค่นี้เรียกว่าความเข้าใจในเครื่องหมายคำพูด (quotument comprehension. qu) นี่เป็นตัวอย่างแรกของสำนวนภาษา Python ซึ่งเป็นสัญกรณ์ถาวรที่เราใช้เป็นประจำโดยไม่รบกวนการวิเคราะห์ในแต่ละครั้ง การเรียนรู้สำนวนดังกล่าวเป็นส่วนสำคัญในการเป็นโปรแกรมเมอร์ Python ที่คล่องแคล่ว ให้กลับไปที่คำถามเกี่ยวกับขนาดของคำศัพท์และใช้สำนวนเดียวกันที่นี่: ตอนนี้เราไม่ได้นับถ้อยคำแบบสองครั้งเช่น This และ this เราสามารถไปขั้นตอนต่อไปและกำจัดตัวเลขและเครื่องหมายวรรคตอนจากการนับคำศัพท์โดยการกรองรายการที่ไม่ใช่ตัวอักษร: ตัวอย่างนี้ซับซ้อนเล็กน้อย: จะลดจำนวนรายการตามตัวอักษรหมดจดทั้งหมด . บางทีมันอาจจะง่ายกว่าเพียงแค่การนับรายการที่มีอักษรตัวพิมพ์เล็กเท่านั้น แต่คำตอบนี้ให้คำตอบที่ไม่ถูกต้อง (ทำไม) อย่ากังวลหากคุณไม่มั่นใจกับความเข้าใจในรายชื่อเนื่องจากคุณจะเห็นตัวอย่างอื่น ๆ อีกมากมายพร้อมคำอธิบายในบทต่อไปนี้ 4.3 บล็อกโค้ดที่ซ้อนกันภาษาเขียนโปรแกรมส่วนใหญ่อนุญาตให้เราดำเนินการบล็อคโค้ดได้เมื่อนิพจน์เงื่อนไข หรือถ้างบพอใจ. เราได้เห็นตัวอย่างของการทดสอบตามเงื่อนไขในโค้ดเช่น w สำหรับ w ใน sent7 ถ้า len (w) lt 4 ในโปรแกรมต่อไปนี้เราได้สร้างตัวแปรที่เรียกว่า word containing string value cat คำสั่ง if ตรวจสอบว่า len (len) ทดสอบเป็นจริงหรือไม่ เป็นดังนั้นร่างกายของคำสั่ง if จะถูกเรียกใช้และพิมพ์คำสั่งจะถูกดำเนินการแสดงข้อความให้กับผู้ใช้ อย่าลืมพิมพ์คำสั่งพิมพ์โดยพิมพ์สี่ช่องว่าง นี่เรียกว่าลูปเนื่องจาก Python ใช้รหัสในรูปแบบวงกลม เริ่มต้นด้วยการปฏิบัติหน้าที่คำสั่ง Call มีประสิทธิภาพการใช้ตัวแปรคำเพื่อตั้งชื่อรายการแรกของรายการ จากนั้นจะแสดงค่าของคำกับผู้ใช้ จากนั้นก็กลับไปที่คำสั่ง for และทำคำสั่งให้ฉัน ก่อนที่จะแสดงค่าใหม่นี้แก่ผู้ใช้และอื่น ๆ จะดำเนินต่อไปในแบบนี้จนกว่ารายการทั้งหมดของรายการจะได้รับการประมวลผล 4.4 Looping with Conditions ตอนนี้เราสามารถรวมคำสั่ง if และ for เราจะวนไปทุกรายการในรายการและพิมพ์เฉพาะเมื่อลงท้ายด้วยตัวอักษร l ดีเลือกชื่ออื่นสำหรับตัวแปรเพื่อแสดงให้เห็นว่างูหลามไม่พยายามที่จะทำให้ความรู้สึกของชื่อตัวแปร คุณจะสังเกตเห็นว่าถ้าและสำหรับงบมีเครื่องหมายทวิภาคที่ส่วนท้ายของบรรทัดก่อนที่การเยื้องจะเริ่มขึ้น ในความเป็นจริงโครงสร้างการควบคุมของ Python ทั้งหมดจะจบลงด้วยเครื่องหมายจุดคู่ ลำไส้ใหญ่ระบุว่าคำสั่งปัจจุบันเกี่ยวข้องกับบล็อกที่เว้นไว้ว่าดังนี้ นอกจากนี้เรายังสามารถระบุการดำเนินการที่จะต้องทำถ้าเงื่อนไขของคำสั่ง if ไม่เป็นไปตาม ที่นี่เราจะเห็นคำสั่ง elif (else if) และ else statement โปรดทราบว่าเหล่านี้มีเครื่องหมายโคลอนก่อนรหัสเว้นวรรค เราได้สำรวจภาษาด้านล่างขึ้นด้วยความช่วยเหลือของข้อความและภาษาการเขียนโปรแกรม Python อย่างไรก็ตามยังสนใจที่จะใช้ประโยชน์จากความรู้ด้านภาษาและการคำนวณของเราด้วยการสร้างเทคโนโลยีภาษาที่เป็นประโยชน์ ลองใช้โอกาสนี้เพื่อย้อนกลับไปจากเนื้อหาที่มีสาระเพื่อสร้างภาพที่ใหญ่ขึ้นในการประมวลผลภาษาตามธรรมชาติ ในทางปฏิบัติอย่างหมดจดเราทุกคนต้องการความช่วยเหลือในการสำรวจจักรวาลของข้อมูลที่ถูกขังอยู่ในข้อความบนเว็บ เครื่องมือค้นหามีความสำคัญต่อการเติบโตและความนิยมของเว็บ แต่มีข้อบกพร่องบางอย่าง ต้องใช้ทักษะความรู้และความโชคดีในการแยกคำตอบสำหรับคำถามเช่น: สิ่งที่ฉันสามารถไปเที่ยวชมสถานที่ท่องเที่ยวระหว่างฟิลาเดลเฟียและพิตส์เบิร์กได้โดยมีงบประมาณ จำกัด ผู้เชี่ยวชาญบอกว่าอะไรเกี่ยวกับกล้องดิจิตอล SLR สิ่งที่คาดการณ์เกี่ยวกับตลาดเหล็กก็น่าเชื่อถือ ในช่วงสัปดาห์ที่ผ่านมาการรับคอมพิวเตอร์เพื่อตอบคำถามเหล่านี้โดยอัตโนมัติเกี่ยวข้องกับงานด้านการประมวลผลภาษาต่างๆรวมทั้งการดึงข้อมูลการอนุมานและการสรุปผลและจะต้องมีการดำเนินการในระดับหนึ่งและมีระดับความทนทานที่ยังคงอยู่เหนือของเรา ความสามารถในปัจจุบัน เกี่ยวกับระดับปรัชญาเพิ่มเติมความท้าทายอันยาวนานในปัญญาประดิษฐ์คือการสร้างเครื่องอัจฉริยะและส่วนสำคัญของพฤติกรรมอัจฉริยะคือการทำความเข้าใจภาษา หลายปีที่ผ่านมาเป้าหมายนี้ถูกมองว่ายากเกินไป อย่างไรก็ตามเมื่อเทคโนโลยี NLP กลายเป็นผู้ใหญ่ขึ้นและวิธีการที่มีประสิทธิภาพสำหรับการวิเคราะห์ข้อความที่ไม่ จำกัด จะแพร่หลายมากขึ้นโอกาสของความเข้าใจภาษาธรรมชาติจะกลับมาเป็นเป้าหมายที่เป็นไปได้อีกครั้ง ในส่วนนี้เราจะอธิบายเกี่ยวกับเทคโนโลยีเพื่อความเข้าใจในภาษาต่างๆเพื่อให้คุณได้รับทราบถึงความท้าทายที่น่าสนใจที่กำลังรอคุณอยู่ 5.1 Sense Disambiguation ในคำ disambiguation ความรู้สึกที่เราต้องการที่จะทำงานออกซึ่งความรู้สึกของคำที่มีวัตถุประสงค์ในบริบทที่กำหนด พิจารณาคำพูดที่คลุมเครือเสิร์ฟและจาน: เสิร์ฟ ความช่วยเหลือเกี่ยวกับอาหารหรือเครื่องดื่มถือสำนักงานใส่ลูกลงในจานเล่น จานของอุปกรณ์การสื่อสารอาหารในประโยคที่มีวลี: เขาทำอาหารจาน คุณสามารถตรวจสอบได้ว่าทั้งอาหารเสิร์ฟและจานถูกนำไปใช้กับความหมายของอาหาร ไม่น่าที่หัวข้ออภิปรายเปลี่ยนจากกีฬาเป็นเครื่องถ้วยชามในเนื้อที่สามคำ นี้จะบังคับให้คุณคิดค้นภาพที่แปลกประหลาดเช่นเทนนิสโปรเอาออกความผิดหวังของเขาหรือเธอในชุดชาจีนวางข้างศาล กล่าวอีกนัยหนึ่งเราจะกำกวามคำโดยใช้บริบทโดยใช้ความจริงง่ายๆว่าคำใกล้เคียงมีความเกี่ยวข้องกันอย่างใกล้ชิด อีกตัวอย่างหนึ่งของผลกระทบตามบริบทนี้ให้พิจารณาคำนี้ด้วย ซึ่งมีความหมายหลายประการเช่น หนังสือโดย Chesterton (ตัวแทน 8212 Chesterton เป็นผู้เขียนหนังสือ) ถ้วยด้วยเตา (ตำแหน่ง 8212 เตาเป็นที่ถ้วย) และส่งโดยวันศุกร์ (ชั่วคราว 8212 วันศุกร์เป็นเวลาของการส่ง) สังเกตใน (3c) ว่าความหมายของคำตัวเอียงช่วยให้เราตีความความหมายโดย เด็กที่สูญหายถูกค้นพบโดยภูเขา (locative) เด็กที่หายไปพบในช่วงบ่าย (ชั่วคราว) 5.2 ความละเอียดของคำพูด (Pronoun Resolution) ความเข้าใจที่ลึกซึ้งในการทำความเข้าใจภาษาคือการทำงานออกคำพูด whichquot 8212 คือการตรวจจับวัตถุและวัตถุของกริยา คุณเรียนรู้ที่จะทำเช่นนี้ในโรงเรียนประถม แต่มันยากกว่าที่คุณคิด ในประโยคขโมยขโมยภาพวาดมันง่ายที่จะบอกผู้ที่ดำเนินการขโมย พิจารณาสามประโยคต่อไปนี้ใน (4c) และพยายามที่จะตรวจสอบสิ่งที่ถูกขายจับและพบ (กรณีหนึ่งมีความคลุมเครือ) โจรขโมยภาพวาด พวกเขาถูกขายในภายหลัง โจรขโมยภาพวาด พวกเขาถูกจับได้ในภายหลัง โจรขโมยภาพวาด พวกเขาถูกค้นพบในภายหลัง การตอบคำถามนี้เกี่ยวข้องกับการหาคำก่อนหน้าของสรรพนาม ทั้งโจรหรือภาพวาด เทคนิคการคำนวณสำหรับการแก้ปัญหานี้รวมถึงการแก้ปัญหา anphora 8212 ระบุวลีคำสรรพนามหรือคำนามหมายถึง 8212 และการระบุชื่อความหมาย 8212 การระบุวลีคำนามที่เกี่ยวข้องกับคำกริยา (เป็นตัวแทนผู้ป่วยเครื่องมือและอื่น ๆ ) 5.3 การสร้างผลลัพธ์ของภาษาถ้าเราสามารถแก้ปัญหาความเข้าใจภาษาดังกล่าวได้โดยอัตโนมัติเราจะสามารถไปยังงานที่เกี่ยวข้องกับการสร้างผลลัพธ์ของภาษาเช่นการตอบคำถามและการแปลด้วยเครื่อง ในกรณีแรกเครื่องควรจะสามารถตอบคำถามของผู้ใช้เกี่ยวกับการเก็บรวบรวมข้อความ: เพื่อที่จะพิจารณาว่าสมมติฐานได้รับการสนับสนุนจากข้อความระบบจำเป็นต้องมีความรู้พื้นฐานต่อไปนี้: (i) ถ้ามีคนเป็นผู้เขียน ของหนังสือแล้ว heshe ได้เขียนหนังสือเล่มนั้น (ii) ถ้ามีคนเป็นบรรณาธิการของหนังสือแล้ว heshe ไม่ได้เขียน (ทั้งหมด) หนังสือเล่มนั้น (iii) ถ้ามีคนแก้ไขหรือผู้เขียนหนังสือสิบแปดแล้วไม่สามารถสรุปได้ ว่า heshe เป็นผู้เขียนหนังสือสิบแปดเล่ม 5.7 ข้อ จำกัด ของ NLP แม้ว่าความก้าวหน้าด้านการวิจัยในงานต่างๆเช่น RTE ระบบภาษาธรรมชาติที่ใช้งานจริงในโลกใบนี้ยังไม่สามารถใช้เหตุผลร่วมกันหรือวาดความรู้ของโลกได้อย่างทั่วถึงและมีประสิทธิภาพ เราสามารถรอให้ปัญหาทางปัญญาประดิษฐ์ที่ยากลำบากเหล่านี้สามารถแก้ไขได้ แต่ในระหว่างนี้จำเป็นต้องมีข้อ จำกัด บางประการเกี่ยวกับความสามารถในการให้เหตุผลและความรู้ของระบบภาษาธรรมชาติ ดังนั้นตั้งแต่จุดเริ่มต้นเป้าหมายที่สำคัญของการวิจัย NLP ก็คือการทำให้ความคืบหน้าในงานที่ยุ่งยากในการสร้างเทคโนโลยีที่ทำให้เข้าใจภาษาได้โดยใช้เทคนิคที่ผิวเผิน แต่ทรงพลังแทนความรู้และความสามารถในการให้เหตุผลที่ไม่ จำกัด แท้จริงนี่เป็นหนึ่งในเป้าหมายของหนังสือเล่มนี้และเราหวังว่าจะจัดให้คุณมีความรู้และทักษะในการสร้างระบบ NLP ที่เป็นประโยชน์และเพื่อสร้างแรงบันดาลใจในการสร้างเครื่องอัจฉริยะในระยะยาว ข้อความจะถูกแสดงใน Python โดยใช้รายการ: Monty Python เราสามารถใช้การจัดทำดัชนี slicing และ len () ใน list ได้ quottokenquot คำเป็นลักษณะเฉพาะของคำที่ระบุในข้อความ quottypequot คำเป็นรูปแบบเฉพาะของคำเป็นลำดับเฉพาะของตัวอักษร เรานับจำนวนเต็มของคำโดยใช้ len (text) และ word types โดยใช้ len (set (text)) เราได้คำศัพท์ของข้อความ t โดยใช้ sort (set (t)) เราทำงานในแต่ละรายการของข้อความโดยใช้ f (x) สำหรับ x ในข้อความ เราสามารถเขียนชุด (w. lower () สำหรับ w ใน text ถ้า w. isalpha ()) เพื่อให้ได้คำศัพท์การยุบตัวของ case และละเว้นเครื่องหมายวรรคตอน เราประมวลผลแต่ละคำในข้อความโดยใช้คำสั่ง for เช่น w in t: หรือ word ใน text:. ต้องปฏิบัติตามด้วยตัวอักษรลำไส้ใหญ่และกลุ่มรหัสที่เว้นไว้เพื่อดำเนินการทุกครั้งที่ผ่านลูป เราทดสอบเงื่อนไขโดยใช้คำสั่ง if: if len (word) lt 5:. ต้องปฏิบัติตามด้วยตัวอักษรลำไส้ใหญ่และกลุ่มรหัสที่เว้นไว้เพื่อดำเนินการเฉพาะในกรณีที่เงื่อนไขเป็นจริง การแจกแจงความถี่คือชุดของรายการพร้อมกับการนับความถี่ (เช่นคำพูดของข้อความและความถี่ในการปรากฏตัว) ฟังก์ชันคือกลุ่มของรหัสที่ได้รับการกำหนดชื่อและสามารถนำมาใช้ซ้ำได้ หน้าที่ถูกกำหนดโดยใช้คำหลัก def เช่นใน def mult (x, y) x และ y เป็นพารามิเตอร์ของฟังก์ชันและทำหน้าที่เป็นตัวยึดตำแหน่งสำหรับค่าข้อมูลจริง ฟังก์ชันถูกเรียกโดยการระบุชื่อตามด้วยอาร์กิวเมนต์ 0 หรือมากกว่าในวงเล็บเช่นนี้ text () mult (3, 4) len (text1) บทนี้ได้แนะนำแนวความคิดใหม่ ๆ ในการเขียนโปรแกรมการประมวลผลภาษาธรรมชาติและภาษาศาสตร์ผสมผสานเข้าไว้ด้วยกัน หลายคนรวมอยู่ในบทต่อไปนี้ อย่างไรก็ตามคุณอาจต้องการศึกษาเนื้อหาออนไลน์ที่ให้ไว้ในบทนี้ (ที่ nltk. org) ซึ่งรวมถึงลิงก์ไปยังวัสดุพื้นหลังเพิ่มเติมและลิงก์ไปยังระบบ NLP ออนไลน์ คุณอาจต้องการอ่านเกี่ยวกับภาษาศาสตร์และแนวคิดเกี่ยวกับ NLP ในวิกิพีเดีย (เช่น collocations, Turing Test, ความแตกต่างของ type-token) คุณควรทำความเข้าใจกับเอกสาร Python ที่ docs. python. org รวมถึงบทแนะนำมากมายและเอกสารอ้างอิงที่ครอบคลุมที่มีอยู่ คู่มือเริ่มต้นของ Python มีอยู่ที่ wiki. python. orgmoinBeginnersGuide คำถามเบ็ดเตล็ดเกี่ยวกับ Python อาจได้รับการตอบในคำถามที่พบบ่อยที่ python. orgdocfaqgeneral ในขณะที่คุณเจาะลึก NLTK คุณอาจต้องการสมัครสมาชิกรายชื่ออีเมลที่เผยแพร่ชุดเครื่องมือใหม่ ๆ นอกจากนี้ยังมีรายชื่อผู้รับจดหมาย NLTK-Users ซึ่งผู้ใช้ช่วยเหลือซึ่งกันและกันเมื่อเรียนรู้วิธีใช้ Python และ NLTK ในการวิเคราะห์ภาษา รายละเอียดของรายการเหล่านี้มีอยู่ที่ nltk. org สำหรับข้อมูลเพิ่มเติมเกี่ยวกับหัวข้อที่กล่าวถึงใน 5. และใน NLP โดยทั่วไปคุณอาจต้องการปรึกษาหนังสือที่ยอดเยี่ยมต่อไปนี้: Indurkhya, Nitin และ Fred Damerau (eds, 2010) คู่มือการประมวลผลภาษาธรรมชาติ (Second Edition) Chapman amp HallCRC 2010 (Indurkhya amp Damerau, 2010) (Dale, Moisl, Somers, 2000) Jurafsky, Daniel และ James Martin (2008) การพูดและการประมวลผลภาษา (Second Edition) ศิษย์ฮอลล์. (Jurafsky แอมป์มาร์ติน, 2008) Mitkov, Ruslan (ed, 2003) Oxford Handbook of Computational Linguistics สำนักพิมพ์มหาวิทยาลัยออกซฟอร์ด (ฉบับที่สองคาดว่าในปี 2010) (Mitkov, 2002) สมาคมภาษาศาสตร์เชิงคำนวณเป็นองค์กรระหว่างประเทศที่เป็นตัวแทนของ NLP เว็บไซต์ ACL (aclweb. org) เป็นแหล่งข้อมูลที่มีประโยชน์มากมาย ได้แก่ : ข้อมูลเกี่ยวกับการประชุมระหว่างประเทศและระดับภูมิภาคและการประชุมเชิงปฏิบัติการ ACL Wiki พร้อมลิงก์ไปยังแหล่งข้อมูลที่เป็นประโยชน์หลายร้อยชนิดและ ACL Anthology ซึ่งมีเนื้อหาส่วนใหญ่ของงานวิจัยจาก NLP ในช่วง 50 ปีที่ผ่านมาซึ่งมีการจัดทำดัชนีอย่างเต็มที่และดาวน์โหลดได้อย่างอิสระ ตำราภาษาศาสตร์เบื้องต้นเบื้องต้นบางเล่ม ได้แก่ Finegan2007 (OGrady et al, 2004) (OSU, 2007) คุณอาจต้องการปรึกษา LanguageLog บล็อกภาษาศาสตร์ยอดนิยมที่มีข้อความเป็นครั้งคราวที่ใช้เทคนิคที่อธิบายไว้ในหนังสือเล่มนี้ 8 Exercises 9788 ลองใช้ล่าม Python เป็นเครื่องคิดเลขและพิมพ์นิพจน์เช่น 12 (4 1) 9788 ให้ตัวอักษร 26 ตัวมีอำนาจ 26 ให้ 10 หรือ 26 10 สตริงตัวอักษรสิบตัวที่เราสามารถสร้างได้ ทำงานได้ถึง 141167095653376 ตัวอักษรสตริงกี่ตัวที่เป็นไปได้ 9788 การดำเนินการคูณด้วย Python สามารถใช้กับรายการได้ เกิดอะไรขึ้นเมื่อคุณพิมพ์ Monty Python 20 หรือ 3 sent1 9788 บทวิจารณ์ 1 เกี่ยวกับการคำนวณด้วยภาษา มีคำกี่คำใน text2 มีคำที่แตกต่างกันมากน้อยเพียงใด 9788 เปรียบเทียบคะแนนความหลากหลายศัพท์เกี่ยวกับเรื่องตลกและนิยายรักใน 1.1 ประเภทใดที่มีความหลากหลายทางลีลามากขึ้น 9788 สร้างพล็อตการกระจายตัวของตัวเอกหลักทั้งสี่ในความรู้สึกและความรู้สึก Elinor, Marianne, Edward และ Willoughby สิ่งที่คุณสามารถสังเกตเกี่ยวกับบทบาทที่แตกต่างกันที่เล่นโดยเพศชายและหญิงในนวนิยายเรื่องนี้คุณสามารถระบุคู่ 9788 ค้นหา collocations ใน text5 9788 พิจารณานิพจน์ Python ต่อไปนี้: len (set (text4)). ระบุวัตถุประสงค์ของการแสดงออกนี้ อธิบายสองขั้นตอนที่เกี่ยวข้องในการคำนวณนี้ 9788 ตรวจทาน 2 รายการและสตริง กำหนดสตริงและกำหนดให้กับตัวแปรเช่น mystring My String (แต่ใส่บางสิ่งที่น่าสนใจขึ้นในสตริง) พิมพ์เนื้อหาของตัวแปรนี้ในสองวิธีแรกโดยพิมพ์ชื่อตัวแปรและกด enter จากนั้นให้ใช้คำสั่งพิมพ์ ลองเพิ่มสตริงให้ตัวเองโดยใช้ mystring mystring หรือคูณด้วยหมายเลขเช่น mystring 3 สังเกตว่าสตริงจะรวมกันโดยไม่มีช่องว่างใด ๆ คุณสามารถแก้ไขปัญหานี้ได้อย่างไร 9788 กำหนดตัวแปร mysent ให้เป็นรายการของคำโดยใช้ syntax mysent quotemyMyquot quotsentquot (แต่ด้วยคำพูดของคุณเองหรือคำพูดที่ชอบ) ใช้ JOIN (mysent) เพื่อแปลงเป็นสตริง ใช้ split () เพื่อแยกสตริงกลับเป็นรูปแบบรายการที่คุณต้องเริ่มต้น 9788 กำหนดตัวแปรหลายตัวที่ประกอบด้วยรายการของคำเช่น วลี 1 วลี 2 และอื่น ๆ เข้าร่วมกันในชุดค่าผสมต่างๆ (ใช้ตัวดำเนินการบวก) เพื่อสร้างประโยคทั้งหมด ความสัมพันธ์ระหว่าง len (วลี 1 วลี 2) และ len (phrase1) len (วลี2) 9788 พิจารณาสองสำนวนต่อไปนี้ซึ่งมีค่าเท่ากัน Which one will typically be more relevant in NLP Why 9788 We have seen how to represent a sentence as a list of words, where each word is a sequence of characters. What does sent122 do Why Experiment with other index values. 9788 The first sentence of text3 is provided to you in the variable sent3 . The index of the in sent3 is 1, because sent31 gives us the . What are the indexes of the two other occurrences of this word in sent3 9788 Review the discussion of conditionals in 4. Find all words in the Chat Corpus ( text5 ) starting with the letter b. Show them in alphabetical order. 9788 Type the expression list(range(10)) at the interpreter prompt. Now try list(range(10, 20)) . list(range(10, 20, 2)) . and list(range(20, 10, -2)) . We will see a variety of uses for this built-in function in later chapters. 9681 Use text9.index() to find the index of the word sunset. Youll need to insert this word as an argument between the parentheses. By a process of trial and error, find the slice for the complete sentence that contains this word. 9681 Using list addition, and the set and sorted operations, compute the vocabulary of the sentences sent1 . sent8 . 9681 What is the difference between the following two lines Which one will give a larger value Will this be the case for other texts Docutils System Messages1 Language Processing and Python It is easy to get our hands on millions of words of text. What can we do with it, assuming we can write some simple programs In this chapter well address the following questions: What can we achieve by combining simple programming techniques with large quantities of text How can we automatically extract key words and phrases that sum up the style and content of a text What tools and techniques does the Python programming language provide for such work What are some of the interesting challenges of natural language processing This chapter is divided into sections that skip between two quite different styles. In the quotcomputing with languagequot sections we will take on some linguistically motivated programming tasks without necessarily explaining how they work. In the quotcloser look at Pythonquot sections we will systematically review key programming concepts. Well flag the two styles in the section titles, but later chapters will mix both styles without being so up-front about it. We hope this style of introduction gives you an authentic taste of what will come later, while covering a range of elementary concepts in linguistics and computer science. If you have basic familiarity with both areas, you can skip to 1.5 we will repeat any important points in later chapters, and if you miss anything you can easily consult the online reference material at nltk. org . If the material is completely new to you, this chapter will raise more questions than it answers, questions that are addressed in the rest of this book. 1.1 Computing with Language: Texts and Words Were all very familiar with text, since we read and write it every day. Here we will treat text as raw data for the programs we write, programs that manipulate and analyze it in a variety of interesting ways. But before we can do this, we have to get started with the Python interpreter. Getting Started with Python One of the friendly things about Python is that it allows you to type directly into the interactive interpreter 8212 the program that will be running your Python programs. You can access the Python interpreter using a simple graphical interface called the Interactive DeveLopment Environment (IDLE). On a Mac you can find this under Applications 8594 MacPython . and on Windows under All Programs 8594 Python . Under Unix you can run Python from the shell by typing idle (if this is not installed, try typing python ). The interpreter will print a blurb about your Python version simply check that you are running Python 2.4 or 2.5 (here it is 2.5.1): Your Turn: Try searching for other words to save re-typing, you might be able to use up-arrow, Ctrl-up-arrow or Alt-p to access the previous command and modify the word being searched. You can also try searches on some of the other texts we have included. For example, search Sense and Sensibility for the word affection. using text2.concordance( quotaffectionquot ) . Search the book of Genesis to find out how long some people lived, using text3.concordance( quotlivedquot ) . You could look at text4 . the Inaugural Address Corpus . to see examples of English going back to 1789, and search for words like nation. terror. god to see how these words have been used differently over time. Weve also included text5 . the NPS Chat Corpus . search this for unconventional words like im. ur. ฮ่า ๆ. (Note that this corpus is uncensored) Once youve spent a little while examining these texts, we hope you have a new sense of the richness and diversity of language. In the next chapter you will learn how to access a broader range of text, including text in languages other than English. A concordance permits us to see words in context. For example, we saw that monstrous occurred in contexts such as the pictures and the size. What other words appear in a similar range of contexts We can find out by appending the term similar to the name of the text in question, then inserting the relevant word in parentheses: Observe that we get different results for different texts. Austen uses this word quite differently from Melville for her, monstrous has positive connotations, and sometimes functions as an intensifier like the word very . The term commoncontexts allows us to examine just the contexts that are shared by two or more words, such as monstrous and very. We have to enclose these words by square brackets as well as parentheses, and separate them with a comma: Your Turn: Pick another pair of words and compare their usage in two different texts, using the similar() and commoncontexts() functions. It is one thing to automatically detect that a particular word occurs in a text, and to display some words that appear in the same context. However, we can also determine the location of a word in the text: how many words from the beginning it appears. This positional information can be displayed using a dispersion plot. Each stripe represents an instance of a word, and each row represents the entire text. In 1.2 we see some striking patterns of word usage over the last 220 years (in an artificial text constructed by joining the texts of the Inaugural Address Corpus end-to-end). You can produce this plot as shown below. You might like to try more words (e. g. liberty. constitution ), and different texts. Can you predict the dispersion of a word before you view it As before, take care to get the quotes, commas, brackets and parentheses exactly right. Figure 1.2. Lexical Dispersion Plot for Words in U. S. Presidential Inaugural Addresses: This can be used to investigate changes in language use over time. Important: You need to have Pythons NumPy and Matplotlib packages installed in order to produce the graphical plots used in this book. Please see nltk. org for installation instructions. Now, just for fun, lets try generating some random text in the various styles we have just seen. To do this, we type the name of the text followed by the term generate . (We need to include the parentheses, but theres nothing that goes between them.) Note that the first time you run this command, it is slow because it gathers statistics about word sequences. Each time you run it, you will get different output text. Now try generating random text in the style of an inaugural address or an Internet chat room. Although the text is random, it re-uses common words and phrases from the source text and gives us a sense of its style and content. (What is lacking in this randomly generated text) When generate produces its output, punctuation is split off from the preceding word. While this is not correct formatting for English text, we do it to make clear that words and punctuation are independent of one another. You will learn more about this in 3 . Counting Vocabulary The most obvious fact about texts that emerges from the preceding examples is that they differ in the vocabulary they use. In this section we will see how to use the computer to count the words in a text in a variety of useful ways. As before, you will jump right in and experiment with the Python interpreter, even though you may not have studied Python systematically yet. Test your understanding by modifying the examples, and trying the exercises at the end of the chapter. Lets begin by finding out the length of a text from start to finish, in terms of the words and punctuation symbols that appear. We use the term len to get the length of something, which well apply here to the book of Genesis: So Genesis has 44,764 words and punctuation symbols, or quottokens. quot A token is the technical name for a sequence of characters 8212 such as hairy . his . or :) 8212 that we want to treat as a group. When we count the number of tokens in a text, say, the phrase to be or not to be. we are counting occurrences of these sequences. Thus, in our example phrase there are two occurrences of to. two of be. and one each of or and not. But there are only four distinct vocabulary items in this phrase. How many distinct words does the book of Genesis contain To work this out in Python, we have to pose the question slightly differently. The vocabulary of a text is just the set of tokens that it uses, since in a set, all duplicates are collapsed together. In Python we can obtain the vocabulary items of text3 with the command: set(text3) . When you do this, many screens of words will fly past. Now try the following: By wrapping sorted() around the Python expression set(text3) . we obtain a sorted list of vocabulary items, beginning with various punctuation symbols and continuing with words starting with A. All capitalized words precede lowercase words. We discover the size of the vocabulary indirectly, by asking for the number of items in the set, and again we can use len to obtain this number . Although it has 44,764 tokens, this book has only 2,789 distinct words, or quotword types. quot A word type is the form or spelling of the word independently of its specific occurrences in a text 8212 that is, the word considered as a unique item of vocabulary. Our count of 2,789 items will include punctuation symbols, so we will generally call these unique items types instead of word types. Now, lets calculate a measure of the lexical richness of the text. The next example shows us that each word is used 16 times on average (we need to make sure Python uses floating point division): Notice that our indexes start from zero: sent element zero, written sent0 . is the first word, word1 . whereas sent element 9 is word10 . The reason is simple: the moment Python accesses the content of a list from the computers memory, it is already at the first element we have to tell it how many elements forward to go. Thus, zero steps forward leaves it at the first element. This practice of counting from zero is initially confusing, but typical of modern programming languages. Youll quickly get the hang of it if youve mastered the system of counting centuries where 19XY is a year in the 20th century, or if you live in a country where the floors of a building are numbered from 1, and so walking up n-1 flights of stairs takes you to level n . Now, if we accidentally use an index that is too large, we get an error: This time it is not a syntax error, because the program fragment is syntactically correct. Instead, it is a runtime error. and it produces a Traceback message that shows the context of the error, followed by the name of the error, IndexError . and a brief explanation. Lets take a closer look at slicing, using our artificial sentence again. Here we verify that the slice 5:8 includes sent elements at indexes 5, 6, and 7: We can modify an element of a list by assigning to one of its index values. In the next example, we put sent0 on the left of the equals sign . We can also replace an entire slice with new material . A consequence of this last change is that the list only has four elements, and accessing a later value generates an error . Your Turn: Take a few minutes to define a sentence of your own and modify individual words and groups of words (slices) using the same methods used earlier. Check your understanding by trying the exercises on lists at the end of this chapter. From the start of 1.1. you have had access to texts called text1 . text2 . และอื่น ๆ It saved a lot of typing to be able to refer to a 250,000-word book with a short name like this In general, we can make up names for anything we care to calculate. We did this ourselves in the previous sections, e. g. defining a variable sent1 . as follows: Such lines have the form: variable expression . Python will evaluate the expression, and save its result to the variable. This process is called assignment. It does not generate any output you have to type the variable on a line of its own to inspect its contents. The equals sign is slightly misleading, since information is moving from the right side to the left. It might help to think of it as a left-arrow. The name of the variable can be anything you like, e. g. mysent . sentence . xyzzy . It must start with a letter, and can include numbers and underscores. Here are some examples of variables and assignments: Remember that capitalized words appear before lowercase words in sorted lists. Notice in the previous example that we split the definition of mysent over two lines. Python expressions can be split across multiple lines, so long as this happens within any kind of brackets. Python uses the quot . quot prompt to indicate that more input is expected. It doesnt matter how much indentation is used in these continuation lines, but some indentation usually makes them easier to read. It is good to choose meaningful variable names to remind you 8212 and to help anyone else who reads your Python code 8212 what your code is meant to do. Python does not try to make sense of the names it blindly follows your instructions, and does not object if you do something confusing, such as one two or two 3 . The only restriction is that a variable name cannot be any of Pythons reserved words, such as def . if . not . and import . If you use a reserved word, Python will produce a syntax error: Take care with your choice of names (or identifiers ) for Python variables. First, you should start the name with a letter, optionally followed by digits ( 0 to 9 ) or letters. Thus, abc23 is fine, but 23abc will cause a syntax error. Names are case-sensitive, which means that myVar and myvar are distinct variables. Variable names cannot contain whitespace, but you can separate words using an underscore, e. g. myvar . Be careful not to insert a hyphen instead of an underscore: my-var is wrong, since Python interprets the quot - quot as a minus sign. Some of the methods we used to access the elements of a list also work with individual words, or strings. For example, we can assign a string to a variable . index a string . and slice a string : We can also perform multiplication and addition with strings: We can join the words of a list to make a single string, or split a string into a list, as follows: We will come back to the topic of strings in 3. For the time being, we have two important building blocks 8212 lists and strings 8212 and are ready to get back to some language analysis. 1.3 Computing with Language: Simple Statistics Lets return to our exploration of the ways we can bring our computational resources to bear on large quantities of text. We began this discussion in 1.1. and saw how to search for words in context, how to compile the vocabulary of a text, how to generate random text in the same style, and so on. In this section we pick up the question of what makes a text distinct, and use automatic methods to find characteristic words and expressions of a text. As in 1.1. you can try new features of the Python language by copying them into the interpreter, and youll learn about these features systematically in the following section. Before continuing further, you might like to check your understanding of the last section by predicting the output of the following code. You can use the interpreter to check whether you got it right. If youre not sure how to do this task, it would be a good idea to review the previous section before continuing further. Frequency Distributions How can we automatically identify the words of a text that are most informative about the topic and genre of the text Imagine how you might go about finding the 50 most frequent words of a book. One method would be to keep a tally for each vocabulary item, like that shown in 1.3. The tally would need thousands of rows, and it would be an exceedingly laborious process 8212 so laborious that we would rather assign the task to a machine. Figure 1.3. Counting Words Appearing in a Text (a frequency distribution) The table in 1.3 is known as a frequency distribution. and it tells us the frequency of each vocabulary item in the text. (In general, it could count any kind of observable event.) It is a quotdistributionquot because it tells us how the total number of word tokens in the text are distributed across the vocabulary items. Since we often need frequency distributions in language processing, NLTK provides built-in support for them. Lets use a FreqDist to find the 50 most frequent words of Moby Dick . Try to work out what is going on here, then read the explanation that follows. When we first invoke FreqDist . we pass the name of the text as an argument . We can inspect the total number of words (quotoutcomesquot) that have been counted up 8212 260,819 in the case of Moby Dick . The expression keys () gives us a list of all the distinct types in the text . and we can look at the first 50 of these by slicing the list . Your Turn: Try the preceding frequency distribution example for yourself, for text2 . Be careful to use the correct parentheses and uppercase letters. If you get an error message NameError: name FreqDist is not defined . you need to start your work with from nltk. book import Do any words produced in the last example help us grasp the topic or genre of this text Only one word, whale. is slightly informative It occurs over 900 times. The rest of the words tell us nothing about the text theyre just English quotplumbing. quot What proportion of the text is taken up with such words We can generate a cumulative frequency plot for these words, using fdist1.plot(50, cumulativeTrue) . to produce the graph in 1.4. These 50 words account for nearly half the book Figure 1.4. Cumulative Frequency Plot for 50 Most Frequently Words in Moby Dick . these account for nearly half of the tokens. If the frequent words dont help us, how about the words that occur once only, the so-called hapaxes. View them by typing fdist1.hapaxes() . This list contains lexicographer. cetological. contraband. expostulations. and about 9,000 others. It seems that there are too many rare words, and without seeing the context we probably cant guess what half of the hapaxes mean in any case Since neither frequent nor infrequent words help, we need to try something else. Fine-grained Selection of Words Next, lets look at the long words of a text perhaps these will be more characteristic and informative. For this we adapt some notation from set theory. We would like to find the words from the vocabulary of the text that are more than 15 characters long. Lets call this property P. so that P(w) is true if and only if w is more than 15 characters long. Now we can express the words of interest using mathematical set notation as shown in (1a). This means quotthe set of all w such that w is an element of V (the vocabulary) and w has property P quot. From this we see that the most frequent word length is 3, and that words of length 3 account for roughly 50,000 (or 20) of the words making up the book. Although we will not pursue it here, further analysis of word length might help us understand differences between authors, genres, or languages. 1.2 summarizes the functions defined in frequency distributions. These expressions have the form f(w) for. or w. f() for. . where f is a function that operates on a word to compute its length, or to convert it to uppercase. For now, you dont need to understand the difference between the notations f(w) and w. f() . Instead, simply learn this Python idiom which performs the same operation on every element of a list. In the preceding examples, it goes through each word in text1 . assigning each one in turn to the variable w and performing the specified operation on the variable. The notation just described is called a quotlist comprehension. quot This is our first example of a Python idiom, a fixed notation that we use habitually without bothering to analyze each time. Mastering such idioms is an important part of becoming a fluent Python programmer. Lets return to the question of vocabulary size, and apply the same idiom here: Now that we are not double-counting words like This and this. which differ only in capitalization, weve wiped 2,000 off the vocabulary count We can go a step further and eliminate numbers and punctuation from the vocabulary count by filtering out any non-alphabetic items: This example is slightly complicated: it lowercases all the purely alphabetic items. Perhaps it would have been simpler just to count the lowercase-only items, but this gives the wrong answer (why). Dont worry if you dont feel confident with list comprehensions yet, since youll see many more examples along with explanations in the following chapters. Nested Code Blocks Most programming languages permit us to execute a block of code when a conditional expression. or if statement, is satisfied. We already saw examples of conditional tests in code like w for w in sent7 if len(w) lt 4 . In the following program, we have created a variable called word containing the string value cat . The if statement checks whether the test len(word) lt 5 is true. It is, so the body of the if statement is invoked and the print statement is executed, displaying a message to the user. Remember to indent the print statement by typing four spaces. This is called a loop because Python executes the code in circular fashion. It starts by performing the assignment word Call . effectively using the word variable to name the first item of the list. Then, it displays the value of word to the user. Next, it goes back to the for statement, and performs the assignment word me . before displaying this new value to the user, and so on. It continues in this fashion until every item of the list has been processed. Looping with Conditions Now we can combine the if and for statements. We will loop over every item of the list, and print the item only if it ends with the letter l . Well pick another name for the variable to demonstrate that Python doesnt try to make sense of variable names. You will notice that if and for statements have a colon at the end of the line, before the indentation begins. In fact, all Python control structures end with a colon. The colon indicates that the current statement relates to the indented block that follows. We can also specify an action to be taken if the condition of the if statement is not met. Here we see the elif (else if) statement, and the else statement. Notice that these also have colons before the indented code. 1.5 Automatic Natural Language Understanding We have been exploring language bottom-up, with the help of texts and the Python programming language. However, were also interested in exploiting our knowledge of language and computation by building useful language technologies. Well take the opportunity now to step back from the nitty-gritty of code in order to paint a bigger picture of natural language processing. At a purely practical level, we all need help to navigate the universe of information locked up in text on the Web. Search engines have been crucial to the growth and popularity of the Web, but have some shortcomings. It takes skill, knowledge, and some luck, to extract answers to such questions as: What tourist sites can I visit between Philadelphia and Pittsburgh on a limited budget What do experts say about digital SLR cameras What predictions about the steel market were made by credible commentators in the past week Getting a computer to answer them automatically involves a range of language processing tasks, including information extraction, inference, and summarization, and would need to be carried out on a scale and with a level of robustness that is still beyond our current capabilities. On a more philosophical level, a long-standing challenge within artificial intelligence has been to build intelligent machines, and a major part of intelligent behaviour is understanding language. For many years this goal has been seen as too difficult. However, as NLP technologies become more mature, and robust methods for analyzing unrestricted text become more widespread, the prospect of natural language understanding has re-emerged as a plausible goal. In this section we describe some language understanding technologies, to give you a sense of the interesting challenges that are waiting for you. Word Sense Disambiguation In word sense disambiguation we want to work out which sense of a word was intended in a given context. Consider the ambiguous words serve and dish : serve. help with food or drink hold an office put ball into play dish. plate course of a meal communications device In a sentence containing the phrase: he served the dish. you can detect that both serve and dish are being used with their food meanings. Its unlikely that the topic of discussion shifted from sports to crockery in the space of three words. This would force you to invent bizarre images, like a tennis pro taking out their frustrations on a china tea-set laid out beside the court. In other words, we automatically disambiguate words using context, exploiting the simple fact that nearby words have closely related meanings. As another example of this contextual effect, consider the word by. which has several meanings, e. g. the book by Chesterton (agentive 8212 Chesterton was the author of the book) the cup by the stove (locative 8212 the stove is where the cup is) and submit by Friday (temporal 8212 Friday is the time of the submitting). Observe in (3c) that the meaning of the italicized word helps us interpret the meaning of by . The lost children were found by the searchers (agentive) The lost children were found by the mountain (locative) The lost children were found by the afternoon (temporal) Pronoun Resolution A deeper kind of language understanding is to work out quotwho did what to whomquot 8212 i. e. to detect the subjects and objects of verbs. You learnt to do this in elementary school, but its harder than you might think. In the sentence the thieves stole the paintings it is easy to tell who performed the stealing action. Consider three possible following sentences in (4c). and try to determine what was sold, caught, and found (one case is ambiguous). The thieves stole the paintings. They were subsequently sold . The thieves stole the paintings. They were subsequently caught . The thieves stole the paintings. They were subsequently found . Answering this question involves finding the antecedent of the pronoun they. either thieves or paintings. Computational techniques for tackling this problem include anaphora resolution 8212 identifying what a pronoun or noun phrase refers to 8212 and semantic role labeling 8212 identifying how a noun phrase relates to the verb (as agent, patient, instrument, and so on). Generating Language Output If we can automatically solve such problems of language understanding, we will be able to move on to tasks that involve generating language output, such as question answering and machine translation. In the first case, a machine should be able to answer a users questions relating to collection of texts: In order to determine whether the hypothesis is supported by the text, the system needs the following background knowledge: (i) if someone is an author of a book, then heshe has written that book (ii) if someone is an editor of a book, then heshe has not written (all of) that book (iii) if someone is editor or author of eighteen books, then one cannot conclude that heshe is author of eighteen books. Limitations of NLP Despite the research-led advances in tasks like RTE, natural language systems that have been deployed for real-world applications still cannot perform common-sense reasoning or draw on world knowledge in a general and robust manner. We can wait for these difficult artificial intelligence problems to be solved, but in the meantime it is necessary to live with some severe limitations on the reasoning and knowledge capabilities of natural language systems. Accordingly, right from the beginning, an important goal of NLP research has been to make progress on the difficult task of building technologies that quotunderstand language, quot using superficial yet powerful techniques instead of unrestricted knowledge and reasoning capabilities. Indeed, this is one of the goals of this book, and we hope to equip you with the knowledge and skills to build useful NLP systems, and to contribute to the long-term aspiration of building intelligent machines. 1.6 Summary Texts are represented in Python using lists: Monty. Python . We can use indexing, slicing, and the len() function on lists. A word quottokenquot is a particular appearance of a given word in a text a word quottypequot is the unique form of the word as a particular sequence of letters. We count word tokens using len(text) and word types using len(set(text)) . We obtain the vocabulary of a text t using sorted(set(t)) . We operate on each item of a text using f(x) for x in text . To derive the vocabulary, collapsing case distinctions and ignoring punctuation, we can write set(w. lower() for w in text if w. isalpha()) . We process each word in a text using a for statement, such as for w in t: or for word in text: . This must be followed by the colon character and an indented block of code, to be executed each time through the loop. We test a condition using an if statement: if len(word) lt 5: . This must be followed by the colon character and an indented block of code, to be executed only if the condition is true. A frequency distribution is a collection of items along with their frequency counts (e. g. the words of a text and their frequency of appearance). A function is a block of code that has been assigned a name and can be reused. Functions are defined using the def keyword, as in def mult (x, y) x and y are parameters of the function, and act as placeholders for actual data values. A function is called by specifying its name followed by one or more arguments inside parentheses, like this: mult(3, 4) . เช่น. len(text1) . 1.7 Further Reading This chapter has introduced new concepts in programming, natural language processing, and linguistics, all mixed in together. Many of them are consolidated in the following chapters. However, you may also want to consult the online materials provided with this chapter (at nltk. org ), including links to additional background materials, and links to online NLP systems. You may also like to read up on some linguistics and NLP-related concepts in Wikipedia (e. g. collocations, the Turing Test, the type-token distinction). You should acquaint yourself with the Python documentation available at docs. python. org . including the many tutorials and comprehensive reference materials linked there. A Beginners Guide to Python is available at wiki. python. orgmoinBeginnersGuide . Miscellaneous questions about Python might be answered in the FAQ at python. orgdocfaqgeneral . As you delve into NLTK, you might want to subscribe to the mailing list where new releases of the toolkit are announced. There is also an NLTK-Users mailing list, where users help each other as they learn how to use Python and NLTK for language analysis work. Details of these lists are available at nltk. org . For more information on the topics covered in 1.5. and on NLP more generally, you might like to consult one of the following excellent books: Indurkhya, Nitin and Fred Damerau (eds, 2010) Handbook of Natural Language Processing (Second Edition) Chapman amp HallCRC. 2010. (Indurkhya amp Damerau, 2010) (Dale, Moisl, amp Somers, 2000) Jurafsky, Daniel and James Martin (2008) Speech and Language Processing (Second Edition). Prentice Hall. (Jurafsky amp Martin, 2008) Mitkov, Ruslan (ed, 2003) The Oxford Handbook of Computational Linguistics . Oxford University Press. (second edition expected in 2010). (Mitkov, 2002) The Association for Computational Linguistics is the international organization that represents the field of NLP. The ACL website ( aclweb. org ) hosts many useful resources, including: information about international and regional conferences and workshops the ACL Wiki with links to hundreds of useful resources and the ACL Anthology. which contains most of the NLP research literature from the past 50 years, fully indexed and freely downloadable. Some excellent introductory Linguistics textbooks are: (Finegan, 2007). (OGrady et al, 2004). (OSU, 2007). You might like to consult LanguageLog. a popular linguistics blog with occasional posts that use the techniques described in this book. 1.8 Exercises 9788 Try using the Python interpreter as a calculator, and typing expressions like 12 (4 1) . 9788 Given an alphabet of 26 letters, there are 26 to the power 10, or 26 10 . ten-letter strings we can form. That works out to 141167095653376L (the L at the end just indicates that this is Pythons long-number format). How many hundred-letter strings are possible 9788 The Python multiplication operation can be applied to lists. What happens when you type Monty. Python 20 . or 3 sent1 9788 Review 1.1 on computing with language. How many words are there in text2 . How many distinct words are there 9788 Compare the lexical diversity scores for humor and romance fiction in 1.1. Which genre is more lexically diverse 9788 Produce a dispersion plot of the four main protagonists in Sense and Sensibility . Elinor, Marianne, Edward, and Willoughby. What can you observe about the different roles played by the males and females in this novel Can you identify the couples 9788 Find the collocations in text5 . 9788 Consider the following Python expression: len(set(text4)) . State the purpose of this expression. Describe the two steps involved in performing this computation. 9788 Review 1.2 on lists and strings. Define a string and assign it to a variable, e. g. mystring My String (but put something more interesting in the string). Print the contents of this variable in two ways, first by simply typing the variable name and pressing enter, then by using the print statement. Try adding the string to itself using mystring mystring . or multiplying it by a number, e. g. mystring 3 . Notice that the strings are joined together without any spaces. How could you fix this 9788 Define a variable mysent to be a list of words, using the syntax mysent quotMyquot. quotsentquot (but with your own words, or a favorite saying). Use. join(mysent) to convert this into a string. Use split() to split the string back into the list form you had to start with. 9788 Define several variables containing lists of words, e. g. phrase1 . phrase2 . และอื่น ๆ Join them together in various combinations (using the plus operator) to form whole sentences. What is the relationship between len(phrase1 phrase2) and len(phrase1) len(phrase2) 9788 Consider the following two expressions, which have the same value. Which one will typically be more relevant in NLP Why 9788 We have seen how to represent a sentence as a list of words, where each word is a sequence of characters. What does sent122 do Why Experiment with other index values. 9788 The first sentence of text3 is provided to you in the variable sent3 . The index of the in sent3 is 1, because sent31 gives us the . What are the indexes of the two other occurrences of this word in sent3 9788 Review the discussion of conditionals in 1.4. Find all words in the Chat Corpus ( text5 ) starting with the letter b. Show them in alphabetical order. 9788 Type the expression range(10) at the interpreter prompt. Now try range(10, 20) . range(10, 20, 2) . and range(20, 10, -2) . We will see a variety of uses for this built-in function in later chapters. 9681 Use text9.index() to find the index of the word sunset. Youll need to insert this word as an argument between the parentheses. By a process of trial and error, find the slice for the complete sentence that contains this word. 9681 Using list addition, and the set and sorted operations, compute the vocabulary of the sentences sent1 . sent8 . 9681 What is the difference between the following two lines Which one will give a larger value Will this be the case for other texts
Comments
Post a Comment