Java преобразование языка - PullRequest
0 голосов
/ 30 апреля 2020

это код, который я использую для перевода некоторого текста на английский язык sh с латинского языка pig и наоборот. За исключением того, что метод fromPig, похоже, не работает правильно.

Ожидаемый результат: «java - замечательный язык программирования, и объектно-ориентированное программирование - лучшая вещь после нарезанного хлеба».

Получил вывод: "avajavajay isyisyay ayay onderfulwonderfulway ogrammingprogrammingpray ..."

Итак, вы можете видеть, что слова находятся внутри, но мне нужно избавиться от других частей на концах слова , Пожалуйста, попробуйте предоставить код, если вы можете исправить мою ошибку.

public class PigLatin {
    public String fromPig(String pigLatin) {
        String res="";
        String[] data=pigLatin.split(" ");
        for(String word : data)
        res += toEnglishWord(word) + " ";
        return res;
    }

    private String toEnglishWord(String word) {
        char punc=0;
        for(int i=0;i<word.length();i++) {
            if(word.charAt(i)=='.'||word.charAt(i)==','||word.charAt(i)==';'||word.charAt(i)=='!') {
                punc=word.charAt(i);
                break;
            }
        }
        word=word.replace(punc + "","");
        String[] data=word.split("-");
        String firstPart=data[0];
        String lastPart=data[0];
        if(lastPart.equals("yay"))
        return firstPart + punc ;
        else {
            lastPart=lastPart.replace("ay","");
            return(lastPart+firstPart+punc);
        }
    }
}

Это класс, который должен выполнять предложения.

public class Convert {
    public static void main(String args []) {
        PigLatin demo=new PigLatin();
        String inEnglish="Now is the winter of our discontent " +
            "Made glorious summer by this sun of York; " +
            "And all the clouds that lour'd upon our house " +
            "In the deep bosom of the ocean buried.";
        String inPigLatin="avajay isyay ayay onderfulway ogrammingpray " +
            "anguagelay, andyay objectyay orientedyay ogrammingpray " +
            "isyay hetay estbay ingthay afteryay icedslay eadbray.";
        System.out.println(demo.toPig(inEnglish));
        System.out.println(demo.fromPig(inPigLatin));
    }
}      

В основном предложение engli sh должно быть преобразованным в латинскую свинью, и предложение латинской свиньи должно быть преобразовано в английский sh.

Engli sh к пиглатину делается правильно. Но пиглатин по-английски sh не является.

Ответы [ 2 ]

1 голос
/ 30 апреля 2020

Устраните следующие проблемы:

  1. Преобразование текста в один регистр (например, в нижний регистр), поскольку вы сравниваете только с гласными строчными буквами.
  2. Код внутри toEnglishWord не является правильным. Измените его следующим образом:
private String toEnglishWord(String word) {
    char punc = 0;
    for (int i = 0; i < word.length(); i++) {
        if (word.charAt(i) == '.' || word.charAt(i) == ',' || word.charAt(i) == ';' || word.charAt(i) == '!') {
            punc = word.charAt(i);
            break;
        }
    }
    // Trim the word, and remove all punctuation chars
    word = word.trim().replaceAll("[\\.,;!]", "");
    String english = "";

    // If the word ends with 'yay', remove 'yay' from its end. Otherwise, if the
    // word ends with 'ay', form the word as (3rd last letter + characters from
    // beginning till the 4th last character). Also, add the punctuation at the end
    if (word.length() > 2 && word.substring(word.length() - 3).equals("yay")) {
        english = word.substring(0, word.length() - 3) + String.valueOf(punc);
    } else if (word.length() > 3 && word.substring(word.length() - 2).equals("ay")) {
        english = word.substring(word.length() - 3, word.length() - 2) + word.substring(0, word.length() - 3)
                + String.valueOf(punc);
    }
    return english;
}

Демо:

