Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. import random
  2. import shelve
  3. import string
  4. import sys
  5.  
  6. words = shelve.open('word.db')
  7.  
  8. class Word(object):
  9.    
  10.     def __init__(self, word):
  11.         self.word = word
  12.         self.relations = {}
  13.    
  14.     def __eq__(self, other):
  15.         return self.word == other.word
  16.    
  17.     def __ne__(self, other):
  18.         return not(self == other)
  19.    
  20.     def set(self, word):
  21.         if word in self.relations:
  22.             self.relations[word] += 1
  23.         else:
  24.             self.relations[word] = 1
  25.         print '%s : %s + 1 = %d' % (self.word, word, self.relations[word])
  26.    
  27.     def get(self, n):
  28.         return sorted((list(x)[::-1] for x in self.relations.iteritems()))[::-1][:n]
  29.  
  30. def insert(word, succ):
  31.     if word in words:
  32.         w = words[word]
  33.         w.set(succ)
  34.     else:
  35.         w = Word(word)
  36.         w.set(succ)
  37.     words[word] = w
  38.        
  39. def noise_reduct(sent):
  40.     symbols = string.lowercase + ' '
  41.     return ''.join([char for char in sent if char.lower() in symbols])
  42.  
  43. def analyse(sent):
  44.     sent = noise_reduct(sent).split()
  45.     lens = range(len(sent) - 1)
  46.     for x in lens:
  47.         insert(sent[x].lower(), sent[x + 1].lower())
  48.        
  49. def read(path):
  50.     text = open(path, 'r')
  51.     sentences = text.read()
  52.     analyse(sentences)
  53.  
  54. def generate(word, size =10):
  55.     sent = []
  56.     if word not in words:
  57.         return
  58.     for x in range(size):
  59.         sent.append(word)
  60.         index = 0
  61.         while True:
  62.             word = random.choice(words[word].get(10))[1]
  63.             if word not in sent:
  64.                 break
  65.     return ' '.join(sent)
  66.    
  67. def run():
  68.     while True:
  69.         n = raw_input(': ')
  70.         if n in words:
  71.             for x in words[n].get(10):
  72.                 print x[1],
  73.             print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement