неділю, 29 листопада 2009 р.

Задача №6 Лабораторна робота №5

Прочитайте деякий текст з корпуса, здійсніть його токенізацію і збережіть у список всі wh-слова, які в ньому зустрічаються.

По пятій беріть будь-який файл *.html який є у вас вказуйте шлях до нього при виконанні
завдань.
Дивно, що навіть шоста задача викликає проблеми (дивіться рисунок1 і повторюйте ).
import nltk
from nltk.corpus import gutenberg
gutenberg.fileids()
['austen-emma.txt', 'austen-persuasion.txt', 'austen-sense.txt', 'bible-kjv.txt', 'blake-poems.txt', 'bryant-stories.txt', 'burgess-busterbrown.txt', 'carroll-alice.txt',
'chesterton-ball.txt', 'chesterton-brown.txt', 'chesterton-thursday.txt',
'edgeworth-parents.txt', 'melville-moby_dick.txt', 'milton-paradise.txt',
'shakespeare-caesar.txt', 'shakespeare-hamlet.txt', 'shakespeare-macbeth.txt',
'whitman-leaves.txt']
raw=gutenberg.raw('chesterton-ball.txt') #стрічка символів з тексту
len(raw)
457450
tokens1 = nltk.word_tokenize(raw) # токенізація тексту (виділені слова)
len(tokens1)
93663
tokens1[:100]
['[', 'The', 'Ball', 'and', 'The', 'Cross', 'by', 'G.K.', 'Chesterton', '1909', ']', 'I.', 'A', 'DISCUSSION', 'SOMEWHAT', 'IN', 'THE', 'AIR', 'The', 'flying', 'ship', 'of',
'Professor', 'Lucifer', 'sang', 'through', 'the', 'skies', 'like', 'a', 'silver',
'arrow', ';', 'the', 'bleak', 'white', 'steel', 'of', 'it', ',', 'gleaming', 'in', 'the',
'bleak', 'blue', 'emptiness', 'of', 'the', 'evening.', 'That', 'it', 'was', 'far',
'above', 'the', 'earth', 'was', 'no', 'expression', 'for', 'it', ';', 'to', 'the', 'two',
'men', 'in', 'it', ',', 'it', 'seemed', 'to', 'be', 'far', 'above', 'the', 'stars.',
'The', 'professor', 'had', 'himself', 'invented', 'the', 'flying', 'machine', ',', 'and',
'had', 'also', 'invented', 'nearly', 'everything', 'in', 'it.', 'Every', 'sort', 'of',
'tool', 'or', 'apparatus']
tokens2 = nltk.wordpunct_tokenize(raw) # токенізація тексту (виділені слова і
розділові знаки)
len(tokens2)
96996
tokens2[:100]
['[', 'The', 'Ball', 'and', 'The', 'Cross', 'by', 'G', '.', 'K', '.', 'Chesterton', '1909', ']', 'I', '.', 'A', 'DISCUSSION', 'SOMEWHAT', 'IN', 'THE', 'AIR', 'The',
'flying', 'ship', 'of', 'Professor', 'Lucifer', 'sang', 'through', 'the', 'skies',
'like', 'a', 'silver', 'arrow', ';', 'the', 'bleak', 'white', 'steel', 'of', 'it', ',',
'gleaming', 'in', 'the', 'bleak', 'blue', 'emptiness', 'of', 'the', 'evening', '.',
'That', 'it', 'was', 'far', 'above', 'the', 'earth', 'was', 'no', 'expression', 'for',
'it', ';', 'to', 'the', 'two', 'men', 'in', 'it', ',', 'it', 'seemed', 'to', 'be', 'far',
'above', 'the', 'stars', '.', 'The', 'professor', 'had', 'himself', 'invented', 'the',
'flying', 'machine', ',', 'and', 'had', 'also', 'invented', 'nearly', 'everything', 'in',
'it']
text1=nltk.Text(tokens1) # перетворили в текст NLTK
text2=nltk.Text(tokens2) # перетворили в текст NLTK
words1=[w.lower() for w in text1]
words2=[w.lower() for w in text2]
len(words1)
93663
len(words2)
96996
wh_words1=[w.lower() for w in text1 if w.startswith('wh')] # список wh - слів
wh_words2=[w.lower() for w in text2 if w.startswith('wh')] # список wh - слів
len(wh_words1) # сподіваємось, що отримали однакові списки
1213
len(wh_words2) # наші сподівання не справдилися
1227
[w for w in wh_words2 if w not in wh_words1] # можна побавитись, щоб знайти причину,
що списки не є однакові
['wheel', 'whirlpool']

Немає коментарів:

Дописати коментар