Skip to content

Instantly share code, notes, and snippets.

@TomLisankie
Created January 19, 2016 18:06
Show Gist options
  • Save TomLisankie/fc75e1cdf7b52518a317 to your computer and use it in GitHub Desktop.
Save TomLisankie/fc75e1cdf7b52518a317 to your computer and use it in GitHub Desktop.
//
// Phoneme.swift
import Foundation
class Phoneme {
private var isAVowelPhoneme = false;
private var phoneme = "";
private var stress = -1;
func setPhoneme(phonemeName: String){
self.phoneme = phonemeName;
}
func getPhoneme() -> String{
return phoneme;
}
func setIsAVowelPhoneme(isAVowel: Bool){
self.isAVowelPhoneme = isAVowel;
}
func getIsAVowelPhoneme() -> Bool{
return isAVowelPhoneme;
}
func setStress(stress: Int){
self.stress = stress;
}
func getStress() -> Int{
return stress;
}
func isEqualTo(p2: Phoneme) -> Bool{
if(self.getPhoneme() == p2.getPhoneme()){
return true;
}else{
return false;
}
}
deinit{
print("Phoneme deinitializing");
}
}
//
// ViewController.swift
import UIKit
class ViewController: UIViewController {
let DEBUGGING = true;
@IBAction func buttonTapped(){
print("hello")
let sampleNeme = Phoneme();
sampleNeme.setPhoneme("AH");
print(sampleNeme.getPhoneme());
buildWords();
}
func buildWords(){
var phonemes = [Phoneme]();
var linesOfDictionary = [String]();
let path = NSBundle.mainBundle().pathForResource("cmudict-0.7b_modified", ofType: "txt");
var i = 0;
//reads in the dictionary's original text file
if let aStreamReader = StreamReader(path: path!) {
defer {
aStreamReader.close()
}
while let line = aStreamReader.nextLine() {
linesOfDictionary.append(line);
debugPrint(linesOfDictionary[i]);
i = i + 1;
}
}
//takes the read in original text file and breaks each line into word names and lists of phonemes; sets up resources for the dictionary.
var wordNames = [String]();
var listsOfPhonemesForWords = [[Phoneme]]();
var word = "";
debugPrint(linesOfDictionary.count);
var spacesToSkip = 2;
for(var i = 0; i < linesOfDictionary.count; i++){
spacesToSkip = 2;
let lineBeingExamined = linesOfDictionary[i];
var indexOfCharBeingExamined = 0;
for (var j = 0; j < lineBeingExamined.characters.count; j++){
indexOfCharBeingExamined = j;
let charBeingExamined = lineBeingExamined[lineBeingExamined.startIndex.advancedBy(indexOfCharBeingExamined)];
if(charBeingExamined != " " && charBeingExamined != "("){
word = word + String(charBeingExamined);
}else if(charBeingExamined == "("){
spacesToSkip = spacesToSkip + 3;
wordNames.append(word);
word = "";
break;
}else{
wordNames.append(word);
word = "";
break;
}
}
print(i);
indexOfCharBeingExamined = indexOfCharBeingExamined + spacesToSkip;
var phoneme = Phoneme();
var phonemeName = "";
//creates list of phonemes
let lineBeingExaminedChars = lineBeingExamined.utf16;
for(var k = indexOfCharBeingExamined; k < lineBeingExaminedChars.count; k++){
let charBeingExamined = lineBeingExaminedChars[lineBeingExaminedChars.startIndex.advancedBy(k)];
if(NSCharacterSet.letterCharacterSet().characterIsMember(charBeingExamined)) {
phonemeName = phonemeName + String(lineBeingExamined[lineBeingExamined.startIndex.advancedBy(k)]);
}else if NSCharacterSet.decimalDigitCharacterSet().characterIsMember(charBeingExamined) {
let stress16 = charBeingExamined;
let stress = Int(stress16);
phoneme.setStress(stress);
}else if(String(charBeingExamined) == " "){
phoneme.setPhoneme(phonemeName);
phonemes.append(phoneme);
phoneme = Phoneme();
phonemeName = "";
}else{}
}
//for the last phoneme
phoneme.setPhoneme(phonemeName);
phonemes.append(phoneme);
phoneme = Phoneme();
phonemeName = "";
listsOfPhonemesForWords.append(phonemes);
}
//builds dictionary
var anchorWords = [Word]();
for(var f = 0; f < wordNames.count; f++){
anchorWords.append(Word(wordName:(wordNames[f].lowercaseString), phonemes: listsOfPhonemesForWords[f]));
}
print("hello");
print(anchorWords[145].getWordName());
//now put this list of Words into a trie
}
func debugPrint(obj: AnyObject){
if(DEBUGGING){
print(obj);
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//
// Word.swift
import Foundation
class Word {
private var wordName = "";
private var listOfPhonemes = [Phoneme]();
private var wordsThisRhymesWith = [WordIndexRhymePercentilePair]();
private var numOfSyllables = 0;
init (wordName: String, phonemes: [Phoneme]) {
self.wordName = wordName;
self.listOfPhonemes = phonemes;
}
func addWordThisRhymesWith(wordIndex: Int, rhymePercentile: Double){
let wordToBeInserted = WordIndexRhymePercentilePair(wordIndex: wordIndex, rhymePercentile: rhymePercentile);
wordsThisRhymesWith.append(wordToBeInserted);
//TODO add way of sorting these
}
func getWordName() -> String {
return wordName;
}
func setWordName(wordName: String) {
self.wordName = wordName;
}
func getListOfPhonemes() -> [Phoneme]{
return listOfPhonemes;
}
func setListOfPhonemes(listOfPhonemes: [Phoneme]) {
self.listOfPhonemes = listOfPhonemes;
}
func getWordsThisRhymesWith() -> [WordIndexRhymePercentilePair]{
return wordsThisRhymesWith;
}
func setWordsThisRhymesWith(wordsThisRhymesWith: [WordIndexRhymePercentilePair]) {
self.wordsThisRhymesWith = wordsThisRhymesWith;
}
deinit{
print("Word deinitializing");
}
}
//
// WordIndexRhymePercentilePair.swift
import Foundation
class WordIndexRhymePercentilePair {
private var wordIndex = 0, rhymePercentile = 0.0;
init(){}
init(wordIndex: Int, rhymePercentile: Double){
self.wordIndex = wordIndex;
self.rhymePercentile = rhymePercentile;
}
func setWordIndex(wordIndex: Int){
self.wordIndex = wordIndex;
}
func setRhymePercentile(rhymePercentile: Double){
self.rhymePercentile = rhymePercentile;
}
func getWordIndex() -> Int{
return wordIndex;
}
func getRhymePercentile() -> Double{
return rhymePercentile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment