def getNearRhymes(inputString): rhymes = [] pron1 = Wordsmith.tokenize(inputString) words = Wordsmith.getRelevantWords(inputString) if not words: return [] for word in words: word = str(word) if inputString == word: continue elif "'" in word: continue pron2 = Wordsmith.tokenize(word) if not pron2: continue score = nearRhymeScore(pron1, pron2) if score > 0: rhymes.append((word, score)) return sorted(rhymes, key=lambda t: t[1], reverse=True)
def add(self, word): stress = Wordsmith.getStress(Wordsmith.tokenize(word)) if (len(stress) < 1): return False phone = stress[-1][1] if phone not in self.buckets.keys(): self.buckets[phone] = [] if not word in self.buckets[phone]: self.buckets[phone].append(word)
def check(inputString, word): if inputString == word: return -1 pron1 = Wordsmith.tokenize(inputString) pron2 = Wordsmith.tokenize(word) print pron1 print pron2 return nearRhymeScore(pron1, pron2)
def nearRhymeScore(pron1, pron2): stress1 = Wordsmith.getStress(pron1) stress2 = Wordsmith.getStress(pron2) if not stress1 or not stress2: return -1 if ((len(stress1) - len(stress2)) > 1): return -1 match = 0 if (len(stress1) > len(stress2)): tempStress = stress1 stress1 = stress2 stress2 = tempStress tempPron = pron1 pron1 = pron2 pron2 = tempPron for i in range(1, len(stress1) + 1): # Check same stress pattern of word if (stress1[-i][0] == stress2[-i][0]): match += 1 score = nearSyllableScore(stress1[-i], stress2[-i]) if (score < 0): match = -1 break elif (i == 1) and (score * 1.5 < 1): match = -1 break else: consonantScore = 0 if (stress1[-i][2] > 0) and (stress2[-i][2] > 0) and ( stress1[-i][2] < len(pron1) - 1) and (stress2[-i][2] < len(pron2) - 1): index1 = stress1[-i][2] index2 = stress2[-i][2] # Check surrounding consonants for closeness consonantScore = nearConsonantScore(pron1[index1 + 1], pron2[index2 + 1]) if i == 1: score *= 1.5 if score > 1: consonantScore *= 2.5 # if i == 2: # consonantScore *=1.5 # print i # print stress1[-i] # print stress2[-i] # print score # print consonantScore match += score match += consonantScore return match
def get_rhyme_score(word1, word2): if word1 == word2: return 0 pron1 = Wordsmith.tokenize(word1) pron2 = Wordsmith.tokenize(word2) return nearRhymeScore(pron1, pron2)
# all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. import Wordsmith import DataReader import math import time Wordsmith.setup() def rhyme(word, maxnum=75): rhymes = getNearRhymes(word.lower()) output = [] count = 0 for i in range(len(rhymes)): output.append(rhymes[i][0]) count += 1 if count == maxnum: break return output
def getListFromWord(self, word): stress = Wordsmith.getStress(Wordsmith.tokenize(word)) if (len(stress) < 1): return False phone = stress[-1][1] return self.get(phone)
def nearRhymeScore(pron1, pron2): stress1 = Wordsmith.getStress(pron1) stress2 = Wordsmith.getStress(pron2) if not stress1 or not stress2: return -1 if (len(stress1) - len(stress2)) > 1: return -1 match = 0 if len(stress1) > len(stress2): tempStress = stress1 stress1 = stress2 stress2 = tempStress tempPron = pron1 pron1 = pron2 pron2 = tempPron for i in range(1, len(stress1) + 1): # Check same stress pattern of word if stress1[-i][0] == stress2[-i][0]: match += 1 score = nearSyllableScore(stress1[-i], stress2[-i]) if score < 0: match = -1 break elif (i == 1) and (score * 1.5 < 1): match = -1 break else: consonantScore = 0 if ( (stress1[-i][2] > 0) and (stress2[-i][2] > 0) and (stress1[-i][2] < len(pron1) - 1) and (stress2[-i][2] < len(pron2) - 1) ): index1 = stress1[-i][2] index2 = stress2[-i][2] # Check surrounding consonants for closeness consonantScore = nearConsonantScore(pron1[index1 + 1], pron2[index2 + 1]) if i == 1: score *= 1.5 if score > 1: consonantScore *= 2.5 # if i == 2: # consonantScore *=1.5 # print i # print stress1[-i] # print stress2[-i] # print score # print consonantScore match += score match += consonantScore return match
def getListFromWord(self,word): stress = Wordsmith.getStress(Wordsmith.tokenize(word)) if (len(stress) < 1): return False phone = stress[-1][1] return self.get(phone)
def get_rhyme_score(word1,word2): if word1 == word2: return 0 pron1 = Wordsmith.tokenize(word1) pron2 = Wordsmith.tokenize(word2) return nearRhymeScore(pron1,pron2)