bod*_*tva 7 c++ python perl nlp utf-8
我有一个像这样的印地文脚本文件:
3. ???? ?? ?????? ???? ?????? ??? ??????? ???
Run Code Online (Sandbox Code Playgroud)
我必须编写一个程序,为每个句子中的每个单词添加一个位置.因此,特定单词位置的每一行的编号应从括号中的1开始.输出应该是这样的.
3. ????(1) ??(2) ??????(3) ????(4) ??????(5) ???(6) ???????(7) ??(8) ?(9)
Run Code Online (Sandbox Code Playgroud)
上述句子的含义是:
3. India has a long and rich history.
Run Code Online (Sandbox Code Playgroud)
如果你观察'.'(这是一个完整的句子,相当于英语中的'.')也有一个单词位置,同样其他特殊符号也会有,因为我试图去英语 - 印地语单词对齐(自然语言处理(NLP)的一部分)所以完整的英语'.' 应映射到'.' 用印地语.串行nos保持不变.我认为逐字逐句阅读可能是一个解决方案.你可以帮我解决一下如何使用C++,如果它简单或者更容易,你可以通过其他一些编程语言建议其他方式,如Python/Perl ..?
问题是我能够使用C++获取我的英文文本的单词位置,因为我能够使用C++中的ASCII值逐个字符地阅读,但我不知道如何对印地文文本进行相同的操作.
所有这一切的最终目的是看看英文文本的哪个单词位置映射到印地语中的哪个位置.这样我就可以实现双向对齐.
感谢您的时间...:)
哇,已经有6个答案,而不是一个答案实际上做了mgj想要的.jkp接近,但随后通过删除daṇḍa来丢球.
Perl救援.代码更少,错误更少.
use utf8; use strict; use warnings;
use Encode qw(decode);
my $index;
join ' ', map { $index++; "$_($index)" } split /\s+|(?=?)/, decode 'UTF-8', <>;
# returns ????(1) ??(2) ??????(3) ????(4) ????(5) ??(6) ?????(7) ?(8) ?(9)
Run Code Online (Sandbox Code Playgroud)
编辑:STDIN根据评论更改为读取,添加了最佳实践编译指示
如果您正在使用C++并且确定UTF-8是您的应用程序的可行编码,那么您可以查看utfcpp,它是一个库,它为stdlib中的类型提供了许多等价物(例如流和字符串处理函数),但是抽象了处理像UTF8这样的可变长度编码的困难.
另一方面,如果您可以自由使用任何语言,我会说在Python之类的东西上做这样的事情要容易得多:它的unicode支持非常好,捆绑的字符串处理例程也是如此.
#!/usr/bin/env python
# encoding: utf-8
string = u"???? ?? ?????? ???? ?????? ??? ??????? ???"
parts = []
for part in string.split():
parts.extend(part.split(u"?"))
print "No of Parts: %d" % len(parts)
print "Parts: %s" % parts
Run Code Online (Sandbox Code Playgroud)
输出:
No of Parts: 9
Parts: [u'\u092d\u093e\u0930\u0924', u'\u0915\u093e', u'\u0907\u0924\u093f\u0939\u093e\u0938', u'\u0915\u093e\u092b\u0940', u'\u0938\u092e\u0943\u0926\u094d\u0927', u'\u090f\u0935\u0902', u'\u0935\u093f\u0938\u094d\u0924\u0943\u0924', u'\u0939\u0948', u'']
Run Code Online (Sandbox Code Playgroud)
此外,由于您正在进行自然语言处理,您可能需要查看用于Python 的NLTK库,它具有丰富的工具,仅用于此类工作.
我强烈建议您使用 Python 来实现这样的应用程序。\n它将减轻解码字符串的负担(更不用说为它们分配内存等)。您将可以自由地专注于您的问题,而不是语言问题。
\n\n例如,如果上面的句子包含在 utf-8 文件中,并且您正在使用 python2.x。\n如果您使用 python 3.x,它会更具可读性,因为您不必在 unicode 字符串前添加前缀与 \'u" \' 一样,如本例所示(但是您将丢失许多第三方库:
\n\nseparators = [u"\xe0\xa5\xa4", u",", u"."]\ntext = open("indiantext.txt").read()\n#This converts the encoded text to an internal unicode object, where\n# all characters are properly recognized as an entity:\ntext = text.decode("utf-8")\n\n#this breaks the text on the white spaces, yielding a list of words:\nwords = text.split()\n\ncounter = 1\n\noutput = ""\nfor word in words:\n #if the last char is a separator, and is joined to the word:\n if word[-1] in separators and len(word) > 1:\n #word up to the second to last char:\n output += word[:-1] + u"(%d) " % counter\n counter += 1\n #last char\n output += word[-1] + u"(%d) " % counter\n else:\n output += word + u"(%d) " % counter\n counter += 1\n\nprint output\nRun Code Online (Sandbox Code Playgroud)\n\n这是一个“展开”的示例,随着您越来越习惯 Python,有更支持的方法来表达它。按照教程,您可以在短短几个小时内学习该语言的基础知识。(例如,http ://python.org本身)
\n