词干、词形与频率的词形还原(过滤和分析)

2023-12-23

好的。我正在尝试添加一个word_tag, 但我不确定这是否是正确的方法。 (抱歉我是新手)

from nltk.corpus import wordnet as wn

# Count the words
        word_count = Counter(words)

        # Clean the content a little
        filter_words = ['artists']
        for word in filter_words:
            if word in word_count:
                del word_count[word]

        # POS_TAG the words
        word_tag = nltk.corpus.wn.synsets(word_count)

        # And the survey says...
        print("The Top {0} words".format(n))
        for word, count, word_tag in word_count.most_common(n) and nltk.corpus.wordnet.synsets(n):
            print("{0}: {1, 2}".format(word, count, word_tag))

I'd like to make a DB table with stemmed/lemmatized words' frequency and tagged Part-of-speech Tagging(VERB,NOUN,ADV,..) like this.. http://www.nltk.org/book/ch05.html#tab-universal-tagset http://www.nltk.org/book/ch05.html#tab-universal-tagset enter image description here

我该如何解决该错误? 在 mySQL 数据库上,# |词| POS 标签。 |频率 我也在寻找一种方法来删除字典中没有的单词(artistessex、asifyou),因为我使用 len 解析单词...

    ##
import re
import MySQLdb as mdb
import xml.etree.ElementTree as ET    
import requests, re
from xml.etree import ElementTree
from collections import Counter
from lxml import html
import nltk
from nltk.corpus import wordnet
from nltk import word_tokenize, sent_tokenize, pos_tag
from nltk.corpus import wordnet as wn
from nltk.stem import PorterStemmer, WordNetLemmatizer

##    

def is_noun(tag):
        return tag in ['NN', 'NNS', 'NNP', 'NNPS']


    def is_verb(tag):
        return tag in ['VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ']


    def is_adverb(tag):
        return tag in ['RB', 'RBR', 'RBS']


    def is_adjective(tag):
        return tag in ['JJ', 'JJR', 'JJS']


    def penn_to_wn(tag):
        if is_adjective(tag):
            return wn.ADJ
        elif is_noun(tag):
            return wn.NOUN
        elif is_adverb(tag):
            return wn.ADV
        elif is_verb(tag):
            return wn.VERB
        return None


    stemmer = PorterStemmer()
    lemmatiser = WordNetLemmatizer()



    ## XML PARSING
    def main(n=10):

        # A list of feeds to process and their xpath


        feeds = [
            {'url': 'http://www.nyartbeat.com/list/event_type_print_painting.en.xml', 'xpath': './/Description'},
            {'url': 'http://feeds.feedburner.com/FriezeMagazineUniversal?format=xml', 'xpath': './/description'}
        ]



        # A place to hold all feed results
        results = []

        # Loop all the feeds
        for feed in feeds:
            # Append feed results together
            results = results + process(feed['url'], feed['xpath'])

        # Join all results into a big string
        contents=",".join(map(str, results))

        # Remove double+ spaces
        contents = re.sub('\s+', ' ', contents)

        # Remove everything that is not a character or whitespace
        contents = re.sub('[^A-Za-z ]+', '', contents)

        # Create a list of lower case words that are at least 8 characters
        words=[w.lower() for w in contents.split() if len(w) >=8 ]


        # Count the words
        word_count = Counter(words)

        # POS_TAG the words

        word_stem = stemmer.stem(words)
        word_refine = lemmatiser.lemmatize(word_stem)
    #    tokens = word_tokenize(words) # Generate list of tokens
    #    tokens_pos = pos_tag(tokens)


        # Clean the content a little
        filter_words = ['artists']
        for word in filter_words:
            if word in word_refine:
                del word_refine[word]


        # And the survey says...

        print("The Top {0} words".format(n))
        for word, pos in word_refine.stemmer.stem(n):

            for word, count in word_count.most_common(n):
                print("{0}: {1, 2}".format(word, pos, count))



    def process(url, xpath):
        """
        Downloads a feed url and extracts the results with a variable path
        :param url: string
        :param xpath: string
        :return: list
        """
        contents = requests.get(url)
        root = ElementTree.fromstring(contents.content)
        return [element.text.encode('utf8') if element.text is not None else '' for element in root.findall(xpath)]


    # Add to DB
        for word, count in word_count.most_common(n):

                sql = """INSERT INTO Table1 (keyword, pos, freq) VALUES(%s, %s, %s)"""
                cursor.execute(sql, (word, pos, count))
                db.commit()


    if __name__ == "__main__":
        main()

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

词干、词形与频率的词形还原(过滤和分析) 的相关文章

