Program to sort out phrases based on their appearances in Python

PythonServer Side ProgrammingProgramming

Suppose, we are given two lists; 'phrases' that contain a few selected phrases and 'sentences' that contain several sentences that may or may not contain the phrases from the other list. We have to find out if the various phrases in the first list appear in the second list and sort the first list phrases based on their appearances in the second list. We return the sorted list 'phrases' as output.

So, if the input is like phrases = ['strong', 'durable', 'efficient'], sentences = ['the product is durable and efficient', 'strong and durable', 'it is efficient', 'like it because it is efficient'], then the output will be ['efficient', 'durable', 'strong']

The phrase 'efficient' appears in sentences 0, 2, and 4. It has the most appearances, so it is at the beginning of the output. The phrases 'durable' and 'strong' appears in sentence 0 and 1, and 1 respectively. So, these phrases acquire the next positions in the output.

To solve this, we will follow these steps −

  • cnt := a new map
  • for each feature in phrases, do
    • cnt[feature] := 0
  • for each response in sentences, do
    • p := a new list that contain the words of a response
    • s := a new set from p
    • for each i in s, do
      • if i is present in cnt, then
        • cnt[i] := cnt[i] + 1
  • res := a new list containing pairs (k, cnt[k]) for every k in cnt
  • sort the list res based on the count k
  • return the list res without including the count value k

Example

Let us see the following implementation to get better understanding −

def solve(phrases, sentences):
   cnt = {}
   for feature in phrases:
      cnt[feature] = 0
   for response in sentences:
      p = response.split()
      s = set(p)
      for i in s:
         if i in cnt:
            cnt[i] += 1
   res = [[k, cnt[k]] for k in cnt]
   res.sort(key = lambda x:(-x[1], phrases.index(x[0])))
   return [i[0] for i in res]

print(solve(['strong', 'durable', 'efficient'], ['the product is durable and efficient', 'strong and durable', 'it is efficient', 'like it because it is efficient']))

Input

['strong', 'durable', 'efficient'],
['the product is durable and efficient', 'strong and durable', 'it is
efficient', 'like it because it is efficient']

Output

['efficient', 'durable', 'strong']
raja
Published on 07-Oct-2021 11:59:00
Advertisements