小编mag*_*tar的帖子

使用NLTK将早期现代英语转换为20世纪的拼写

我有一个字符串列表,这些字符串都是以"th"结尾的早期现代英语单词.这些包括神话,任命,解除等等 - 它们都是第三人称单数的共轭.

作为一个更大的项目的一部分(使用我的计算机将Gutenberg的Gargantua和Pantagruel的翻译成更像20世纪英语的东西,以便我能够更容易地阅读它)我想删除最后两三个所有这些单词中的字符并用's替换它们',然后对仍然没有现代化的单词使用稍微修改过的函数,两者都包含在下面.

我的主要问题是我从来没有设法在Python中输入我的内容.我发现这部分语言在这一点上确实令人困惑.

这是删除th的功能:

from __future__ import division
import nltk, re, pprint

def ethrema(word):
    if word.endswith('th'):
        return word[:-2] + 's'
Run Code Online (Sandbox Code Playgroud)

这是删除多余e的函数:

def ethremb(word):
    if word.endswith('es'):
        return word[:-2] + 's'
Run Code Online (Sandbox Code Playgroud)

因此,'abateth'和'accuseth'这两个词会通过ethrema而不是ethremb(ethrema),而'abhorreth'这个词则需要通过两者.

如果有人能想到一种更有效的方法来做到这一点,我会全力以赴.

这是我非常业余的尝试在需要现代化的标记化单词列表上使用这些函数的结果:

>>> eth1 = [w.ethrema() for w in text]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'ethrema'
Run Code Online (Sandbox Code Playgroud)

所以,是的,这确实是打字的问题.这些是我用Python编写的第一个函数,我不知道如何将它们应用于实际的对象.

python text nlp nltk

3
推荐指数
1
解决办法
526
查看次数

如何在Python中写入文件之前确保文件存在或可以创建?

我正在编写一个函数,我想将它放到touch一个文件中,以便我可以写入该文件.如果该文件不存在,我将收到错误.我该怎么说呢?

python filesystems file-io

3
推荐指数
2
解决办法
8476
查看次数

如何在Haskell中表达这个Python for循环?

有时当我想使用时wget,我最终会用Python打印一堆行,如下所示:

>>> for i in range(25):
...   print "http://www.theoi.com/Text/HomerOdyssey", i, ".html"
... 
http://www.theoi.com/Text/HomerOdyssey 0 .html
http://www.theoi.com/Text/HomerOdyssey 1 .html
http://www.theoi.com/Text/HomerOdyssey 2 .html
http://www.theoi.com/Text/HomerOdyssey 3 .html
http://www.theoi.com/Text/HomerOdyssey 4 .html
http://www.theoi.com/Text/HomerOdyssey 5 .html
http://www.theoi.com/Text/HomerOdyssey 6 .html
http://www.theoi.com/Text/HomerOdyssey 7 .html
http://www.theoi.com/Text/HomerOdyssey 8 .html
http://www.theoi.com/Text/HomerOdyssey 9 .html
http://www.theoi.com/Text/HomerOdyssey 10 .html
http://www.theoi.com/Text/HomerOdyssey 11 .html
http://www.theoi.com/Text/HomerOdyssey 12 .html
http://www.theoi.com/Text/HomerOdyssey 13 .html
http://www.theoi.com/Text/HomerOdyssey 14 .html
http://www.theoi.com/Text/HomerOdyssey 15 .html
http://www.theoi.com/Text/HomerOdyssey 16 .html
http://www.theoi.com/Text/HomerOdyssey 17 .html
http://www.theoi.com/Text/HomerOdyssey 18 .html
http://www.theoi.com/Text/HomerOdyssey 19 .html
http://www.theoi.com/Text/HomerOdyssey 20 .html
http://www.theoi.com/Text/HomerOdyssey 21 .html …
Run Code Online (Sandbox Code Playgroud)

python iteration io haskell wget

3
推荐指数
2
解决办法
701
查看次数

在Haskell中表达一系列脚本化的Python字符串替换

我经常使用Python来替换文本中的各种类型的字符,使用如下所示的脚本:

#!/usr/bin/env python                                                                                                                                                                                                                                                         
# coding=UTF-8

import sys

for file in sys.argv[1:]:                                                                                                                                                                                                                                                     
    f = open(file)                                                                                                                                                                                                                                                            
    fs = f.read()
    r1 = fs.replace('\n',' ')
    r2 = r1.replace('\r',' ')                                                                                                                                                                                                                                                   
    r3 = r2.replace('. ','.\n\n')                                                                                                                                                                                                                                                   
    r4 = r3.replace('é','e')
    r5 = r4.replace('\xc2',' ')
    r6 = r5.replace('\xa0',' ')
    r7 = r6.replace(' ',' ')
    r8 = r7.replace(' ',' ')
    r9 = r8.replace('\n ','\n')
    f.close()                                                                                                                                                                                                                                                                 
    print r8
