0

I have a for loop currently that will go through a list of paragraphs separated by word, psw_list, randomly select a word paragraph, and then change that word to another of the same part of speech. You'll notice the bottom else statement is blank because I want it to go back to the beginning of the if statement if the part of speech is any of the ones listed, but I'm not sure how, any ideas? Sorry if the code snippet is long.

for (i in iterations){ # for a specific change in the total amount of changes
  n <- sample(names(psw_list), 1) 
  j <- sample(length(psw_list[[n]]), 1) 
  parsed <- spacy_parse(psw_list[[n]][j])
  pos <- parsed[1,6]
    # tabbing because the if starts here
  if (pos == "NOUN"){
    sample(nouns, 1) -> replacement
    as.character(replacement) -> replacement
    psw_list[[n]][j] <- replacement
  } else if (pos == "ADJ"){
    sample(adjs, 1) -> replacement
    as.character(replacement) -> replacement
    psw_list[[n]][j] <- replacement
  } else if (pos == "ADV"){
    sample(adverbs, 1) -> replacement
    as.character(replacement) -> replacement
    psw_list[[n]][j] <- replacement
  } else if (pos == "PROPN"){
    sample(proper_nouns, 1) -> replacement
    as.character(replacement) -> replacement
    psw_list[[n]][j] <- replacement
  } else if (pos == "ADP"){
    sample(prepositions, 1) -> replacement
    as.character(replacement) -> replacement
    psw_list[[n]][j] <- replacement
  } else if (pos == "VERB"){
    # Need to add nested if to determine if it's past or present tense
    sample(verbs_past, 1) -> replacement
    as.character(replacement) -> replacement
    psw_list[[n]][j] <- replacement
  } else if( word.pos == "AUX" || "CONJ" || "DET" || "INTJ" || "NUM" || "PART" || "PROPN" || "PUNCT" 
             || "SCONJ" || "SYM" || "X") {
   
  }
}
djc1223
  • 45
  • 1
  • 6
  • Couldn't you just reset the index variable (i in your case) to the position that you want (so whatever the first value of iteration is)? – nilesguo Jun 26 '20 at 14:06
  • @nilesguo well iteration is a user defined variable so I don't really wanna do that – djc1223 Jun 26 '20 at 14:17
  • @nilesguo That would not even work, even if OP was willing to do it. I think https://stackoverflow.com/a/42983545/8386140 would be instructive for both of you. @djc1223, you probably want a different flow control setup than a `for` loop, likely a `while` loop as demonstrated in the linked answer – duckmayr Jun 26 '20 at 14:26

1 Answers1

1

Seems like you should just avoid proceeding instead of trying 'go back'. Nest a repeat loop around the first few lines that select the word until you find one that's handled

for (i in iterations){ # for a specific change in the total amount of changes

  repeat{

    n <- sample(names(psw_list), 1) 
    j <- sample(length(psw_list[[n]]), 1) 
    parsed <- spacy_parse(psw_list[[n]][j])
    pos <- parsed[1,6]
  
    if(pos %in% LIST_OF_ACCEPTABLE_POS) break;

  }

...rest of for loop...

}

``
Marcus
  • 3,478
  • 1
  • 7
  • 16
  • I've been playing with this for awhile and I'm realizing that the break statement breaks the entire loop, it doesn't send it back to the beginning, any ideas? – djc1223 Jun 30 '20 at 14:55
  • You will need `break` at some point to get our of the `repeat` loop (otherwise it will be an infinite loop. That being said, it sounds like you are looking for the `next` command – Marcus Jul 01 '20 at 14:25