Skip to content

Instantly share code, notes, and snippets.

@farizrahman4u
Created May 23, 2016 11:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save farizrahman4u/c973821e70a850d5dd5aaa6e522e3889 to your computer and use it in GitHub Desktop.
Save farizrahman4u/c973821e70a850d5dd5aaa6e522e3889 to your computer and use it in GitHub Desktop.
import numpy as np
__author__ = 'Fariz Rahman'
def eq(x, y):
return x.lower().replace(" ", "") == y.lower().replace(" ", "")
def get_words(x):
x = x.replace(" ", " ")
words = x.split(" ")
if "" in words:
words.remove("")
return words
def match(nb1, nb2):
l_1 = set(nb1[0])
l_2 = set(nb2[0])
n_l1 = len(l_1)
n_l2 = len(l_2)
if n_l1 + n_l2 == 0:
l_score = .5
else:
n_l_intersect = n_l1 - len(l_1 - l_2)
l_score = float(n_l_intersect) / (n_l1 + n_l2)
r_1 = set(nb1[0])
r_2 = set(nb2[0])
n_r1 = len(r_1)
n_r2 = len(r_2)
if n_r1 + n_r2 == 0:
r_score = .5
else:
n_r_intersect = n_r1 - len(r_1 - r_2)
r_score = float(n_r_intersect) / (n_r1 + n_r2)
return l_score + r_score
def get_neighbourhood(s, x):
left = []
right = []
n = len(s)
for i in range(n):
w = s[i]
if eq(w, x):
left += s[0 : i]
right += s[i + 1 : n]
return [left, right]
def get_transformation(q, a):
qw = get_words(q)
aw = get_words(a)
transform = []
for w1 in aw:
flag = False
for w2 in qw:
if eq(w1, w2):
flag = w2
break
if flag:
elem = ['neighbourhood', get_neighbourhood(qw, flag)]
else:
elem = ['word', w1]
transform += [elem]
return transform
def get_all_neighbourhoods(s):
nbs = []
for w in s:
nbs += [get_neighbourhood(s, w)]
return nbs
def get_max_match(nb, nbs):
scores = []
for nbx in nbs:
scores += [match(nbx, nb)]
return np.argmax(scores)
def apply_transformation(q, transformation):
a = ""
q = get_words(q)
nbs = get_all_neighbourhoods(q)
for elem in transformation:
if elem[0] == 'word':
a += elem[1] + ' '
else:
a += q[get_max_match(elem[1], nbs)] + ' '
return a[:-1]
Q = "please open the door"
A = "door : open"
t = get_transformation(Q, A)
Q2 = "close the window"
A2 = apply_transformation(Q2, t)
print A2
@viksit
Copy link

viksit commented May 24, 2016

thanks! would you talk about the general concept of how you're trying to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment