-2

I am looking for any proper solution to check number of occurrence of word in the sentences.

Input:
k = 2
keywords = ["anacell", "cetracular", "betacellular"]
reviews = [
  "Anacell provides the best services in the city",
  "betacellular has awesome services",
  "Best services provided by anacell, everyone should use anacell",
]

Output:
["anacell", "betacellular"]

Explanation:
"anacell" is occuring in 2 different reviews and "betacellular" is only occuring in 1 review.

Input:
k = 2
keywords = ["anacell", "betacellular", "cetracular", "deltacellular", "eurocell"]
reviews = [
  "I love anacell Best services; Best services provided by anacell",
  "betacellular has great services",
  "deltacellular provides much better services than betacellular",
  "cetracular is worse than anacell",
  "Betacellular is better than deltacellular.",
]

Output:
["betacellular", "anacell"]

Explanation:
"betacellular" is occuring in 3 different reviews. "anacell" and "deltacellular" are occuring in 2 reviews, but "anacell" is lexicographically smaller.

based on the keywords value i would like to check their occurrences in the reviews array and want to display the numbers based on which keyword occured more.

Bravo
  • 61
  • 2
  • 7
  • 26

4 Answers4

2
countKeywords(keywords: string[], reviews: string[]) => {
    let result = [];

    keywords.forEach(key => {
        let data = {};
        data.keyword = key;
        data.review_count = 0;

        reviews.forEach(review => {
            if (review.toLowerCase().includes(key.toLowerCase())) {
                data.review_count += 1;
            }
        })

        result.push(data);
    })

    return result;
}

Returns an Object Array with corresponding values.

enter image description here

jamesmallred
  • 206
  • 1
  • 6
1

you can use the below code to find the number of occurrences of the keywords.

let map:any
this.keywords.forEach((keyword)=>
{
  let temp=0;
 this.reviews.forEach((review)=>{
   if(review.indexOf(keyword)>-1){
     temp++
    }
 });
  map[keyword]=temp
})

console.log(map['anacell'])
Minal Shah
  • 1,402
  • 1
  • 5
  • 13
1

const keywords = ["anacell", "cetracular", "betacellular"];
const reviews = [
    "Anacell provides the best services in the city",
    "betacellular has awesome services",
    "Best services provided by anacell, everyone should use anacell",
];

let occurences = [];
for (const kw of keywords)
    for (const rw of reviews) {
        const lower = rw.toLowerCase();
        const lower2 = kw.toLowerCase();
        if (occurences.indexOf(lower2) < 0 && lower.indexOf(lower2) >= 0)
            occurences.push(kw);
    }
console.log(occurences);
Locoder
  • 22
  • 1
  • 4
1

used jamesmallred to update sorting and splice methods.

countKeywords(keywords: string[], reviews: string[]) => {
    let result = [];

    keywords.forEach(key => {
        let data = {};
        data.keyword = key;
        data.count = 0;

        reviews.forEach(review => {
            if (review.toLowerCase().includes(key.toLowerCase())) {
                data.count += 1;
            }
        })

        result.push(data);
    })

    return result;
}

//sorting 

 result.sort((a,b) => {
            if(b.keyword === a.keyword) {
                return a.keyword.localeCompare(b.keyword); // if equal - sort alphabetically
            }
            else {
                return b.keyword  - a.keyword  // we want to sort from highest to low 
            }

        });
        
        
//splice based on the input

result.splice(0,k);
a.p. patel
  • 103
  • 4
  • 25
  • 1
    For this instance, you would want to sort recursively kinda like I processed the data. The first sort to sort based on review count and then another sort to alphabetize the keywords if the review count was equal. I also don’t think the splice is necessary. – jamesmallred Jul 25 '20 at 00:20
  • I have added splice as bravo mentioned k =2 means based on k it will show the most higher number of data which is mentioned in example 2 of question. – a.p. patel Jul 25 '20 at 03:18
  • In example 2 output , there are only 2 data based on higher to lower as well as Alphabetic order ( if same size) as k=2 mentioned in input. @bravo correct me if I am wrong – a.p. patel Jul 25 '20 at 03:20