Recently I came to know about the vicinal words. As python is good for string processing. So thoought of writing code for finding vicinal words in python. Below is the code to find vicinal words in python.
Vicinal: Vicinals are words where every letter in the word has an alphabetic neighbour. That means, if the word contains a 'C', it also contains either 'B' or 'D'. Check here for more details.
import string def find_vicinals(input_words): vicinal =  non_vicinal =  #take words one by one for word in input_words: count = 0 #logic to compare each char with all for their neighbours if len(word) > 1: for c1 in word.lower(): for c2 in word.lower(): if c2 == 'a' and ord(c1)+1 == ord(c2) or ord(c2) == ord('z'): count += 1 break; elif c2 == 'z' and ord(c2) == ord('a') or ord(c2)-1 == ord(c2): count += 1 break; elif ord(c1)+1 == ord(c2) or ord(c1)-1 == ord(c2): count += 1 break #if match count n word len are same #then its vicinal if count == len(word): vicinal.append(word) elif not count: non_vicinal.append(word) else: non_vicinal.append(word) #print if list not empty if vicinal.__len__(): print "Vicinals: ", ' '.join(vicinal) if non_vicinal.__len__(): print "Non-vicinals: ", ' '.join(non_vicinal) #read the line s = raw_input("Line: ") # check length n proceed if not zero while len(s): input_words = s.split(' ') input_words = ' '.join(s.split()) for c in string.punctuation: input_words= input_words.replace(c,"") find_vicinals(input_words.split(' ')) # read line again s = raw_input("Line: ")