-3

たとえば、次のような文字列があります。

"Xin chao moi nguoi"

そして、プログラムでその文字列を次のように変換したいと思います。

"Xin chào mọi người"

研究を始める方法の大まかなアイデアが必要です。なにか提案を?

これは翻訳ではありません。以下の文字列は両方とも1つの言語からのものですが、最初の文字列は発音区別符号を見逃しています。このプログラムは、発音区別符号を英語の文字に追加します。

a->ă

4

5 に答える 5

3

あなたの状況に対処するための特定のライブラリがあるかどうかはわかりませんが、Ruby のすべてに精通しているわけではありません。ただし、次のようなことを試すことができます。

特別にエンコードされた単語のリストと、基本文字から複雑文字へのマッピングを作成します。

次に、候補単語の基本的なエンコーディングを取得したら、文字ごとに一致する単語のリストをフィルター処理し、現在の文字と正確に一致する単語、または以前に定義されたマッピングで許容可能な代替がある単語を保持します。 .

簡単で汚い例:

wordList = ['chào', 'chao', 'chaobella'] #etc...
charMap = {'a'=>['a', 'à'], 'c'=>['c'], 'h'=>['h'], 'o'=>['o', 'ọ', 'ờ']} # etc...

word = 'chao'
filterList = wordList

# for each character in the word
word.split("").each_with_index do |char, index|
  # get the list of characters you'd like it to match
  matchingChars = charMap[char]
  # select the words that match at this index
  filterList = filterList.select{ |w| matchingChars.include? w.split("")[index] }
end

# filter out longer words
filterList = filterList.select{ |w| word.length == w.length }

puts "Words that match: " + filterList.join(",")

それを関数に分解し、一致する単語が複数ある場合にどうするかを決定する必要があります。

于 2013-01-18T14:33:56.377 に答える
2

Unicode結合文字を使用して、基本文字から結果の文字を作成できます。

   a   +   ̀      = à
U+0061 + U+0300

Unicode分解、つまり正規化形式の標準形分解(NFD)を使用して結合文字を取得できます。逆に、複数の文字を1つの文字に構成するのが、正規化形式の正規化構成(NFC)です。

このRubyUnicodeライブラリを合成と分解に使用できます。

于 2013-01-18T15:14:07.503 に答える
1

Depends on how much you want to do. If you just encode your strings in UTF-8 (start your file with #encoding: utf-8), you can make a rudimentary changing dictionary:

conversions = {
  "a" => "á",
  "e" => "é"
}
#And so on... (or with words, as @Hannele said)
str.gsub(/[aeiou]/, conversions)

Feel free to replace any of the conversions to how you see fit. Remember, though, this will replace all vowels in the string. It won't do what makes sense for the human language.

于 2013-01-18T14:40:54.530 に答える
1

1 対 1 の文字変換には、trメソッドがあります。

#encoding: utf-8
p "Xin chao moi nguoi".tr('aou', 'àọư')
#=>"Xin chàọ mọi ngưọi"
于 2013-01-18T16:07:30.817 に答える
-1

あなたは英語をベトナム語に翻訳しています。

Google API を使用して文字列をある言語から別の言語に翻訳するRuby gem 。

于 2013-01-18T14:27:48.070 に答える