有没有办法从Nunjucks模板语言中的字符串中获取子字符串?

时间:2017-12-19 06:21:59

标签: nunjucks

如果我们有值'cat',我想要子串'at'(需要删除第一个和最后一个字母)。我们如何在Nunjucks模板中实现这一目标?

1 个答案:

答案 0 :(得分:1)

您可以使用JavaScript import nltk def extract_entity_names(t): entity_names = [] if hasattr(t, 'label') and t.label: if t.label() == 'NE': entity_names.append(' '.join([child[0] for child in t])) else: for child in t: entity_names.extend(extract_entity_names(child)) return entity_names with open('sample.txt', 'r') as f: for line in f: sentences = nltk.sent_tokenize(line) tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences] tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences] chunked_sentences = nltk.ne_chunk_sents(tagged_sentences, binary=True) entities = [] for tree in chunked_sentences: entities.extend(extract_entity_names(tree)) print(entities) 方法执行此操作,如下所示:

slice