小编Swa*_*ale的帖子

使用python计算文件中的双字节(一对两个单词)

我想用python计算文件中所有bigrams(一对相邻单词)的出现次数.在这里,我正在处理非常大的文件,所以我正在寻找一种有效的方法.我尝试在文件内容上使用带有正则表达式"\ w +\s\w +"的count方法,但它没有被证明是有效的.

例如,假设我要计算文件a.txt中的双字母数,其中包含以下内容:

"the quick person did not realize his speed and the quick person bumped "
Run Code Online (Sandbox Code Playgroud)

对于上面的文件,bigram集和它们的计数将是:

(the,quick) = 2
(quick,person) = 2
(person,did) = 1
(did, not) = 1
(not, realize) = 1
(realize,his) = 1
(his,speed) = 1
(speed,and) = 1
(and,the) = 1
(person, bumped) = 1
Run Code Online (Sandbox Code Playgroud)

我在Python中遇到了一个Counter对象的例子,它用于计算unigrams(单个单词).它还使用正则表达式方法.

这个例子是这样的:

>>> # Find the ten most common words in Hamlet
>>> import re
>>> from collections import Counter
>>> words = re.findall('\w+', open('a.txt').read())
>>> print …
Run Code Online (Sandbox Code Playgroud)

python regex

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

如何检查字符串是否具有List中的子字符串?

我正在寻找检查字符串是否包含关键字列表中的子字符串的最佳方法.

例如,我创建一个这样的列表:

List<String> keywords = new ArrayList<>();
keywords.add("mary");
keywords.add("lamb");

String s1 = "mary is a good girl";
String s2 = "she likes travelling";
Run Code Online (Sandbox Code Playgroud)

字符串s1具有来自关键字的"mary",但字符串s2没有它.所以,我想定义一个方法:

boolean containsAKeyword(String str, List<String> keywords)
Run Code Online (Sandbox Code Playgroud)

哪里containsAKeyword(s1, keywords)会返回true但containsAKeyword(s2, keywords)会返回false.即使有一个子字符串匹配,我也可以返回true.

我知道我可以遍历关键字列表并在列表中的每个项目上调用str.contains(),但我想知道是否有更好的方法来迭代完整列表(避免O(n)复杂性)或者Java为此提供了任何内置方法.

java iteration substring

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

过滤出具有O(n)时间复杂度的列表元素

我有一个元素列表,其中每个元素是一个非负整数范围.我想以这样一种方式过滤列表,即只分离出最大的未封闭范围.我想以O(n)单循环的方式做到这一点.此列表将始终根据每个范围的起始整数进行排序.封闭范围元素可能出现在列表中的封闭范围元素之前或之后.

例:

假设我拥有的列表是{[0-12],[5-15],[5-20],[10-20],[11-30],[25-42],[28-40]}.在此列表中,范围[5-15]和范围[10-20]都在[5-20]范围内,因此我需要丢弃它们.类似地,范围元素[28-40]在其落入范围内时被丢弃[25-42].我想使用单个循环进行此过滤以实现O(n)时间复杂度.

是否有可能实现这一目标?如果没有,那么用复杂度进行过滤的最佳方法是什么O(n)?Java中的解决方案会很棒.

java sorting algorithm data-structures

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

Python 2.7中的UnicodeDecodeError

我试图在python中读取一个utf-8编码的xml文件,我正在对从文件中读取的行进行一些处理,如下所示:

next_sent_separator_index =  doc_content.find(word_value, int(characterOffsetEnd_value) + 1)
Run Code Online (Sandbox Code Playgroud)

其中doc_content是从文件中读取的行,而word_value是来自同一行的字符串之一.每当doc_content或word_value有一些Unicode字符时,我就会在上面的行中获得编码相关的错误.所以,我尝试首先用utf-8解码(而不是默认的ascii编码)解码它们,如下所示:

next_sent_separator_index =  doc_content.decode('utf-8').find(word_value.decode('utf-8'), int(characterOffsetEnd_value) + 1)
Run Code Online (Sandbox Code Playgroud)

但我仍然得到UnicodeDecodeError如下:

Traceback (most recent call last):
  File "snippetRetriver.py", line 402, in <module>
    sentences_list,lemmatised_sentences_list = getSentenceList(form_doc)
  File "snippetRetriver.py", line 201, in getSentenceList
    next_sent_separator_index =  doc_content.decode('utf-8').find(word_value.decode('utf-8'), int(characterOffsetEnd_value) + 1)
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 8: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议我一个合适的方法/方法来避免python 2.7中的这种编码错误?

python unicode python-2.7

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

python正则表达式匹配任何有效的英语句子

我想知道是否可以写一个python正则表达式来匹配任何有效的英语句子,它可以有字母数字字符和特殊字符.
基本上,我想从XML文件中提取一些特定元素.这些特定元素将具有以下形式:

<p o=<Any Number>> <Any English sentence> </p>  
Run Code Online (Sandbox Code Playgroud)

例如:

<p o ="1"> The quick brown fox jumps over the lazy dog </p>
Run Code Online (Sandbox Code Playgroud)

要么

<p o ="2">  And This is a number 12.90! </p>
Run Code Online (Sandbox Code Playgroud)

我们可以轻松编写正则表达式

<p o=<Any Number>>
Run Code Online (Sandbox Code Playgroud)

</p>标签.但我有兴趣通过编写正则表达式组来提取这些标签之间的句子.

任何人都可以建议使用正则表达式来解决上述问题吗?

此外,如果您可以建议一种解决方法,那么它对我也很有帮助.

python regex text-manipulation

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