国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

尋找只有一個(gè)輔音不同的單字在大型單字清單中的方法
P粉757640504
P粉757640504 2023-08-15 16:26:13
0
1
740
<p>我有一個(gè)近5000個(gè)「幻想」單字的列表,這些單字以ASCII文字形式書寫。其中一些單字如下:</p> <pre class="brush:php;toolbar:false;">txintoq txiqbal txiqfun txiqwek txiqyal txiyton txonmiq txoqwul txoqxik</pre> <p>我想設(shè)計(jì)一個(gè)演算法,檢查/驗(yàn)證清單中沒有兩個(gè)單字之間只相差一個(gè)「相似子音」。因此,我會(huì)像這樣定義「相似子音集合」(暫時(shí)):</p> <pre class="brush:php;toolbar:false;">zs xj pb td kg</pre> <p><em>一個(gè)集合中可能有3個(gè)或更多輔音,但我現(xiàn)在只展示2個(gè)。隨著我對幻想語言音調(diào)中哪些輔音聽起來相似的了解越來越深入,我需要進(jìn)一步調(diào)整這個(gè)定義。 </em></p> <p>因此,像下面這樣的單字將被標(biāo)記為「需要修正」(因?yàn)樗鼈兟犉饋硖嗨疲?lt;/p> <pre class="brush:php;toolbar:false;">txindan txintan # 只有d/t不同 xumaq jumaq # 只有x/j不同 dolpar dolbar # 只有a b/p不同</pre> <p>我如何在我的約5000個(gè)單字清單中以<em>相對高效</em>的方式找到這些只相差一個(gè)輔音的單字? </p> <p>這是我目前所想到的一個(gè)非常天真的解決方法,如下所示:</p> <pre class="brush:php;toolbar:false;">import fs from 'fs' const terms = fs .readFileSync('term.csv', 'utf-8') .trim() .split(/n /) .map(line => { let [term] = line.split(',') return term }) .filter(x => x) const consonantSets = ` zs xj pb td kg` .split(/n /) .map(x => x.split('')) function computeSimilarTerms( term: string, consonantSets: Array<Array<string>>, ) { const termLetters = term?.split('') ?? [] const newTerms: Array<string> = [] for (const consonantSet of consonantSets) { for (const letter of consonantSet) { for (const letter2 of consonantSet) { if (letter === letter2) { continue } let i = 0 while (i < termLetters.length) { const termLetter = termLetters[i] if (termLetter === letter) { const newTerm = termLetters.concat() termLetters[i] = letter2 newTerms.push(newTerm.join('')) } i } } } } return newTerms } for (const term of terms) { const similarTerms = computeSimilarTerms(term, consonantSets) similarTerms.forEach(similarTerm => { if (terms.includes(similarTerm)) { console.log(term, similarTerm) } }) }</pre> <p>如何以相對較少的蠻力方式完成這個(gè)任務(wù)?而且這個(gè)解決方法還不完整,因?yàn)樗鼪]有建構(gòu)<em>所有可能相似的單字組合</em>。所以在演算法的某個(gè)地方,它應(yīng)該能夠做到這一點(diǎn)。有什麼想法嗎? </p>
P粉757640504
P粉757640504

全部回覆(1)
P粉238433862

在每個(gè)組中選擇一個(gè)子音作為該組的「代表」。然後,建立一個(gè)將單字分組在一起的映射,當(dāng)它們的輔音被代表輔音替換時(shí),它們變得相同。

重要提示:此方法僅在子音組形成等價(jià)類別時(shí)有效。特別是,輔音的相似性必須是傳遞的。如果'bp'相似,'bv'相似,但'pv'不相似,則此方法無效。

以下是用Python範(fàn)例的程式碼; 我讓你寫JavaScript程式碼。

  • f是一個(gè)將每個(gè)子音對應(yīng)到其代表子音的對映;
  • d是一個(gè)將每個(gè)代表單字對應(yīng)到具有此代表的單字清單的對應(yīng)。
bigwordlist = '''dolbar
dolpar
jumaq
txindan
txintan
txintoq
txiqbal
txiqfun
txiqwek
txiqyal
txinton
txonmiq
txoqwul
txoqxik
xumaq'''.splitlines()

consonant_groups = '''zs
xj
pb
td
kg'''.splitlines()

f = {}
for g in consonant_groups:
    for c in g:
        f[c] = g[0]

print(f)
# {'z': 'z', 's': 'z', 'x': 'x', 'j': 'x', 'p': 'p', 'b': 'p', 't': 't', 'd': 't', 'k': 'k', 'g': 'k'}
    
d = {}
for word in bigwordlist:
    key = ''.join(f.get(c, c) for c in word)
    d.setdefault(key, []).append(word)

print(d)
# {'tolpar': ['dolbar', 'dolpar'], 'xumaq': ['jumaq', 'xumaq'], 'txintan': ['txindan', 'txintan'], 'txintoq': ['txintoq'], 'txiqpal': ['txiqbal'], 'txiqfun': ['txiqfun'], 'txiqwek': ['txiqwek'], 'txiqyal': ['txiqyal'], 'txinton': ['txinton'], 'txonmiq': ['txonmiq'], 'txoqwul': ['txoqwul'], 'txoqxik': ['txoqxik']}

最後,我們可以看到哪些單字是相似的:

print([g for g in d.values() if len(g) > 1])
# [['dolbar', 'dolpar'], ['jumaq', 'xumaq'], ['txindan', 'txintan']]
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板