随机推荐

  • 如何使用 javascript (jquery) 将整数值添加到返回字符串的值?

    我有一个简单的 html 块 例如 span 8 span 我尝试使用 jquery 将 1 添加到值 8 var currentValue replies text var newValue currentValue 1 replies
  • Win32 命名管道和消息大小限制 - 旧的 64K 限制是否仍然适用?

    Win32 过去对消息模式管道的消息大小限制为 64K 正如 KB 文章的残余所证明的那样Q119218 PRB 命名管道 Write 限制为 64K https support microsoft com en us kb 119218
  • BeautifulSoup:如何显示不显示的div的内部?

    我是 BeautifulSoup 的新手 我有一些我不明白的问题 我认为这个问题可能已经得到解答 但我找到的答案在这种情况下都没有帮助我 我需要访问 div 的内部来检索网站的词汇表条目 但是该 div 的内部似乎根本 不显示 在 Beau
  • 如何在Powershell中比较关联数组?

    我有两个关联数组 a k1 v1 k2 k21 v21 b k1 v1 k2 k21 v21 我想知道有没有什么好的方法可以在不编写自己的函数的情况下进行比较 除了编写一个函数来比较每个键的值之外 我不知道有什么方法 如果该值不是原始对象
  • Scrapy XPath 页面上的所有链接

    我正在尝试使用 Scrapy 收集某个域下的所有 URL 我试图使用 CrawlSpider 从主页开始抓取他们的网络 对于每个页面 我想使用 Xpath 提取所有的 href 并以键值对等格式存储数据 键 当前Url 值 该页面上的所有链
  • 将 netbean 升级到 JEE 8

    我的问题很简单 如何升级 Netbeans 8 2 以使用 Java EE 8 我已经安装了普通的 NB 8 2 和 glassfish 5 Java 1 8 并尝试使用 java 9 我已经安装了通过NB控制的GF 5服务器 当我创建新的
  • 服务器选择超时错误 Pymongo

    我第一次尝试 pymongo 但不断收到 ServerSelectionTimeoutError 使用 mongo 命令行登录时 我运行命令如下 mongo 3 0 ssl test net 27080 db qa sslAllowInva
  • 有没有办法将进程的标准输出发送到 SLF4J?

    我本质上是在做以下事情 Process process new ProcessBuilder command start InputStream stdout process getInputStream LoggerFactory get
  • 使用 JQL 过滤特定用户在一段时间内更新的问题

    有没有办法使用 JQL 查找特定用户在每天特定时间段更新的所有问题 或者是否有任何插件可以解决这个问题 如果更新意味着状态改变 你可以检查如下内容 status changed by user name and updated gt sta
  • 保存 JSON 数据以供多个 UIView 使用时出现问题 [重复]

    这个问题在这里已经有答案了 我在访问已拉入的 JSON 数据时遇到了一些问题 我正在使用 JSONModel 来获取 JSON 数据 如下所示 在我的左侧 ViewController m 的顶部 interface LeftViewCon
  • 我的滑块无法在自动模式下工作

    我创建了简单的滑块手风琴 这是我的代码 唯一的问题是它适用于自动模式 并且适用 我的代码
  • *ng如果不工作

    我在用角 2 RC 4和 ngIf 在这里不起作用是视图的代码 View div class navbar navbar inverse navbar fixed top div class container div class navb
  • 两个 Azure 移动服务(.NET 后端)共享同一数据库

    我有两个共享同一个 Azure 数据库的 Azure 移动服务 NET 后端 假设服务 X 和 Y 该数据库由服务 X 首次运行时 创建 并创建了模式名称 X 的表 TA 然后我运行服务 Y 它在同一数据库中创建了相同的表 TA 和 TB
  • 如何在 iPhone sdk 中序列化一个简单的对象?

    我有一本对象字典 它们都是应该可序列化的 POCO 对象 我应该采用什么技术将它们写入磁盘 我正在寻找最简单的选项来编写一些列表来保存状态 我想我有3个选择 plist 文件 然而 这似乎仅限于仅存储预定义的对象 字符串 数字等 而不是对象
  • 在 Eclipse 中哪里可以看到项目的构建进度?

    Question 我正在使用 Eclipse for Java EE Mars 2 当我构建项目时 我在Console window 在哪里可以看到项目构建进度和错误 Window gt Show View gt Progress 为了进步
  • 通过 AJAX 请求执行操作 DNN MVC

    我的DNN MVC开发历程中还出现了另一个问题 我想知道这是否是我犯了一个错误的错误 缺失功能 下面我将尝试解释这个问题 我想要实现什么 我想通过调用 AJAX post 在控制器中执行操作 到目前为止它有效 但是 当我尝试将一些变量返回到
  • 使用 PyAV 将视频直接读入 Numpy(无迭代)

    是否可以使用 PyAV 将视频直接读入 3D Numpy 目前 我正在循环每一帧 i 0 container av open myvideo avi for frame in container decode video 0 if i 0
  • Jupyter 笔记本中的阻塞式交互式绘图

    我正在尝试从 Jupyter 笔记本中获得一个交互式的 阻塞的 matplotlib 窗口 也就是说 我希望 matplotlib 窗口出现并在笔记本中执行暂停 直到它关闭 但我的代码的各种看似合理的排列似乎不起作用 以下产生预期结果 ma
  • 在模板函数中包含不变假设

    考虑一个典型的有限差分应用 assuming T size gt 2 void process T double T0 double T const int T size bool periodic for int i 0 i lt T s
  • 词干、词形与频率的词形还原(过滤和分析)

    好的 我正在尝试添加一个word tag 但我不确定这是否是正确的方法 抱歉我是新手 from nltk corpus import wordnet as wn Count the words word count Counter word