相关疑难解决方法(0)

在Python中查找所有出现的子字符串

Python已经string.find()并且string.rfind()在字符串中获取子字符串的索引.

我想知道,也许有类似的东西string.find_all()可以返回所有已创建的索引(不仅从开始或从头到尾)?

例如:

string = "test test test test"

print string.find('test') # 0
print string.rfind('test') # 15

#this is the goal
print string.find_all('test') # [0,5,10,15]
Run Code Online (Sandbox Code Playgroud)

python regex string

325
推荐指数
12
解决办法
39万
查看次数

nltk NaiveBayesClassifier培训情绪分析

我正在NaiveBayesClassifier使用句子训练Python,它给出了下面的错误.我不明白错误是什么,任何帮助都会很好.

我尝试了很多其他输入格式,但错误仍然存​​在.代码如下:

from text.classifiers import NaiveBayesClassifier
from text.blob import TextBlob
train = [('I love this sandwich.', 'pos'),
         ('This is an amazing place!', 'pos'),
         ('I feel very good about these beers.', 'pos'),
         ('This is my best work.', 'pos'),
         ("What an awesome view", 'pos'),
         ('I do not like this restaurant', 'neg'),
         ('I am tired of this stuff.', 'neg'),
         ("I can't deal with this", 'neg'),
         ('He is my sworn enemy!', 'neg'),
         ('My boss is horrible.', 'neg') ]

test = [('The beer …
Run Code Online (Sandbox Code Playgroud)

python nlp nltk sentiment-analysis textblob

22
推荐指数
3
解决办法
3万
查看次数

Python - 用于将文本拆分为句子的RegEx(句子标记化)

我想从一个字符串中创建一个句子列表然后将它们打印出来.我不想用NLTK来做这件事.因此,它需要在句子末尾的句点分割,而不是在小数,缩写或名称的标题上,或者如果句子有.com这是尝试正则表达式不起作用.

import re

text = """\
Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid a lot for it. Did he mind? Adam Jones Jr. thinks he didn't. In any case, this isn't true... Well, with a probability of .9 it isn't.
"""
sentences = re.split(r' *[\.\?!][\'"\)\]]* *', text)

for stuff in sentences:
        print(stuff)    
Run Code Online (Sandbox Code Playgroud)

示例输出的示例

Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid a lot for it. 
Did he mind?
Adam Jones Jr. thinks he …
Run Code Online (Sandbox Code Playgroud)

python regex nlp tokenize

22
推荐指数
3
解决办法
4万
查看次数

R将语料库分成句子

  1. 我有许多PDF文档,我已将其读入带库的语料库中tm.如何将语料库分解成句子?

  2. 可以readLines通过sentSplit从包qdap[*] 读取文件来完成.该功能需要数据帧.它还需要放弃语料库并单独阅读所有文件.

  3. 如何在语料库中传递函数sentSplit{ qdap} tm?或者,还有更好的方法?.

注意:sentDetect 库中有一个函数,openNLP现在是Maxent_Sent_Token_Annotator- 同样的问题适用:如何将它与语料库[tm]结合起来?

split r sentence tm qdap

12
推荐指数
2
解决办法
2万
查看次数

NLTK/pyNLTK可以"按语言"(即非英语),以及如何工作?

如何告诉NLTK以特定语言处理文本?

有一段时间我会编写一个专门的NLP例程,在非英语(但仍然是欧洲的)欧洲文本域上进行POS标记,标记等操作.

这个问题似乎只涉及不同的语料库,而不是代码/设置的变化: 德语中的POS标记

或者,是否有任何专门用于python的希伯来语/西班牙语/波兰语NLP模块?

python nlp nltk

10
推荐指数
1
解决办法
6979
查看次数

如何在Python中用句子分解段落

我需要解析Python中段落的句子.是否有现成的包,或者我应该尝试在这里使用正则表达式?

python regex text-segmentation

9
推荐指数
1
解决办法
2万
查看次数

将文本拆分为句子

我希望将文本分成句子.谁能帮我?

我还需要处理缩写.但是我的计划是在早期阶段更换这些.先生 - >先生

import re  
import unittest    

class Sentences:

    def __init__(self,text):
        self.sentences = tuple(re.split("[.!?]\s", text))

class TestSentences(unittest.TestCase):

    def testFullStop(self):
        self.assertEquals(Sentences("X. X.").sentences, ("X.","X."))

    def testQuestion(self):
        self.assertEquals(Sentences("X? X?").sentences, ("X?","X?"))

    def testExclaimation(self):
        self.assertEquals(Sentences("X! X!").sentences, ("X!","X!"))

    def testMixed(self):
        self.assertEquals(Sentences("X! X? X! X.").sentences, ("X!", "X?", "X!", "X."))
Run Code Online (Sandbox Code Playgroud)

谢谢,巴里

编辑:首先,我很乐意满足上面列出的四项测试.这有助于我更好地理解正则表达式的工作原理.现在我可以在我的测试中定义一个句子作为X等.

python regex python-3.x text-segmentation

5
推荐指数
1
解决办法
7836
查看次数

准确地分裂句子

我的程序采用一个文本文件,并将每个句子分成一个列表,使用的split('.')意思是,当它注册一个完整的停止时它会分裂但是它可能是不准确的.

例如

str='i love carpets. In fact i own 2.4 km of the stuff.'
Run Code Online (Sandbox Code Playgroud)

产量

listOfSentences = ['i love carpets', 'in fact i own 2', '4 km of the stuff']

期望的输出

 listOfSentences = ['i love carpets', 'in fact i own 2.4 km of the stuff']
Run Code Online (Sandbox Code Playgroud)

我的问题是:我如何分割句子的结尾,而不是每一个句号.

python parsing nlp

5
推荐指数
1
解决办法
1319
查看次数

使用Python中的正则表达式将文本拆分为句子

我试图将一个样本文本拆分成一个没有分隔符的句子列表,每个句子末尾没有空格.

示范文本:

你第一次看到第二次文艺复兴时,它可能看起来很无聊.至少看两次,绝对看第2部分.它会改变你对矩阵的看法.人类是开始战争的人吗?AI是一件坏事吗?

进入此(所需输出):

['The first time you see The Second Renaissance it may look boring', 'Look at it at least twice and definitely watch part 2', 'It will change your view of the matrix', 'Are the human people the ones who started the war', 'Is AI a bad thing']
Run Code Online (Sandbox Code Playgroud)

我的代码目前是:

def sent_tokenize(text):
    sentences = re.split(r"[.!?]", text)
    sentences = [sent.strip(" ") for sent in sentences]
    return sentences
Run Code Online (Sandbox Code Playgroud)

但是这个输出(电流输出):

['The first time you see The Second Renaissance it may look boring', …
Run Code Online (Sandbox Code Playgroud)

python regex split

2
推荐指数
1
解决办法
2507
查看次数