Does Python define a word?

Can python be used to define a word like this:

x=raw_input("Enter Word: ")
x=define(x)
print x

      

Is it possible? If si .. how to do it?

It's just a "what if" question, so no hate plz.

-2


source to share


2 answers


You can do this with the re and urllib2 modules. Try this code from dawg.

import re
import urllib2
def find(x):
    srch=str(x)
    x=urllib2.urlopen("http://dictionary.reference.com/browse/"+srch+"?s=t")
    x=x.read()
    items=re.findall('<meta name="description" content="'+".*$",x,re.MULTILINE)
    for x in items:
        y=x.replace('<meta name="description" content="','')
        z=y.replace(' See more."/>','')
        m=re.findall('at Dictionary.com, a free online dictionary with pronunciation,              synonyms and translation. Look it up now! "/>',z)
        if m==[]:
            if z.startswith("Get your reference question answered by Ask.com"):
                print "Word not found! :("
            else:
                print z
    else:
            print "Word not found! :("
x=raw_input("Enter word to find: ")
find(x)

      



Let me know how it goes!

+5


source


import re
from urllib2 import urlopen


def define(word):
    html = urlopen("http://dictionary.reference.com/browse/" + word + "?s=t").read()
    items = re.findall('<div class="def-content">\s.*?</div>', html, re.S)
    defs = [re.sub('<.*?>','', x).strip() for x in items]
    for i, d in enumerate(defs):
        print '\n', '%s'%(i+1), d


define(raw_input("Enter the word you'd like to define: "))

      



This is essentially the same idea as @ SamTubb's answer.
If the .com dictionary has no definition for the word you entered, you get HTTPError

.

0


source







All Articles