Reordering list items based on score to match function curve

Given that I have:

  • word list
  • scores / scores that indicate "simplicity" for each word
  • difficulty levels of each word:

eg.

>>> words = ['apple', 'pear', 'car', 'man', 'average', 'older', 'values', 'coefficient', 'exponential']
>>> points = ['9999', '9231', '8231', '5123', '4712', '3242', '500', '10', '5']
>>> bins = [0, 0, 0, 0, 1, 1, 1, 2, 2]

Currently, the list of words is ordered by simplicity points.

What if I want to simulate simplicity as a “quadratic curve”? , i.e. from the highest to the lowest point, and then return to high, i.e. create a list of words that looks like this: corresponding points:

['apple', 'pear', 'average', 'coefficient', 'exponential', 'older', 'values', 'apple', 'pear']

I tried this, but it was painfully crazy:

>>> from collections import Counter
>>> Counter(bins)[0]
4
>>> num_easy, num_mid, num_hard = Counter(bins)[0], Counter(bins)[1], Counter(bins)[2]
>>> num_easy
4
>>> easy_words = words[:num_easy]
>>> mid_words = words[num_easy:num_easy+num_mid]
>>> hard_words = words[-num_hard:]
>>> easy_words, mid_words, hard_words
(['apple', 'pear', 'car', 'man'], ['average', 'older', 'values'], ['coefficient', 'exponential'])
>>> easy_1 = easy_words[:int(num_easy/2)]
>>> easy_2 = easy_words[len(easy_1):]
>>> mid_1 = mid_words[:int(num_mid/2)]
>>> mid_2 = mid_words[len(mid_1):]
>>> new_words = easy_1 + mid_1 + hard_words + mid_2 + easy_1 
>>> new_words
['apple', 'pear', 'average', 'coefficient', 'exponential', 'older', 'values', 'apple', 'pear']

Imagine not. from boxes> 3, or maybe I want to “indicate” the words so that they correspond to a sinusoidal curve.

, nlp, zipf -, .

, ( ), , , .

+4
2

. , , :

>>> s = sorted(zip(map(int, points), words))
>>> new_words = [word for p, word in list(reversed(s[::2])) + s[1::2]]
# If you have lots of words you'll be better off using some 
# itertools like islice and chain, but the principle becomes evident
>>> new_words
['apple', 'car', 'older', 'values', 'exponential', 'coefficient', 'average', 'man', 'pear']

:

[(9999, 'apple'), (8231, 'car'), (4712, 'older'), (500, 'values'), (5, 'exponential'), (10, 'coefficient'), (3242, 'average'), (5123, 'man'), (9231, 'pear')]
+2

, , , :

>>> def peak(s):
...     return s[::2]+s[-1-(len(s)%2)::-2]
...
>>> peak('112233445566778')
'123456787654321'
>>> peak('1122334455667788')
'1234567887654321'

, :

>>> peak('11111123')
'11123111'
+2

Source: https://habr.com/ru/post/1671222/


All Articles