如何拆分字符串然后编辑字符串然后将分隔符放回原始位置

时间:2016-02-11 17:31:44

标签: java regex string split apache-pig

我正在研究Pig Latin项目,该项目需要更改用户输入的任何句子以便翻译成Pig Latin。我有转换,它的工作原理。但是我有标点问题。当我分割我的字符串以处理字符串中的每个单词时,标点符号会受到阻碍。我想知道一种方法,能够将字符串输入分成单个单词,但保留分隔符,然后能够正确地放回标点符号和空格?

谢谢

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a word or phrase: ");
    String convert = scanner.nextLine();
    String punctuations = ".,?!;";
    //convert = convert.replaceAll("\\p{Punct}+", ""); //idk if this is useful for me 
    String finalSentence = "";
    if (convert.contains(" ")) {
        String[] arr = convert.split("[ ,?!;:.]+");
        for (int index = 0; index < arr.length; index++) {
                if (vowel(arr[index]) == true) {
                    System.out.println(arr[index] + "yay");
                    finalSentence = (finalSentence + arr[index] + "yay ");
                } else {
                    System.out.println(newConvert(arr[index]));
                    finalSentence = (finalSentence + newConvert(arr[index]) + " ");
                }

        } 

2 个答案:

答案 0 :(得分:0)

...

  constructor(
    private routeData: RouteData,
    private router: Router
  ) {
    this.foo = this.routeData.get('foo');

    this.router.config([
      {
        path: '/nested-z',
        name: 'NestedZ',
        component: NestedChildComponent,
        data: {parentFoo: this.foo}
      }
    ]);

    // router.config should trigger "renavigate"

  }
...

使用 public static void main(String[] args) { String convert = "The quick? brown!!fox jumps__over the lazy333 dog."; StringBuilder finalSentence = new StringBuilder(); List<String> tokens = Arrays.asList(convert.split("")); Iterator<String> it = tokens.iterator(); while (it.hasNext()) { String token = it.next(); StringBuilder sb = new StringBuilder(); while (token.matches("[A-Za-z]")) { sb.append(token); if (it.hasNext()) { token = it.next(); } else { token = ""; break; } } String word = sb.toString(); if (!word.isEmpty()) { finalSentence.append(magic(word)); } finalSentence.append(token); } //prints "The1 quick1? brown1!!fox1 jumps1__over1 the1 lazy1333 dog1." System.out.println(finalSentence.toString()); } private static String magic(String word) { return word + 1; } 方法进行Pig拉丁语翻译。

答案 1 :(得分:0)

我为Pig Latin翻译定义了两种方法: convert_word_to_pig_latin 方法是将每个单词转换为Pig Latin, convert_sentence_to_pig_latin 方法用于使用convert_word_to_pig_latin的句子方法

def convert_word_to_pig_latin(word)
  vowels = "aeiou"
  punctuations = ".,?!'\":;-"

  if vowels.include?(word[0])
    return word
  else
    if punctuations.include?(word[-1])
      punctuation = word[-1]
      word = word.chop
    end
      first_vowel_index = word.chars.find_index { |letter| vowels.include?(letter) }
      new_word = word[first_vowel_index..-1] + word[0...first_vowel_index] + "ay"
      return punctuation ? new_word += punctuation : new_word
  end
end



def convert_sentence_to_pig_latin(sentence)
  sentence_array = sentence.split(" ")
  sentence_array.map { |word| convert_word_to_pig_latin(word) }.join(" ")
end

注意:请随意添加任何其他标点符号。

最后,这是我的RSpec,以确保我的两个方法都通过所有测试:

require_relative('../pig_latin')

describe 'Converting single words to Pig Latin' do
  word1 = "beautiful"
  word2 = "easy"
  word3 = "straight"

  it "converts word to Pig Latin" do
    expect(convert_word_to_pig_latin(word1)).to eq "eautifulbay"
  end

  it "does not change word if it begins with a vowel" do
    expect(convert_word_to_pig_latin(word2)).to eq "easy"
  end

  it "converts word to Pig Latin" do
    expect(convert_word_to_pig_latin(word3)).to eq "aightstray"
  end
end

describe 'Converting a sentence to Pig Latin' do
  sentence1 = "Make your life a masterpiece; imagine no limitations on what you can be, have, or do."
  sentence2 = "The pessimist sees difficulty in every opportunity. The optimist sees the opportunity in every difficulty."

  it "converts motivational quote from Brian Tracy to Pig Latin" do
    expect(convert_sentence_to_pig_latin(sentence1)).to eq "akeMay ouryay ifelay a asterpiecemay; imagine onay imitationslay on atwhay ouyay ancay ebay, avehay, or oday."
  end

  it "converts motivational quote from Winston Churchill to Pig Latin" do
    expect(convert_sentence_to_pig_latin(sentence2)).to eq "eThay essimistpay eessay ifficultyday in every opportunity. eThay optimist eessay ethay opportunity in every ifficultyday."
  end
end