python code examples n.
Skip this Video
Loading SlideShow in 5 Seconds..
Python Code Examples PowerPoint Presentation
Download Presentation
Python Code Examples

Loading in 2 Seconds...

play fullscreen
1 / 30

Python Code Examples - PowerPoint PPT Presentation

  • Uploaded on

Python Code Examples. Word Spotting. import sys fname1 = "c:\Python Course\ex1.txt" for line in open(fname1,'r').readlines(): for word in line.split(): if word.endswith('ing'): print word. Creating a Dictionary of First Names. def createNameDict():

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Python Code Examples' - akiva

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
word spotting
Word Spotting

import sys

fname1 = "c:\Python Course\ex1.txt"

for line in open(fname1,'r').readlines():

for word in line.split():

if word.endswith('ing'):

print word

creating a dictionary of first names
Creating a Dictionary of First Names

def createNameDict():

dictNameFile=open('project/dictionaries/names.txt','r') #read all the file

dictWords=dictContent.split(",") #return a list with the words

nameDict={} # initialize a dictionary

for word in dictWords:

nameDict[word.strip()]=" " #enters each word to the dctionary.

return nameDict

computing accuracy results i
Computing Accuracy Results I


# Program to analyze the results of speaker identification.

# Illustrates Python dictionarys

import string, glob, sys

def main():

# read correct file and test file

fname1 = sys.argv[1]

fname2 = sys.argv[2]

text1 = open(fname1,'r').read()

text1 = string.lower(text1)

words1 = string.split(text1)

correct_len = len(words1)

text2 = open(fname2,'r').read()

text2 = string.lower(text2)

words2 = string.split(text2)

computing accuracy results ii
Computing Accuracy Results II

# construct a dictionary of correct results

correct = {}

for w in words1:

correct[w] = 1

for i in range(correct_len):

in_count = 0

portion2 = words2[:i+1]

for w in portion2:

if correct.get(w,0) > 0:


accuracy = float(in_count)/float(len(portion2))

print "%5d, %5d,%.2f" % (len(portion2), in_count, accuracy)

if __name__ == '__main__': main()

word histograms
Word Histograms

import sre, string

pattern = sre.compile( r'[a-zA-Z]+' )

def countwords(text):

dict = {}


iterator = pattern.finditer(text)

for match in iterator:

word =


dict[word] = dict[word] + 1

except KeyError:

dict[word] = 1

except sre.error:

pass # triggers when first index goes to -1, terminates loop.

word histograms1
Word Histograms

items = []

for word in dict.keys():

items.append( (dict[word], word) )



return items

# if run as a script, count words in stdin.

if __name__ == "__main__":

import sys

x = countwords( )

s = map(str, x)

t = string.joinfields(s, "\n")

print t

extracting people names and company names
Extracting People Names and Company Names

import string, sre, glob, sys

def createNameDict():

dictNameFile=open('names.txt','r') #read all the file

dictWords=dictContent.split(",") #return a list with the words

nameDict={} # initialize a dictionary

for word in dictWords:

nameDict[word.strip()]=" " #enters each word to the dctionary.

return nameDict

def main():

# read file

fname1 = sys.argv[1]

text1 = open(fname1,'r').read()

namesDic = createNameDict()

CompanySuffix = sre.compile(r'corp | ltd | inc | corporation | gmbh | ag | sa ', sre.IGNORECASE)

pattern = sre.compile( r'([A-Z]\w+[ .,-]+)+'

extracting people names and company names1
Extracting People Names and Company Names



pattern1 = sre.compile( r'([A-Z]\w+[\s.-]*){2,4}' )



for match in capitalWords:

CapSeq =

print CapSeq



for match in capitalWords1:

#check name in names dictionary

if namesDic.has_key(wordList[0].strip()):


if __name__ == '__main__': main()

  • NLTK defines a basic infrastructure that can be used to build NLP programs in Python. It provides:
    • Basic classes for representing data relevant to natural language processing.
    • Standard interfaces for performing tasks, such as tokenization, tagging, and parsing.
    • Standard implementations for each task, which can be combined to solve complex problems.
    • Extensive documentation, including tutorials and reference documentation.
re show
RE Show

>>> from nltk.util import re_show

>>> string = """

... It’s probably worth paying a premium for funds that invest in markets

... that are partially closed to foreign investors, such as South Korea, ...

... """

>>> re_show(’t...’, string)

I{t’s }probably wor{th p}aying a premium for funds {that} inves{t in} markets {that} are par{tial}ly closed {to f}oreign inves{tors}, such as Sou{th K}orea, ...


defining classes
Defining Classes

>>> class SimpleClass:

... def __init__(self, initial_value):

... = initial_value

... def set(self, value):

... = value

... def get(self):

... print


>>> x = SimpleClass(4)


B is a subclass of A

>>> class B(A):

... def __init__(self):

SimpleTokenizer implements the interface of TokenizerI

>>> class SimpleTokenizer(TokenizerI):

... def tokenize(self, str):

... words = str.split()

... return [Token(words[i], Location(i))

... for i in range(len(words))]

inheritance example
Inheritance Example

class point:

def __init__(self, x=0, y=0):

self.x, self.y = x, y

class cartesian(point):

def distanceToOrigin(self):

return floor(sqrt(self.x**2 + self.y**2))

class manhattan(point):

def distanceToOrigin(self):

return self.x + self.y

sets in python
Sets in Python
  • The sets module provides classes for constructing and manipulating unordered collections of unique elements. Common uses include:
    • membership testing,
    • removing duplicates from a sequence,
    • and computing standard math operations on sets such as intersection, union, difference, and symmetric difference.
  • Like other collections, sets support x in set, len(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.
some details about implementation
Some Details about Implementation
  • Most set applications use the Set class which provides every set method except for __hash__(). For advanced applications requiring a hash method, the ImmutableSet class adds a __hash__() method but omits methods which alter the contents of the set.
  • The set classes are implemented using dictionaries. As a result, sets cannot contain mutable elements such as lists or dictionaries.
  • However, they can contain immutable collections such as tuples or instances of ImmutableSet.
  • For convenience in implementing sets of sets, inner sets are automatically converted to immutable form, for example, Set([Set(['dog'])]) is transformed to Set([ImmutableSet(['dog'])]).
set examples
Set Examples

>>> from sets import Set

>>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])

