-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_db.py
57 lines (51 loc) · 1.35 KB
/
word_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pymongo import MongoClient
#connect to db
client = MongoClient('localhost', 27017)
db = client.wsd
#to create a meaning object
def createMeaning(m,rel):
obj={
m:[
]
}
for i in range(len(rel)):
obj[m].append({rel[i]:1})
return obj
#to create an array of meanings
def createMeanings(means,rels):
arr=[]
for i in range(len(means)):
arr.append(createMeaning(means[i],rels[i]))
return arr
#to create an object for a word to be inserted to db
def createWord(w,means):
obj={
'pkey': w,
w:[
]
}
for i in range(len(means)):
obj[w].append(means[i])
return obj
re=[]
re1=[]
w=raw_input("Enter the word:")
m=raw_input("Enter all meanings of word (delimited with space):").split()
print m
if not w:
print "Enter a word to store not enter key/spacekey"
elif not m:
print "Enter a meanings to store not enter key/spacekey"
else:
for i in range(len(m)):
re1 = raw_input("Enter rel. words of meaning (delimited with space) " + str(i) + " :").split()
re.append(re1)
# create object for word and given data
o = createWord(w, createMeanings(m, re))
words = db.words
print db.collection_names()
# insert into db
wid = words.insert(o)
print wid