我有一個帶有句子的字串和一個帶有一些單詞的陣列,我需要對這個陣列進行排序,以便其中包含的單詞根據它們在字串中出現的頻率而降序。
前任:
str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
array = ["a", "em", "i", "el"]
X = 3
arrayFrequency = ["i", "a", "em", "el"] // They repeat 11, 7, 2, 1 respectively
XarrayFrequency = ["i", "a", "em"] // The first X words most repeated
我嘗試過這種方式,但我的大腦停止了,我想不出辦法繼續:
str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
array = ["a", "em", "i", "el"]
X = 3
for(let word of words) {
console.log(s.split(word).length - 1)
} // output: 7 2 11 1
我期待輸出回傳 ["i", "a", "em", "el"] 所以我可以輕松地從陣列中獲取前 X 個單詞
例如:X = 3 XarrayFrequency = ["i", "a", "em"]
uj5u.com熱心網友回復:
const s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
const m = ["a", "em", "i", "el"], x = 3;
// given a string s and substring match m, output the occurrences of m in s
const c = (s, m) => [...s].filter((e,i)=>s.substring(i).startsWith(m)).length;
// given an array of substring matches, get the occurrences of each substring
// and place them in an array as a list of [substring, occurences]
// then sort the array by occurrences descending, take the first x entries,
// and finally extract only the substring out of the [substring, occurences] pairs
const f = (s, m, x) => m.map(i=>[i, c(s,i)]).sort(([a,b],[c,d])=>d-b)
.slice(0,x).map(i=>i[0]);
console.log(f(s, m, 3));
uj5u.com熱心網友回復:
使用正則運算式查找出現的情況。
const str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
const words = ["a", "em", "i", "el"];
const x = 3;
// Dictionary to store counts
var resDict = {}
for (let word of words)
{
// Use regex to find all possible matches
var re = new RegExp(word, 'g');
count = (str.match(re) || []).length;
resDict[word] = count; // store the count for each word
}
// Create the array of key-value pairs
resArray = Object.keys(resDict).map((key) => { return [key, resDict[key]] });
// Sort the array in descending order
resArray.sort(
(first, second) => { return second[1] - first[1] }
);
// Get the list of keys in sorted order of the values
var keys = resArray.map((e) => { return e[0] });
// Print the array based on the number needed
console.log(keys.slice(0, x));
uj5u.com熱心網友回復:
你可以把你想做的事情分成3個步驟:
步驟 1.計算句子中每個單詞的頻率。
function countWords(words, sentence) {
const counts = words.map(word => {
const regex = new RegExp(word, "g")
const count = (sentence.match(regex) || []).length
return count
})
return counts
}
步驟 2.根據上一步的頻率對單詞進行排序。
function sortWords(words, counts) {
const sortedWords = words.slice()
sortedWords
.sort((a, b) => {
aIndex = sortedWords.indexOf(a)
bIndex = sortedWords.indexOf(b)
return counts[aIndex] - counts[bIndex]
})
.reverse()
return sortedWords
}
第 3 步。最后一步是呼叫這 2 個函式并使用slice來獲取 x 最常見的單詞。
sentence = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
words = ["a", "em", "i", "el"]
counts = countWords(words, sentence)
sortedWords = sortWords(words, counts)
const x = 3
// should print: ["i", "a", "em"]
console.log(sorted.slice(0, x))
將代碼拆分為多個函式是一種很好的做法,這樣您就不會被您想做的任何事情所淹沒。這也導致了可測驗且更具可讀性的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/527833.html
