Part of Speech Basic

Pada lesson part of speech (POS) basic, kita akan membahas lebih jauh mengenai POS pada spaCy.

Kata dalam sebuah doc object, bisa memiliki arti yang berbeda berdasarkan konteks kalimat. Oleh karena itu kita harus melihat part of speech dari kata tersebut, bukan hanya arti dari kata tersebut.

spaCy memiliki 2 jenis POS

  • POS, POS hanya menampilkan POS standar seperti: noun, verb atau adj
  • TAG, TAG menampilkan POS detail, seperti: plural noun, past-tense verb, superlative-adjective.
import spacy
nlp = spacy.load('en_core_web_sm')

doc = nlp(u"The quick brown fox jumped over the lazy dog's back.")

for token in doc:
    print(f'{token.text:{10}} {token.pos_:{8}} {token.tag_:{6}} {spacy.explain(token.tag_)}')
The        DET      DT     determiner
quick      ADJ      JJ     adjective
brown      ADJ      JJ     adjective
fox        NOUN     NN     noun, singular or mass
jumped     VERB     VBD    verb, past tense
over       ADP      IN     conjunction, subordinating or preposition
the        DET      DT     determiner
lazy       ADJ      JJ     adjective
dog        NOUN     NN     noun, singular or mass
's         PART     POS    possessive ending
back       NOUN     NN     noun, singular or mass
.          PUNCT    .      punctuation mark, sentence closer

Note, untuk mendapatkan info detail dari object spaCy, gunakan perintah spacy.explain, contoh spacy.explain(token.tag_) yang digunakan pada kode diatas.

Dalam bahasa Inggris, kata yang sama dapat memiliki arti yang berbeda, bahkan dalam kalimat yang sama. Untuk alasan ini, morfologi penting. spaCy menggunakan algoritma pembelajaran mesin untuk memprediksi penggunaan token dalam kalimat.

doc = nlp(u'I read books on NLP.')
r = doc[1]

print(f'{r.text:{10}} {r.pos_:{8}} {r.tag_:{6}} {spacy.explain(r.tag_)}')
read       VERB     VBP    verb, non-3rd person singular present
doc = nlp(u'I read a book on NLP.')
r = doc[1]

print(f'{r.text:{10}} {r.pos_:{8}} {r.tag_:{6}} {spacy.explain(r.tag_)}')
read       VERB     VBP    verb, past tense

Menghitung Jumlah POS dan TAG

Perintah Doc.count_by() menerima argument spesifik token attribute, dan mengembalikan nilai dalam dictionary.

Keys pada dictionary adalah nilai integer dari attribute ID, dan values adalah jumlah dari attribute tersebut.

doc = nlp(u"The quick brown fox jumped over the lazy dog's back.")

# Count the frequencies of different coarse-grained POS tags:
POS_counts = doc.count_by(spacy.attrs.POS)
POS_counts
{90: 2, 84: 3, 92: 3, 100: 1, 85: 1, 94: 1, 97: 1}

POS_counts diatas tentu kurang intuitif. Kita gunakan doc.vocab[] untuk mendapatkan informasi dari nilai key pada dictionary diatas.

for k,v in sorted(POS_counts.items()):
    print(f'{k}. {doc.vocab[k].text:{5}}: {v}')
84. ADJ  : 3
85. ADP  : 1
90. DET  : 2
92. NOUN : 3
94. PART : 1
97. PUNCT: 1
100. VERB : 1

Pada kode diatas adalah untuk menghitung jumlah POS. Untuk menghitung jumlah TAG, kita masih menggunakan perintah Doc.count_by() , hanya argument yang digunakan adalah spacy.attrs.TAG

TAG_counts = doc.count_by(spacy.attrs.TAG)

for k,v in sorted(TAG_counts.items()):
    print(f'{k}. {doc.vocab[k].text:{4}}: {v}')
74. POS : 1
1292078113972184607. IN  : 1
10554686591937588953. JJ  : 3
12646065887601541794. .   : 1
15267657372422890137. DT  : 2
15308085513773655218. NN  : 3
17109001835818727656. VBD : 1

Kita lihat ID numbers sangat besar, pada spaCy, text values tertentu di hardcoded pada Doc.vocab menggunakan ratusan ID numbers pertama. Seperti ‘NOUN’ dan ‘VERB’ sangat sering digunakan dalam operasi spaCy internal. Sedangkan, TAG detail, menggunakan hash value saat dibutuhkan.

Visualisasi POS

Visualisasi adalah untuk menampilkan hubungan antar token dalam format graphic. Untuk menggunakan visualisai, library displacy harus diimport terlebih dahulu.

from spacy import displacy

doc = nlp(u'Find cafe with good wifi.')
displacy.render(doc, style='dep', jupyter=True, options={'distance': 110})
Token visualization

Jika Anda menggunakan jupyter notebook, maka parameter jupyter=True harus ditambahkan pada perintah diplacy.render().

Jika Anda ingin menampilkan hasil render diluar jupyter notebook, gunakan perintah displacy.serve()

displacy.serve(doc, style='dep', options={'distance': 110})
After running the cell above, click the link below to view the dependency parse:

http://127.0.0.1:5000

To shut down the server and return to jupyter, interrupt the kernel either through the Kernel menu above, by hitting the black square on the toolbar, or by typing the keyboard shortcut Esc, I, I

Visualizer Options

Selain parameter distance, masih ada parameter untuk mengatur tampilan visualisasi. Contoh bg untuk mengubah warna background color, atau color untuk mengubah warna font.

Untuk detail options parameter, kunjungi https://spacy.io/api/top-level#displacy_options

Sharing is caring: