小编Oli*_*own的帖子

如何匹配句子中的字符串

我想检查句子中是否存在特定字符串.我为此目的使用简单的代码

subStr = 'joker'
Sent = 'Hello World I am Joker'

if subStr.lower() in Sent.lower():
    print('found')
Run Code Online (Sandbox Code Playgroud)

这是一种简单直接的方法,但当句子出现时它会失败

你好世界我是Jo ker

你好世界我是J oker

当我从PDF文件中解析句子时,会有一些不必要的空间来到这里.

解决此问题的一种简单方法是从句子中删除所有空格并查找子字符串匹配.我想知道其他人对此的想法,我应该坚持这种方法还是寻找其他一些选择.

python string string-matching python-3.x

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

如何训练 n-gram 的朴素贝叶斯分类器 (movie_reviews)

以下是模型数据集Naive Bayes Classifier训练的代码。我想通过考虑模型来训练和分析其性能。我们怎样才能做到呢。movie_reviewsunigrambigramtrigram

import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

def create_word_features(words):
    useful_words = [word for word in words if word not in stopwords.words("english")] 
    my_dict = dict([(word, True) for word in useful_words])
    return my_dict

pos_data = []
for fileid in movie_reviews.fileids('pos'):
    words = movie_reviews.words(fileid)
    pos_data.append((create_word_features(words), "positive"))    

neg_data = []
for fileid in movie_reviews.fileids('neg'):
    words = movie_reviews.words(fileid)
    neg_data.append((create_word_features(words), "negative")) 

train_set = pos_data[:800] + neg_data[:800] …
Run Code Online (Sandbox Code Playgroud)

python nlp classification nltk

4
推荐指数
1
解决办法
6035
查看次数

如何使用strip()从句子中删除\n字符

我正在尝试\n使用strip()命令从句子中删除字符,但它似乎不起作用。

str1 = "Hello World \n, I\n am \nhere"
print(str1.strip())
Run Code Online (Sandbox Code Playgroud)

输出

Hello World 
, I
 am 
here
Run Code Online (Sandbox Code Playgroud)

python newline strip python-3.x

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

如何在 Django 应用程序中获取复选框值

我正在尝试在一个简单的django应用程序中使用复选框控件。代码逻辑似乎没问题,但我得到一个空fruit列表 ( [None, None])。我不知道为什么它不起作用,任何人都可以指出错误。提前致谢

索引.html

<div class="form-check">
<input class="form-check-input" type="checkbox" value="Apple" id="apple">
<label class="form-check-label" for="apple">Apple</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="Mango" id="mango">
<label class="form-check-label" for="mango">Mango</label>
</div> 
Run Code Online (Sandbox Code Playgroud)

查看.py

if request.method == 'POST':
    fruit = []
    fruit.append(request.POST.get('apple'))
    fruit.append(request.POST.get('mango'))
Run Code Online (Sandbox Code Playgroud)

python django python-3.x twitter-bootstrap bootstrap-4

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