Professional Documents
Culture Documents
Which of the following word occurs frequently after the word Holy in text collection
text6?
Grail
What is the output of the following expression?
import nltk
lancaster = nltk.LancasterStemmer()
print(lancaster.stem('power')
Pow
What is the output of the following expression?
import nltk
wnl = nltk.WordNetLemmatizer()
print(wnl.lemmatize('women'))
Woman
What is the output of the following expression?
import nltk
porter = nltk.PorterStemmer()
print(porter.stem('lying'))
Lie
How many words are ending with 'ing' in text collection text6?
What is the output of the following expression?
import nltk
porter = nltk.PorterStemmer()
print(porter.stem('ceremony'))
Ceremoni
Which tag occurs maximum in text collections associated with news genre of brown
corpus?
NN
Is it possible to combine Taggers. State if it is true or false?
True
What is the output of the following expression?
s = 'Python is awesome'
print(nltk.pos_tag(nltk.word_tokenize(s)))
6725
Which of the following function is used to obtain set of all pair of consecutive words
appearing in a text?
Bigrams()
Which of the following function is used to generate a set of all possible n consecutive words
appearing in a text?
Ngrams()
Pyhton 3 Programming
Any Python Script can act like a Module. State if the statement is True or False?
True
How are variable length keyword arguments specified in the function heading?
one underscore followed by a valid identifier
def __str__(self):
return 'A(x: {}, y: {})'.format(self.x, self.y)
def f1():
a = A(12, 3)
b = A(3, 12)
if (a == b):
print(b != a)
print(a)
f1()
False
A(x: 12, y: 3)
Which of the following module is not used for parsing command line arguments automatically?
cmdparse
Which of the following keyword is used for creating a method inside a class ?
def
Which of the following modules contain functions that create iterators for efficient looping?
itertools
Which of the following methods of 'random' module is used to pick a single element, randomly,
from a given list of elements?
choice
Which of the following modules are used to deal with Data compression and archiving?
All of those mentioned
If a list has 5 elements, then which of the following exceptions is raised when 8th element is
accessed?
IndexError
Which methods are defined in an iterator class?
Which of the following expression can be used to check if the file 'C:\Sample.txt' exists and is also a
regular file?
os.path.isfile(C:\Sample.txt)
Which of the following exception occurs, when an integer object is added to a string object?
TypeError
when no exception occursHow are variable length non-keyword arguments specified in the function
heading?
one underscore followed by a valid identifier
class A:
__metaclass__ = B
Which of the following method is used by a user defined class to support '+' operator?
__add__
Which of the following error occurs, if an iterator is accessed, when it has no elements?
@bind
def add(x, y):
return x + y
print(add(3, 10))
print(add.data)
13
9
***
Which of the following is true about decorators ?
Decorators can be chained
def decorator_func(func):
def wrapper(*args, **kwdargs):
return func(*args, **kwdargs)
wrapper.__name__ = func.__name__
return wrapper
@decorator_func
def square(x):
return x**2
print(square.__name__)
square
------------------------------------------------------------------------------------
Python Pandas
Knowing a Series
describe() or info()
It is possible to understand a Series better by using describe method.
The method provides details like mean, std, etc. about a series.
Two methods majorly info and describe can be used to know about the data, present in a
data frame.
describe provides details of only numeric fields.
to_csv()
write a data frame data to an output csv file
read_excel
used to read data from excel files
read_csv
used to read data from csv files
skiprows
method to skip first n lines of an input csv files
parse_dates
method to treat data of specific columns as dates
to_datetime
convert a list of date like strings into datetime objects
Indexing
refers to labeling data elements of a Series, a Data Frame or a Panel
Data Aggregation
refers to identifying data satisfying a condition.
merge
method to join two data frames.
################################################################################
######
#Reading Data from JSON
import pandas as pd
import numpy as np
import json
EmployeeRecords = [{'EmployeeID':451621, 'EmployeeName':'Preeti Jain', 'DOJ':'30-Aug-2008'},
{'EmployeeID':123621, 'EmployeeName':'Ashok Kumar', 'DOJ':'25-Sep-2016'},
{'EmployeeID':451589, 'EmployeeName':'Johnty Rhodes', 'DOJ':'04-Nov-2016'}]
emp_records_json_str = json.dumps(EmployeeRecords)
df = pd.read_json(emp_records_json_str, orient='records', convert_dates=['DOJ'])
print(df)
#END
df = pd.DataFrame(np.random.rand(5,2))
df
################################################################################
####
Which of the following attributre or argument used to set column names of a data frame?
index
What is the output of the expression 'b' in s, where is s is the series defined as shown below?
s = pd.Series([89.2, 76.4, 98.2, 75.9], index=list('abcd'))
True
Which of the following expressions are used to check if each element of a series s is present in the
list of elements [67, 32]. Series s is defined as shown below.
s = pd.Series([99, 32, 67],list('abc'))
s.isin([67, 32])
import pandas as pd
s = pd.Series([9.2, 'hello', 89])
X-float
objetc
What is the output of the expression 'b' in s, where is s is the series defined as shown below?
s = pd.Series([89.2, 76.4, 98.2, 75.9], index=list('abcd'))
print(s[['c', 'a']])
dtype: float64
c 98.20
a 89.72
dtype: float64
Which of the following expression returns last two rows of df, defined below.
df = pd.DataFrame({'A':[34, 78, 54], 'B':[12, 67, 43]}, index=['r1', 'r2', 'r3'])
df.loc['r2':'r3']
Which of the following expression is used to delete the column, A from a data frame named df
del df['A']
Which of the following expression is used to add a new column 'C' to a data frame df, with 3 rows
df['C'] = [12, 98, 45]
What does the expression df.loc['r4'] = [67, 78] do for the data frame df, defined below
df = pd.DataFrame({'A':[34, 78, 54], 'B':[12, 67, 43]}, index=['r1', 'r2', 'r3'])
df.loc['r4'] = [67, 78]
Adds a new row
Which of the following method is used to write a data frame data to an output csv file?
to_csv
Which of the following method is used to read data from excel files ?
read_excel
Which of the following is used as argument of read_csv method to treat data of specific columns as
dates
parse_dates
What is the length of PeriodIndex object created from the expression pd.period_range('11-Sep-
2017', '17-Sep-2017', freq='M')
1
What does the expression d + pd.Timedelta('1 days 2 hours') do to DatetimeIndex object d, defined
below
d = pd.date_range('11-Sep-2017', '17-Sep-2017', freq='2D')
d = d + pd.Timedelta('1 days 2 hours')
X-Results in Error
Increases each datetime value by 1 day and 2 hours
What is the length of DatetimeIndex object created with the below expression?
pd.bdate_range('11-Sep-2017', '17-Sep-2017', freq='2D'
4
Which of the following method is used to eliminate rows with null values?
dropna
Which of the following argument values are allowed for method argument of fillna?
All
Which of the following method of pandas is used to check if each value is a null or not?
isnull()
Consider a data frame df with 10 rows and index [ 'r1', 'r2', 'r3', 'row4', 'row5', 'row6', 'r7', 'r8', 'r9',
'row10']. What does the expression g = df.groupby(df.index.str.len()) do?
Groups df based on lebgth of each index value
Which of the following method can be applied on a groupby object to get the group details
groups
Given a data frane df with columns ['A', 'B', 'C', 'D'] and rows ['r1', 'r2', 'r3'], Which of the following
expression filter those rows whose column B values are greater than 45
X-df.iloc[df.B > 45]
df[df.B > 45]
Which of the following argument is used to set the key to be used for merging two data frames?
X-key
k
Which of the following are allowed values of argument how of merge method?
All
Which of the following argument is used to ignore the index while concatenating two date frames
ignore_index
Given a data frane df with columns ['A', 'B', 'C', 'D'] and rows ['r1', 'r2', 'r3'], Which of the following
expression is used to extract columns 'C' and 'D'
df[:, lambda x : x.columns.isin(['C', 'D'])]
df.loc[:, lambda x : x.columns.isin(['C', 'D'])]
Which argument is used to override the existing column names, while using concat method
keys
Which of the following methods is used to group data of a data frame, based on a specifc columns
groupby
Which of the following expression returns data of column B of data frame df, defined below.
None
What does the expression df.iloc[:, lambda x : [0,3]] do ? Given a data frane df with columns ['A',
'B', 'C', 'D'] and rows ['r1', 'r2', 'r3'].
Selects Column 'A' and 'D'
What does the expression df[lambda x : x.index.str.endswith('3')] do ? Given a data frane df with
columns ['A', 'B', 'C', 'D'] and rows ['r1', 'r2', 'r3']
Filters the row labelled r3
Which of the following is used as argument of read_csv method to make data of specific column as
index?
index_col
State whether the following statement is True or False? read_csv method by default reads all blank
lines of an input csv file.
True
Which of the following expression returns first two rows of df, defined below.
import pandas as pd
###########
import pandas as pd
Which of the following expression returns second row of df, defined below.
import pandas
df.iloc[1]
Consider a data frame df with 10 rows and index [ 'r1', 'r2', 'r3', 'row4', 'row5', 'row6', 'r7', 'r8', 'r9',
'row10']. What does the aggregate method shown in below code do?
g = df.groupby(df.index.str.len())
g.aggregate({'A':len, 'B':np.sum})
Given a data frane df with columns ['A', 'B', 'C', 'D'] and rows ['r1', 'r2', 'r3'], Which of the following
expression filter those rows whose column B values are greater than 45
df[df.B > 45]
Which of the following expression returns second row of df, defined below.
import pandas
df = pd.DataFrame({'A':[34, 78, 54], 'B':[12, 67, 43]}, index=['r1', 'r2', 'r3'])
df.iloc[1]
------------------------------------------------------------------------------------
NLP Using Python
sentence = """At eight o'clock on Thursday morning... Arthur didn't feel very good."""
tokens = nltk.word_tokenize(sentence)
print(tokens)
tagged = nltk.pos_tag(tokens)
print(tagged[0:6])
entities = nltk.chunk.ne_chunk(tagged)
print(entities)
wordfreq = nltk.FreqDist(words)
wordfreq.most_common(2)
[('programming', 2), ('.', 2)]
word nltk.import nl
nltk.download('book')
from nltk.book import *.
text1.findall("<tri.*r>")
type(text1)
n_unique_words = len(set(text1))
fdist
top3_text1 = text1_freq.most_common(3)
####TEXT CORPORA
Popular Text Corpora
Genesis: It is a collection of few words across multiple languages.
Brown: It is the first electronic corpus of one million English words.
2166
18.55
['noise','surprise','wise','apologise'] = 4
How many times each unique word of text6 collection is repeated on an average?
1.16
Count the number of words in text collection, text6, ending with ship?
4
How many times does the word 'BROTHER' occur in text collection text6?
Which of the following modules is used for performing Natural language processing in python?
nltk
Which of the following expression is used to download all the required corpus and collections ,
related to NLTK Book ?
nltk.download('book')
In how many number of categories, are all text collections of brown corpus grouped into?
15
Which of the following method is used to determine the number of characters present in a corpus?
char()
#############
items = ['apple', 'apple', 'kiwi', 'cabbage', 'cabbage', 'potato']
nltk.FreqDist(items)
How many times do the word sugar occur in text collections, grouped into genre 'sugar'? Consider
reuters corpus.
521
How many times do the word zinc occur in text collections, grouped into genre 'zinc'? Consider
reuters corpus
70
Which of the following class is used to determine count of all tokens present in a given text ?
FreqDist
What is the number of sentences obtained after breaking 'Python is cool!!!' into sentences using
sent_tokenize
4
Which of the following method is used to tokenize a text based on a regular expression?
regexp_tokenize()
Which of the following class is used to convert a list of tokens into NLTK text?
X-nltk.text
nltk.text
Which of the following module can be used to read text data from a pdf document?
pypdf
How many times do the words gasoline and barrels occur in text collections, grouped
into genre gas? Consider reuters corpus
Which of the following method is used to determine the number of characters present in a
corpus?
raw()
Which of the following method can be used to determine the location of a text collection,
associated with a corpus?
Abspath()
Which of the following class is used to convert your own collections of text into a corpus?
PlaintextCorpusReader
In how many number of categories, are all text collections of brown corpus grouped into?
15
Which of the following module is used to download text from a HTML file?
urllib
Which of the following function is used to generate a set of all possible n consecutive words
appearing in a text
n-grams()
#########
Lancaster Stemmer returns build
Porter Stemmer returns builder.
################FINAL############################
What is the output of the following expression?
import nltk
lancaster = nltk.LancasterStemmer()
print(lancaster.stem('power'))
pow
What is the total number of unique words present in text collection, text6? Considering characters
too as words
2166
How many words are ending with 'ing' in text collection text6?
109
Count the number of words in text collection, text6, which have only digits as characters?
24
Which of the following NLTK corpus represent a collection US presidential inaugural addresses,
starting from 1789?
inaugural
Which tag occurs maximum in text collections associated with news genre of brown corpus?
NN
How many number of words are obtained when the sentence Python is cool!!! is tokenized into
words, with regular expression r'\w+' ?
3
import nltk
lancaster = nltk.LancasterStemmer()
print(lancaster.stem('women'))
wom
Which of the following module is used to download text from a HTML file
urllib
How many times does the word sugar occur in text collections, grouped into genre 'sugar'? Consider
reuters corpus.
521
How many times does the words tonnes and year occur in text collections, grouped into genre
sugar? Consider reuters corpus.
355, 196
How many times does the tag AT is associated with the word The in brown corpus?
7824
How many times does the words lead and smelter occur in text collections, grouped into genre zinc?
Consider reuters corpus.
32, 33
###################
import re
text = 'Python is cool!!!'
tokens = re.findall(r'\w+', text)
len(tokens)
3
import nltk
text = 'Python is awesome.'
words = nltk.word_tokenize(text)
defined_tags = {'is':'BEZ', 'over':'IN', 'who': 'WPS'}
-------------------------------------------------------------------------------------------------------
LIBRARY MANUAL:
https://www.nltk.org/book/ch02.html
ONLINE CONSOLE PYTHON3:
https://www.katacoda.com/courses/python/playground
pip3 install --user setuptools && pip3 install nltk
python3 -c "import nltk; nltk.download('book')"
--------------------------------------------------------------------------------------------------------
EXAMEN FINAL
--------------------------------------------------------------------------------------------------------
Which of the following is not a collocation, associated with text6 ?
import nltk
from nltk.book import text6
gen_text = nltk.Text(text6)
print(gen_text.collocations())
Straight Table
--------------------------------------------------------------------------------------------------------
How many times does the tag AT is associated with the word The in brown corpus?
import ntltk
from nltk.corpus import brown
brown_text_tagged = nltk.corpus.brown.tagged_words()
tag_fd = nltk.FreqDist(tag for (word, tag) in brown_text_tagged if tag=='AT' and word =='The')
print(tag_fd)
6725
--------------------------------------------------------------------------------------------------------
Which of the following function is used to tag parts of speech to words appearing in a text?
pos_tag()
--------------------------------------------------------------------------------------------------------
How many words are ending with 'ly' in text collection text6?c
import nltk
from nltk.book import text6
ly_ending_words = [word for word in text6 if word.endswith('ly') ]
print(len(ly_ending_words))
109
--------------------------------------------------------------------------------------------------------
Which of the following method can be used to determine the number of text collection files
associated with a corpus?
fileids()
--------------------------------------------------------------------------------------------------------
Count the number of words in text collection, text6, which have only digits as characters?
24
--------------------------------------------------------------------------------------------------------
Which of the following method is used to view the tagged words of text corpus
tagged_words()
--------------------------------------------------------------------------------------------------------
What is the output of the following expression?
import nltk
lancaster = nltk.LancasterStemmer()
print(lancaster.stem('lying'))
lying
--------------------------------------------------------------------------------------------------------
What is the frequency of bigram ('HEAD', 'KNIGHT') in text collection text6
import nltk
from nltk.book import text6
bigrams = nltk.bigrams(tokens)
filtered_bigrams = [ (w1, w2) for w1, w2 in bigrams if w1=='HEAD' and w2=='KNIGHT']
print(filtered_bigrams)
29
--------------------------------------------------------------------------------------------------------
What is the output of the following expression ?
import nltk
porter = nltk.PorterStemmer()
print(porter.stem('ceremony'))
ceremoni
--------------------------------------------------------------------------------------------------------
Which of the following method is used to tokenize a text based on a regular expression
regexp_tokenize()
--------------------------------------------------------------------------------------------------------
What is the frequency of word 'ARTHUR' in text collection text6
import nltk
from nltk.book import text6
fdist = nltk.FreqDist(text6)
print(fdist.freq('ARTHUR'))
0.0132
--------------------------------------------------------------------------------------------------------
Which of the following function is used to obtain set of all pair of consecutive words appearing in a
text?
bigrams()
--------------------------------------------------------------------------------------------------------
What is the range of length of words present in text collection text6?
X-1 to 10
--------------------------------------------------------------------------------------------------------
What is the output of the following code?
import re
s = 'Python is cool!!!'
print(re.findall(r'\s\w+\b', s))
[' is', ' cool']
--------------------------------------------------------------------------------------------------------
Which of the following class is used to convert your own collections of text into a corpus?
PlaintextCorpusReader
--------------------------------------------------------------------------------------------------------
What is the output of the following expression?
import nltk
wnl = nltk.WordNetLemmatizer()
print(wnl.lemmatize('women'))
woman
--------------------------------------------------------------------------------------------------------
Which of the following NLTK corpus represent a collection of around 10000 news articles?
reuters
--------------------------------------------------------------------------------------------------------
How many times each unique word of text6 collection is repeated on an average?
X-6.5 times
--------------------------------------------------------------------------------------------------------
What is the frequency of bigram ('BLACK', 'KNIGHT') in text collection text6?
import nltk
from nltk.book import text6
bigrams = nltk.bigrams(text6)
filtered_bigrams = [ (w1, w2) for w1, w2 in bigrams if w1=='BLACK' and w2=='KNIGHT']
print(len(filtered_bigrams))
32
--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
HANDS ON: 1
--------------------------------------------------------------------------------------------------------
pip3 install --user setuptools && pip3 install nltk
python3 -c "import nltk; nltk.download('book')"
import nltk
from nltk.book import text6
n = len(text6)
print(n)
u = len(set(text6))
print(u)
wc = n/u
print(wc)
import re
title_words = len(re.findall(r'([A-Z][a-z]+)', text6))
--------------------------------------------------------------------------------------------------------
HANDS ON: 2
--------------------------------------------------------------------------------------------------------
import nltk, re
from nltk.corpus import gutenberg
for fileid in gutenberg.fileids():
n_words = len(gutenberg.words(fileid))
n_unique_words = len(set(gutenberg.words(fileid)))
word_coverage = n_words / n_unique_words
print(word_coverage, fileid)
aus_words = len(gutenberg.words('austen-sense.txt))
aus_words_apha = len([word for word in gutenberg.words('austen-sense.txt') if word.isalpha()]
aus_words_gt4_z = len([word for word in gutenberg.words('austen-sense.txt') if word.isalpha() and
len(word) > 4 and 'z' in word])
print(aus_words_gt4_z)
--------------------------------------------------------------------------------------------------------
HANDS ON: 3
--------------------------------------------------------------------------------------------------------
import nltk
from nltk.corpus import brown
brown_cdf = nltk.ConditionalFreqDist([
(genre,word.lower())
for genre in brown.categories()
for word in brown.words(categories=genre) ])
print(inaugural_cfd.conditions())
--------------------------------------------------------------------------------------------------------
HANDS ON: 4
--------------------------------------------------------------------------------------------------------
import nltk
from urllib import request
from bs4 import BeautifulSoup
url = "https://en.wikipedia.org/wiki/Python_(programming_language)"
html_content = request.urlopen(url).read()
soup = BeautifulSoup(html_content, 'html.parser')
n_links = len(soup.find_all('a'))
print(n_links)
--------------------------------------------------------------------------------------------------------
HANDS ON: 5
--------------------------------------------------------------------------------------------------------
import nltk
from nltk.corpus import brown
news_words = brown.words(categories='news')
lc_news_words = [w.lower() for w in news_words]
len_news_words = [len(w) for w in lc_news_words]
news_len_bigrams = list(nltk.bigrams(len_news_words))
#Compute the conditional frequency of news_len_bigrams, where condition and event refers to
length of a words.
#Store the result in cfd_news
#Determine the frequency of 6-letter words appearing next to a 4-letter word
cfd_news = nltk.ConditionalFreqDist(news_len_bigrams)
cfd_news.tabulate(conditions=[6,4])
#############
lc_news_bigrams =nltk.ConditionalFreqDist(news_len_bigrams)
#
filtered_bigrams = [(w1, w2) for w1, w2 in news_len_bigrams if w1==6 and w2==4]
cfd_news = nltk.FreqDist(filtered_bigrams)
print(cfd_news[6,4])
#
cfd_news = nltk.FreqDist((l1, l2) in news_len_bigrams if l1==6 amd l2==4)
print(cfd_news[6,4])
--------------------------------------------------------------------------------------------------------
HANDS ON: 6
--------------------------------------------------------------------------------------------------------
from nltk.corpus import brown
humor_words = brown.words(categories='humor')
lc_humor_words = [word.lower() for word in humor_words]
lc_humor_uniq_words = set(lc_humor_words)
from nltk.corpus import words
wordlist_words = words.words()
wordlist_uniq_words = set(wordlist_words)
print(len(lc_humor_uniq_words))
print(len(wordlist_uniq_words ))
--------------------------------------------------------------------------------------------------------
HANDS ON: 7
This results in a list of tuples, where each tuple contain pos tags of 3 consecutive words, occurring
in text.
Store the result in brown_trigram_pos_tags.
Determine the frequency distribution of brown_trigram_pos_tags and store the result in
brown_trigram_pos_tags_freq.
Print the number of occurrences of trigram ('JJ','NN','IN')
--------------------------------------------------------------------------------------------------------
import nltk
from nltk.corpus import brown
brown_tagged_words = [word for (word, tag) in nltk.corpus.brown.tagged_words()]
brown_tagged_trigrams = list(nltk.trigrams(brown_tagged_words))
brown_trigram_pos_tags = list()
for trigram in brown_tagged_trigrams:
trigram_tagged = nltk.pos_tag(trigram)
tags = [tag for (word, tag) in trigram_tagged]
brown_trigram_pos_tags.append(tags)
$buttonStart_Click={
#[System.Windows.MessageBox]::Show('Start')
$this.Enabled=$false
$script:ts1 = [timespan]'0:0:0:10'
$timer1.Start()
}
$1second=[timespan]'0:0:0:1'
$timer1_Tick={
$script:ts1=$ts1.Subtract($1second)
Write-Host $ts1
$labelCounter.Text = $ts1.ToString('hh\:mm\:ss')
if($ts1.Ticks -le 0){
$script:ts1 = [timespan]'0:0:0:10'
}
}
$buttonStop_Click={
$timer1.Stop()
$buttonStart.Enabled=$true
}
$buttonClose_Click={
$timer1.Stop()
$Form.Close()
}
$buttonStart.add_Click($buttonStart_Click)
$buttonStop.add_Click($buttonStop_Click)
$timer1.Interval = 1000
$timer1.add_Tick($timer1_Tick)
$Form.controls.AddRange(@($buttonClose,$buttonStart,$buttonStop,$labelCounter))
[void]$Form.ShowDialog()
*/