You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. # import modules and set up logging
  2. from gensim.models import word2vec
  3. import logging
  4. import gensim
  5. import _pickle as cPickle
  6. logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
  7. # load up unzipped corpus from http://mattmahoney.net/dc/text8.zip
  8. #sentences = word2vec.Text8Corpus('corpus/dewiki.txt') # muss auskommentiert werden und den text der output vom preprocessing.py ist muss rein
  9. # train the skip-gram model; default window=5
  10. #model = word2vec.Word2Vec(sentences, size=200) # muss auskommentiert werden
  11. # ... and some hours later... just as advertised...
  12. #model.most_similar(positive=['woman', 'king'], negative=['man'], topn=1)
  13. # pickle the entire model to disk, so we can load&resume training later
  14. #model.save('models/wiki.model')
  15. # store the learned weights, in a format the original C tool understands
  16. #model.save_word2vec_format('models/wiki.model.bin', binary=True) # Jo diese Zeile muss auskommentiert werden, mit den dreien muesste es klappen.
  17. # or, import word weights created by the (faster) C word2vec
  18. # this way, you can switch between the C/Python toolkits easily
  19. #model = gensim.models.Word2Vec.load('models/wiki.model')
  20. # Hier kommt jetzt ein beispiel, wie man das schoen dann nutzen kann. kommentiere das beim trainieren erst mal aus, also alles ab hier. Dann lade das bin model und schau dir an was bei rumkommt. Ich dachte an cluster von similar_by_word mengen, das muss dann natuerlich alles noch in zahlen umgewandelt werden damit man schnellen db zugriff hat.
  21. print('loading the big model')
  22. model = gensim.models.KeyedVectors.load_word2vec_format('german.model.big', binary=True) # C binary format
  23. print('done')
  24. print('the vocab is')
  25. print(model.vocab)
  26. # "boy" is to "father" as "girl" is to ...?
  27. print(1 ,model.most_similar(positive=['Koenig','Koenigin'], negative=['Mann'], topn=1))
  28. #print(2 ,model.doesnt_match("das nichts dazu".split()))
  29. print(3 , model.similar_by_word("Asylbewerber", topn=20))
  30. print(4, model.n_similarity(['dazu'], ['nicht']))
  31. print(5, model.n_similarity(['dazu'], ['nichts']))
  32. '''
  33. print
  34. print 'Der Vektor fuer Warschau ist:'
  35. print
  36. x = model['Warschau']
  37. print x
  38. print
  39. print 'Und die laenge betraegt:'
  40. print
  41. print len(model['Warschau'])
  42. pickle.dump( x, open( "Vektoren.p", "wb" ) )
  43. y = pickle.load( open( "Vektoren.p", "rb" ) )
  44. print y'''