Commit f086c017 authored by Cristina's avatar Cristina

gensim for javi

parent 56547527
Subproject commit 36f8bbaa818e65bcebe1897ebb997eeb0d746d2f
from gensim import corpora
documents = ["Human machine interface for lab abc computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement",
"The generation of random binary unordered trees",
"The intersection graph of paths in trees",
"Graph minors IV Widths of trees and well quasi ordering",
"Graph minors A survey"]
# remove common words and tokenize
stoplist = set('for a of the and to in'.split())
texts = [[word for word in document.lower().split() if word not in stoplist]
for document in documents]
# remove words that appear only once
from collections import defaultdict
frequency = defaultdict(int)
for text in texts:
for token in text:
frequency[token] += 1
texts = [[token for token in text if frequency[token] > 1]
for text in texts]
from pprint import pprint # pretty-printer
pprint(texts)
[['human', 'interface', 'computer'],
['survey', 'user', 'computer', 'system', 'response', 'time'],
['eps', 'user', 'interface', 'system'],
['system', 'human', 'system', 'eps'],
['user', 'response', 'time'],
['trees'],
['graph', 'trees'],
['graph', 'minors', 'trees'],
['graph', 'minors', 'survey']]
......@@ -6,7 +6,7 @@ import re
import string
import codecs
filename = '../input/astroBlackness.txt'
filename = '../input/frankenstein.txt'
sentences = []
regex = re.compile('[%s]' % re.escape(string.punctuation)) #see documentation here: http://docs.python.org/2/library/string.html
......@@ -15,20 +15,17 @@ decoded = ''
with open(filename, 'r') as source:
lines = source.readlines()
if len(lines) == 1:
lines = lines[0].decode('utf8')
# lines = lines[0].decode('utf8')
decoded = 'yes'
lines = lines.split('. ')
print lines
for line in lines:
# print line
# if "”" in line:
# line = line.replace("”","")
# if "“" in line:
# line = line.replace("“","")
if decoded != 'yes':
line = line.decode('utf8')
print(line)
if "”" in line:
line = line.replace("”","")
if "“" in line:
line = line.replace("“","")
words = word_tokenize(line)
print words
string = []
for word in words:
new_word = regex.sub(u'', word)
......@@ -40,5 +37,5 @@ with open(filename, 'r') as source:
outputfilename = filename.replace('.txt','_stripped.txt')
with codecs.open(outputfilename, "w", "utf-8") as destination:
for sentence in sentences:
destination.write(sentence.strip().capitalize()+" ")
print '*text is stripped*'
destination.write(sentence.strip().lower()+" ")
print('*text is stripped*')
import sys, os
import gensim, logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
test_data_dir = '{}'.format(os.sep).join([gensim.__path__[0], 'test', 'test_data']) + os.sep
lee_train_file = '/Users/cristinacochior/Documents/Algolit/algolit/algoliterary_encounter/word2vec/input/mankind-in-the-making_stripped.txt'
class MyText(object):
def __iter__(self):
for line in open(lee_train_file):
# assume there's one document per line, tokens separated by whitespace
yield line.lower().split()
sentences = MyText()
model = gensim.models.Word2Vec(sentences, min_count=10)
model = gensim.models.Word2Vec(sentences, size=200)
model.save('/Users/cristinacochior/Documents/Algolit/algolit/algoliterary_encounter/word2vec/firstgensimmodel')
print model.most_similar(positive=['man', 'mankind'], negative=['woman'], topn=1)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment