I need to calculate orthographic similarity (edit/Levenshtein distance) among words in a given corpus.
As Kirill suggested below, I tried to do the following:
import csv, itertools, Levenshtein
import numpy as np
# import the list of words from csv file
path = '/Users/my path'
file = path + 'file.csv'
with open(file, 'rb') as f:
reader = csv.reader(f)
wordlist = list(reader)
wordlist = np.array(wordlist) #make it a np array
wordlist2 = wordlist[:,0] #subset the first column of the imported list
for a, b in itertools.product(wordlist, wordlist):
if a < b:
print(a, b, Levenshtein.distance(a, b))
However, the following error pops up:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I understand the ambiguity in the code, but can someone help me figure out how to solve this? Thanks!
Here's the code I came up with thank to the help of Kirill.
import csv#, StringIO
import itertools, Levenshtein
# open the newline-separated list of words
path = '/Users/your path'
file = path + 'wordlists.txt'
output = path + 'ortho_similarities.txt'
words = sorted(set(s.strip() for s in open(file)))
# the following loop take all possible pairwise combinations
# of the words in the list words, and calculate the LD
# and then let's write everything in a csv file
with open(output, 'wb') as f:
writer = csv.writer(f, delimter=",", lineterminator="\n")
for a, b in itertools.product(words, words):
if a < b:
write.writerow([a, b, Levenshtein.distance(a,b)])