我有一个wordnet中所有名词的列表,现在我想只留下车辆中的单词并删除其余的单词.我该怎么做?下面是我想要的伪代码,但我不知道如何使其工作
for word in wordlist:
if not "vehicle" in wn.synsets(word):
wordlist.remove(word)
Run Code Online (Sandbox Code Playgroud) 我的朋友和我正在创建一个请愿板,我正在添加一个喜欢/不喜欢的功能.我打算这样做,只有用户可以喜欢/不喜欢它.问题是,我不知道如何确保用户不多次垃圾邮件按钮以及如何注册哪个用户喜欢/不喜欢哪个主题.以下是我的代码.
编辑:谢谢我现在正在创建喜欢/不喜欢的表.但现在我必须将用户与数据库进行比较,看看他们之前是否喜欢评论.我知道我必须使用WHERE(检查喜欢和不喜欢的表),但我不知道如何将它与IF结合起来.
<?php
include connect.php
if (isset($_POST['like']) || isset($_POST['dislike']))
{
if($_SESSION['signed_in']){
if (isset($_POST['like'])) {
$sql="UPDATE
topics
SET
likes=likes+1,
WHERE
id=topic_id";
echo "You liked it";
}
elseif (isset($_POST['dislike'])) {
$sql="UPDATE
topics
SET
dislikes=dislikes+1,
WHERE
id=topic_id";
echo "You disliked it";
}
}
else{
echo 'Please log in.'
}
?>
Run Code Online (Sandbox Code Playgroud) import nltk
from nltk import *
from nltk.corpus import wordnet as wn
output=[]
wordlist=[]
entries = nltk.corpus.cmudict.entries()
for entry in entries[:200]: #create a list of words, without the pronounciation since.pos_tag only works with a list
wordlist.append(entry[0])
for word in nltk.pos_tag(wordlist): #create a list of nouns
if(word[1]=='NN'):
output.append(word[0])
for word in output:
x = wn.synsets(word) #remove all words which does not have synsets (this is the problem)
if len(x)<1:
output.remove(word)
for word in output[:200]:
print (word," ",len(wn.synsets(word)))
Run Code Online (Sandbox Code Playgroud)
我试图删除没有synsets的所有单词但由于某种原因它不起作用.在运行程序时,我发现即使一个单词被称为len(wn.synsets(word))= 0,它也不会从我的列表中删除.谁能告诉我出了什么问题?
这是我编写的一个函数,用于检查名词列表中是否存在上义词和下义词。
def check_hyper_hypo(wordlist):
returnlist=[]
for word in wordlist: #by definition a base word has a word above and below heirachy
x = wn.synsets(word)
for syn in x:
if not(((len(syn.hypernyms()))==0)or((len(syn.hyponyms()))==0)):
returnlist.append(word)
break
return returnlist
Run Code Online (Sandbox Code Playgroud)
如何仅检查作为名词的同义词集的上位词/下位词的长度?例如
for syn in x:
if ".n." in syn:
#rest of code
Run Code Online (Sandbox Code Playgroud)