Run Code Online (Sandbox Code Playgroud)

但我现在正在学习Haskell,因为我厌倦了Python.

我在Haskell做的最好的尝试是

#!/usr/bin/runhaskell 

import System.IO

main :: IO ()
main = do 
       inh <- getArgs >>= withFileLines
       outh <- -- ??
       mainloop inh …
Run Code Online (Sandbox Code Playgroud)

file-io text-processing haskell replace

3
推荐指数
1
解决办法
152
查看次数

在并行列表上进行迭代打印以在Python中打印列

我有vsort和vsorta,这两个列表具有相同数量的项目,它们应该彼此相邻(每个列表大约250个元素).我想将它们打印为平行列,如下所示:

>>> for x,y in vsort,vsorta:
...     print x, y
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> 
Run Code Online (Sandbox Code Playgroud)

有没有办法解决这个错误?

python printing collections list

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

sed one-liner删除所有单个换行符?

所以,例如,

A paragraph's newlines would be removed let's say
it contained only single
newlines.
Run Code Online (Sandbox Code Playgroud)

然后我想跳过的东西:

However.

Our previous pair of newlines wouldn't.
Run Code Online (Sandbox Code Playgroud)

regex sed

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

在CodingBat上解决雪茄和松鼠拼图会引发语法错误

我试图解决这个 CodingBat问题:

喜欢聚会的松鼠聚在一起抽雪茄.只有当工作日的雪茄数量在40到60之间时,这样的一方才被认为是成功的.然而,在周末,雪茄的数量没有上限.编写一个函数,如果具有给定值的一方成功,则返回True.

不幸的是,虽然偶尔使用Python,但我还不够理解为什么我的代码在第5行出现语法错误而失败:

def cigar_party(cigars, is_weekend):
  if is_weekend:
    if cigars >= 40:
      return True
  else if:
    cigars >= 40 and cigars =< 60:
      return True
  else:
    return False
Run Code Online (Sandbox Code Playgroud)

python if-statement syntax-error

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

编写shell脚本的Shell脚本

两个问题:如何将此脚本中的shell变量写入其子脚本?

有没有更简单的方法来做到这一点?

如果你不能遵循我正在做的事情,我是:

1)从一个目录列表开始,其名称将存储为$ i所取的值

2)cd'ing到$ i的每个值并且ls'ing其内容

3)通过cat将其内容回显到具有目录名称的新脚本

4)使用echo和cat编写一个包含$ i的ls'd值的新脚本,并将它们全部发送到一个名为$i@tumblr.com的博客电子邮件地址

#/bin/sh
read -d '' commands <<EOF

#list of directories goes here
dir1
dir2
dir3
etc...    

EOF

for i in $commands
do

cd $SPECIALPATH/$i
echo ("#/bin/sh \n read -d '' directives <<EOF \n") | cat >> $i.sh
ls | cat >> $i.sh
echo ("EOF \n for q in $directives \n do \n uuencode $q $q | sendmail $i \n done \n") | cat >> $i.sh
# NB -- I am asking …
Run Code Online (Sandbox Code Playgroud)

unix linux shell

0
推荐指数
1
解决办法
595
查看次数

创建一个Python函数,打开文本文件,读取它,标记它,最后从命令行或作为模块运行

我一直在努力学习Python一段时间.偶然的机会,我通过指向此处的Google搜索链接发生了官方教程的第6章 .当我从那个页面了解到,那些功能是模块的核心,并且可以从命令行调用这些模块,我很满意.这是我第一次尝试这两种方法,openbook.py

import nltk, re, pprint
from __future__ import division

def openbook(book):
    file = open(book)
    raw = file.read()
    tokens = nltk.wordpunct_tokenize(raw)
    text = nltk.Text(tokens)
    words = [w.lower() for w in text]
    vocab = sorted(set(words))
    return vocab
if __name__ == "__main__":
    import sys
    openbook(file(sys.argv[1]))
Run Code Online (Sandbox Code Playgroud)

我想要的是这个函数可以作为模块openbook导入,以及openbook.py从命令行获取文件并对其执行所有这些操作.

当我从命令行运行openbook.py时,会发生这种情况:

gemeni@a:~/Projects-FinnegansWake$ python openbook.py vicocyclometer
Traceback (most recent call last):
  File "openbook.py", line 23, in <module>
    openbook(file(sys.argv[1]))
  File "openbook.py", line 5, in openbook
    file = open(book)
Run Code Online (Sandbox Code Playgroud)

当我尝试将其用作模块时,会发生以下情况:

>>> import openbook
>>> openbook('vicocyclometer')
Traceback (most …
Run Code Online (Sandbox Code Playgroud)

python nltk

0
推荐指数
1
解决办法
2661
查看次数