OP: Как я могу получить компанию из текста.
Это было бы сложно, наоборот было бы проще и быстрее:
Вы можете повторитьчерез organisations
и проверьте, существует ли какой-либо из них в text
, используя in
:
organizations = ['mahindra & mahindra','atametica','cognizant Technology','Tata Cosultancy Services']
text = 'XXX has worked in Tata Cosultancy Services and currently working in cognizant technology.He has experience in Java Technology as well'
for org in organizations:
if org.lower() in text.lower():
print(org)
РЕДАКТИРОВАТЬ:
Чтобы получить всю организациюиспользуйте сравнение строк с .lower()
для текстов без учета регистра.
EDIT 2:
Использование re
:
import re
for org in organizations:
if re.search(org, text, re.IGNORECASE):
print(org)
OUTPUT:
cognizant Technology
Tata Cosultancy Services
РЕДАКТИРОВАТЬ 3:
Рассматривая ситуацию, когда элемент в list
существует в text
, но только частично.Вы можете использовать поиск по слову, используя regex
то есть
organizations = ['mahindra & mahindra','atametica','cognizant Technology','Tata Cosultancy Services', 'nitor']
text = 'XXX has worked in Tata Cosultancy Services and currently working in cognizant technology.He has experience in Java Technology as well as monitor'
import re
for org in organizations:
if re.search('\\b' +org+ '\\b', text, re.IGNORECASE):
print(org)
ВЫХОД:
cognizant Technology
Tata Cosultancy Services