>>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])

>>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])

>>> employees = engineers | programmers | managers # union

>>> engineering_management = engineers & managers # intersection

>>> fulltime_management = managers - engineers - programmers # difference

>>> engineers.add('Marvin') # add element

>>> print engineers

Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])

>>> employees.issuperset(engineers) # superset test


set examples1
Set Examples

>>> employees.union_update(engineers) # update from another set

>>> employees.issuperset(engineers)


>>> for group in [engineers, programmers, managers, employees]:

... group.discard('Susan') # unconditionally remove element

... print group


Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])

Set(['Janice', 'Jack', 'Sam'])

Set(['Jane', 'Zack', 'Jack'])

Set(['Jack', 'Sam', 'Jane', 'Marvin', 'Janice', 'John', 'Zack'])

google api
Google API
  • Get it from
  • A Python wrapper for the Google web API. Allows you to do Google searches, retrieve pages from the Google cache, and ask Google for spelling suggestions.
utilizing the google api i
Utilizing the Google API - I

import sys

import string

import codecs

import google

print '<html xmlns="" xml:lang="en">'

print '<head>'

print ' <title>Google with Python</title>'

print '</head>'

print '<body>'

print '<h1>Google with Python</h1>'


sys.stdout = codecs.lookup('utf-8')[-1](sys.stdout)

query = “Your Query"

data = google.doGoogleSearch(query)

utilizing the google api ii
Utilizing the Google API - II

print '<p><strong>1-10 of "' + query + '" total results for '

print str(data.meta.estimatedTotalResultsCount) + '</strong></p>'

for result in data.results:

title = result.title

title = title.replace('<b>', '<strong>')

title = title.replace('</b>', '</strong>')

snippet = result.snippet

snippet = snippet.replace('<b>','<strong>')

snippet = snippet.replace('</b>','</strong>')

snippet = snippet.replace('<br>','<br />')

print '<h2><a href="' + result.URL + '">' + title + '</a></h2>'

print '<p>' + snippet + '</p>'

print '</body>‘

print '</html>'

yahoo api
Yahoo API
  • This project implements a Python API for the Yahoo Search Webservices API. pYsearch is an OO abstraction of the web services, with emphasis on ease of use and extensibility.
  • This module provides a high-level interface for fetching data across the World Wide Web.
  • In particular, the urlopen() function is similar to the built-in function open(), but accepts Universal Resource Locators (URLs) instead of filenames.
  • Some restrictions apply -- it can only open URLs for reading, and no seek operations are available.
urllib syntax
Urllib Syntax
  • # Use for http proxying

proxies = {'http': ''}

filehandle = urllib.urlopen(some_url, proxies=proxies)

  • # Don't use any proxies

filehandle = urllib.urlopen(some_url, proxies={})

urllib examples
URLLIB Examples
  • Here is an example session that uses the "GET" method to retrieve a URL containing parameters:

>>> import urllib

>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})

>>> f = urllib.urlopen("" % params)

>>> print

  • The following example uses the "POST" method instead:

>>> import urllib

>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})

>>> f = urllib.urlopen("", params)

>>> print

what is a proxy
What is a Proxy
  • A proxy server is a computer that offers a computer network service to allow clients to make indirect network connections to other network services.
  • A client connects to the proxy server, then requests a connection, file, or other resource available on a different server.
  • The proxy provides the resource either by connecting to the specified server or by serving it from a cache.
  • In some cases, the proxy may alter the client's request or the server's response for various purposes.
  • A proxy server can also serve as a firewall.