首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Python在句子列表中形成单词大写

用Python在句子列表中形成单词大写
EN

Stack Overflow用户
提问于 2014-02-18 04:41:47
回答 10查看 107.1K关注 0票数 33

我有一个句子清单:

代码语言:javascript
复制
text = ['cant railway station','citadel hotel',' police stn']. 

我需要形成双标对并将它们存储在一个变量中。问题是,当我这样做的时候,我会得到一对句子,而不是单词。以下是我所做的:

代码语言:javascript
复制
text2 = [[word for word in line.split()] for line in text]
bigrams = nltk.bigrams(text2)
print(bigrams)

产额

代码语言:javascript
复制
[(['cant', 'railway', 'station'], ['citadel', 'hotel']), (['citadel', 'hotel'], ['police', 'stn'])

火车站和大本营酒店不能一条龙。我想要的是

代码语言:javascript
复制
[([cant],[railway]),([railway],[station]),([citadel,hotel]), and so on...

第一句的最后一个词不应与第二句的第一个词合并。我该怎么做才能让它发挥作用?

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2014-02-18 05:04:29

使用list comprehensionszip

代码语言:javascript
复制
>>> text = ["this is a sentence", "so is this one"]
>>> bigrams = [b for l in text for b in zip(l.split(" ")[:-1], l.split(" ")[1:])]
>>> print(bigrams)
[('this', 'is'), ('is', 'a'), ('a', 'sentence'), ('so', 'is'), ('is', 'this'), ('this',     
'one')]
票数 56
EN

Stack Overflow用户

发布于 2018-02-19 18:30:32

代码语言:javascript
复制
from nltk import word_tokenize 
from nltk.util import ngrams


text = ['cant railway station', 'citadel hotel', 'police stn']
for line in text:
    token = word_tokenize(line)
    bigram = list(ngrams(token, 2)) 

    # the '2' represents bigram; you can change it to get ngrams with different size
票数 17
EN

Stack Overflow用户

发布于 2014-02-18 04:55:32

与其将文本转换为字符串列表,不如将每个句子分别作为字符串开始。我还删除了标点符号和句号,如果与您无关,只需删除以下部分:

代码语言:javascript
复制
import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import WordPunctTokenizer
from nltk.collocations import BigramCollocationFinder
from nltk.metrics import BigramAssocMeasures

def get_bigrams(myString):
    tokenizer = WordPunctTokenizer()
    tokens = tokenizer.tokenize(myString)
    stemmer = PorterStemmer()
    bigram_finder = BigramCollocationFinder.from_words(tokens)
    bigrams = bigram_finder.nbest(BigramAssocMeasures.chi_sq, 500)

    for bigram_tuple in bigrams:
        x = "%s %s" % bigram_tuple
        tokens.append(x)

    result = [' '.join([stemmer.stem(w).lower() for w in x.split()]) for x in tokens if x.lower() not in stopwords.words('english') and len(x) > 8]
    return result

要使用它,请这样做:

代码语言:javascript
复制
for line in sentence:
    features = get_bigrams(line)
    # train set here

请注意,这会更进一步,并且实际上在统计上对bigram进行了评分(这在训练模型时非常有用)。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21844546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档