ไม่มีใครรู้ว่ามีโมดูลที่มีอยู่หรือวิธีการง่ายๆ ในการอ่านและเขียนประโยคที่แท็กส่วนของคำพูดเข้าและออกจากไฟล์ข้อความหรือไม่? ฉันใช้ python และ Natural Language Toolkit (NLTK) ตัวอย่างเช่น รหัสนี้:
import nltk
sentences = "Call me Ishmael. Some years ago - never mind how long precisely - having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world."
tagged = nltk.sent_tokenize(sentences.strip())
tagged = [nltk.word_tokenize(sent) for sent in tagged]
tagged = [nltk.pos_tag(sent) for sent in tagged]
print tagged
ส่งคืนรายการที่ซ้อนกันนี้:
[[('Call', 'NNP'), ('me', 'PRP'), ('Ishmael', 'NNP'), ('.', '.')], [('Some', 'DT'), ('years', 'NNS'), ('ago', 'RB'), ('-', ':'), ('never', 'RB'), ('mind', 'VBP'), ('how', 'WRB'), ('long', 'JJ'), ('precisely', 'RB'), ('-', ':'), ('having', 'VBG'), ('little', 'RB'), ('or', 'CC'), ('no', 'DT'), ('money', 'NN'), ('in', 'IN'), ('my', 'PRP$'), ('purse', 'NN'), (',', ','), ('and', 'CC'), ('nothing', 'NN'), ('particular', 'JJ'), ('to', 'TO'), ('interest', 'NN'), ('me', 'PRP'), ('on', 'IN'), ('shore', 'NN'), (',', ','), ('I', 'PRP'), ('thought', 'VBD'), ('I', 'PRP'), ('would', 'MD'), ('sail', 'VB'), ('about', 'IN'), ('a', 'DT'), ('little', 'RB'), ('and', 'CC'), ('see', 'VB'), ('the', 'DT'), ('watery', 'NN'), ('part', 'NN'), ('of', 'IN'), ('the', 'DT'), ('world', 'NN'), ('.', '.')]]
ฉันรู้ว่าฉันสามารถทิ้งสิ่งนี้ลงในผักดองได้อย่างง่ายดาย แต่ฉันต้องการส่งออกเป็นส่วนของไฟล์ข้อความที่มีขนาดใหญ่กว่า ฉันต้องการส่งออกรายการเป็นไฟล์ข้อความ จากนั้นกลับมาที่รายการในภายหลัง แยกวิเคราะห์ และกู้คืนโครงสร้างรายการดั้งเดิม มีฟังก์ชันในตัวใน NLTK สำหรับการทำเช่นนี้หรือไม่ ฉันค้นหาแล้ว แต่ไม่พบเลย...
ตัวอย่างผลลัพธ์:
<headline>Article headline</headline>
<body>Call me Ishmael...</body>
<pos_tags>[[('Call', 'NNP'), ('me', 'PRP'), ('Ishmael', 'NNP')...</pos_tags>