class PigLatin {
    public String toPig(String english) {
        String res = "";
        String[] data = english.toLowerCase().split(" ");
        for (String word : data)
            res += toPigWord(word) + " ";
        return res;
    }

    public String fromPig(String pigLatin) {
        String res = "";
        String[] data = pigLatin.toLowerCase().split(" ");
        for (String word : data)
            res += toEnglishWord(word) + " ";
        return res;
    }

    private String toPigWord(String word) {
        char punc = 0;
        for (int i = 0; i < word.length(); i++) {
            if (word.charAt(i) == '.' || word.charAt(i) == ',' || word.charAt(i) == ';' || word.charAt(i) == '!') {
                punc = word.charAt(i);
                break;
            }
        }
        word = word.replace(punc + "", "");
        if (isFirstLetterVowel(word))
            return (word + "yay" + punc);
        else {
            int indexVowel = indexOfFirstVowel(word);
            String after = word.substring(indexVowel);
            String before = word.substring(0, indexVowel);
            return (after + before + "ay" + punc);
        }
    }

    private String toEnglishWord(String word) {
        char punc = 0;
        for (int i = 0; i < word.length(); i++) {
            if (word.charAt(i) == '.' || word.charAt(i) == ',' || word.charAt(i) == ';' || word.charAt(i) == '!') {
                punc = word.charAt(i);
                break;
            }
        }
        // Trim the word, and remove all punctuation chars
        word = word.trim().replaceAll("[\\.,;!]", "");
        String english = "";

        // If the word ends with 'yay', remove 'yay' from its end. Otherwise, if the
        // word ends with 'ay' and form the word as (3rd last letter + characters from
        // beginning to the 4th last character). Also, add the punctuation at the end
        if (word.length() > 2 && word.substring(word.length() - 3).equals("yay")) {
            english = word.substring(0, word.length() - 3) + String.valueOf(punc);
        } else if (word.length() > 3 && word.substring(word.length() - 2).equals("ay")) {
            english = word.substring(word.length() - 3, word.length() - 2) + word.substring(0, word.length() - 3)
                    + String.valueOf(punc);
        }
        return english;
    }

    private boolean isFirstLetterVowel(String word) {
        String temp = word.toLowerCase();
        return (temp.charAt(0) == 'a' || temp.charAt(0) == 'e' || temp.charAt(0) == 'i' || temp.charAt(0) == 'o'
                || temp.charAt(0) == 'u');
    }

    private int indexOfFirstVowel(String word) {
        int index = 0;
        String temp = word.toLowerCase();
        for (int i = 0; i < temp.length(); i++) {
            if (temp.charAt(i) == 'a' || temp.charAt(i) == 'e' || temp.charAt(i) == 'i' || temp.charAt(i) == 'o'
                    || temp.charAt(i) == 'u') {
                index = i;
                break;
            }
        }
        return index;
    }
}

class Main {
    public static void main(String[] args) {
        PigLatin pigLatin = new PigLatin();
        String str = "hello world! good morning! honesty is a good policy.";
        String strToPigLatin = pigLatin.toPig(str);
        System.out.println(strToPigLatin);
        System.out.println(pigLatin.fromPig(strToPigLatin));
    }
}

Вывод:

ellohay orldway! oodgay orningmay! onestyhay isyay ayay oodgay olicypay. 
hello world! good morning! honesty is a good policy. 

Примечание: С текущими логами c невозможно преобразовать слово типа ogrammingpray (которое является пиглатиной programming) обратно в programming.

0 голосов
/ 01 мая 2020

Невозможно преобразовать Pig Latin в Engli sh просто так - вам нужен человек, программа с доступом к базе данных слов или нейронный net или что-то, что было обучено на базе данных слов. Без этого вы будете преобразовывать слова обратно в английский sh только в том случае, если они изначально начинались с гласной буквы или их первая буква была согласной, а вторая буква - гласной. В противном случае у машины буквально нет возможности определить, где слово закончилось.

