1
0
forked from tdwojak/Python2018
Python2018/labs02/task03.py
2018-05-13 12:54:51 +02:00

44 lines
1.2 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Zad 4. Napisz funkcje oov(text, vocab), która zwraca listę wyrazów
(bez duplikatów), które występują w tekście text i nie występują w liście
znanych wyrazów vocab. Argumenty funkcji text i vocab to odpowiednio łańcuch
znakowy i lista łańuchów znakowych. Wszystkie wyrazy należy zmienić na małe
litery. (OOV = out of vocabulary) (W pythonie istnieje struktura danych tak
jak 'set', która przechowuje elementy bez powtórzeń.)
"""
def oov(text, vocab):
flag = []
textSegm = set(text.split(' '))
for word in textSegm:
if word not in vocab:
flag.append(word)
return flag
def tests(f):
inputs = [("this is a string , which i will use for string testing",
[',', 'this', 'is', 'a', 'which', 'for', 'will', 'i'])]
outputs = [['string', 'testing', 'use']]
for input, output in zip(inputs, outputs):
if set(f(*input)) != set(output):
return "ERROR: {}!={}".format(f(*input), output)
break
return "TESTS PASSED"
if __name__ == "__main__":
print(tests(oov))
text = "this is a string , which i will use for string testing"
textSegm = set(text.split(' '))
print(textSegm)
len(textSegm)