По этой причине вам нужно создать метод, который выдает List<String>, например:

public static List<String> allPossibleSentences(String inPigLatin) {
    List<String> pigWords = Arrays.asList(inPigLatin.split("\\s"));
    //You can also use a method reference here
    List<List<String>> possSentences = cartesianProduct(pigWords.stream().map(word -> possibleEnglishWords(word)).collect(Collectors.toList()));
    return possSentences.stream().map(words -> String.join(" ", words)).collect(Collectors.toList());
  }

Но так как вам нужен метод fromPig в вашем коде, вы можете написать его следующим образом:

public static String fromPig(String inPigLatin) {
  return allPossibleSentences(inPigLatin).get(0);
}

Это по сравнению с ответом Арвинда Кумара Авина sh, поскольку он генерирует все перестановки , но я думаю, что будет более полезно, если у вас есть наиболее вероятный случай и все возможные предложения.

Пример основного метода

public static void main(String[] args) {
    String inPigLatin="avajay isyay ayay onderfulway ogrammingpray " +
            "anguagelay andyay objectyay orientedyay ogrammingpray " +
            "isyay hetay estbay ingthay afteryay icedslay eadbray";
    System.out.println(fromPig(inPigLatin));
    inPigLatin = "icedslay eadbray";
    System.out.println("\nExpected = sliced bread, gotten = " + fromPig(inPigLatin));
    System.out.println(String.join("\n", allPossibleSentences(inPigLatin)));
}

Пример вывода

java is a wonderful rogrammingp language and object oriented rogrammingp is the best hingt after liceds readb

Expected = sliced bread, gotten = liceds readb
liceds readb
liceds bread
liceds dbrea
sliced readb
sliced bread //Here you have the right answer, but your method has no way to confirm that
sliced dbrea
dslice readb
dslice bread
dslice dbrea

Ваш класс PigLatin теперь

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

class PigLatin {

//Put your toPig and toPigWord methods here (I couldn't find them)

public static String fromPig(String inPigLatin) {
  return allPossibleSentences(inPigLatin).get(0);
}

/* Methods that above code relies on */
private static List<String> possibleEnglishWords(String word) {
    List<String> possibilities = new ArrayList<>();
    if (word.matches(".*yay$")) {
      possibilities.add(word.substring(0, word.length() - 3));
      return possibilities;
    }

    //Remove the pig latin part
    word = word.substring(0, word.length() - 2);
    for (int i = word.length() - 1; i >= 0; i --) {
      if (isVowel(word.charAt(i))) break;
      if (word == "anguagel") System.out.println("char = " + word.charAt(i));
      possibilities.add(word.substring(i) + word.substring(0, i));
    }
    return possibilities;
  }

  //Put all the words together
  public static List<List<String>> cartesianProduct(List<List<String>> possWordArr) {
      if (possWordArr.size() == 1) {
        List<List<String>> possSentencesAsWords = new ArrayList<>();
        possSentencesAsWords.add(possWordArr.get(0));
        return possSentencesAsWords;
      }
      return _cartesianProduct(0, possWordArr);
  }

  //Helper method
  private static List<List<String>> _cartesianProduct(int index, List<List<String>> possWordArr) {
      List<List<String>> ret = new ArrayList<>();
      if (index == possWordArr.size()) {
          ret.add(new ArrayList<>());
      } else {
          for (String word : possWordArr.get(index)) {
              for (List<String> words : _cartesianProduct(index + 1, possWordArr)) {
                  words.add(0, word);
                  ret.add(words);
              }
          }
      }
      return ret;
  }

  private static boolean isVowel(char c) {
    c = toUppercase(c);
    switch (c) {
      case 'A':
      case 'E':
      case 'I':
      case 'O':
      case 'U':
        return true;
      default:
        return false;
    }
  }

  private static char toUppercase(char c) {
    if (c >= 'a') return (char) (((char) (c - 'a')) + 'A');
    else return c;
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...