gpt4 book ai didi

Java,通过各种标签拆分一个String并存入一个Map

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:31 24 4
gpt4 key购买 nike

我有一个要求,创建一种 Markdown 标签来放置粗体[N] 斜体 [C] 创建时给定字符串中的文本 PDF'sIText .

所以,给定这个字符串:

String toCheck = "Example [N]bold text[N] other example [C]italic text[C]";

应该结果:

Example bold text other example italic text


好吧,我们走吧

我有一个带有字体类型的枚举:

private enum FontType {
BOLD, ITALIC, NORMAL
}

为此,我想创建一个 LinkedHashMap<String, Enum>插入具有相应字体类型的字符串片段(稍后将转换为 com.itextpdf.text.Chunk 并插入到单个 com.itextpdf.text.Paragraph 中。

那么我怎样才能实现 LinkedHashMap这样的结果??

pos String            enum
0 "Example " NORMAL
1 "bold text" BOLD
2 " other example " NORMAL
3 "italic text" ITALIC

我创建了一个自定义 Iterator这给了我标签位置:

public class OwnIterator implements Iterator<Integer> 
{
private Iterator<Integer> occurrencesItr;

public OwnIterator(String toCheck, String[] validPair) {
// build regex to search for every item in validPair
Matcher[] matchValidPair = new Matcher[validPair.length];
for (int i = 0 ; i < validPair.length ; i++) {
String regex =
"(" + // start capturing group
"\\Q" + // quote entire input string so it is not interpreted as regex
validPair[i] + // this is what we are looking for, duhh
"\\E" + // end quote
")" ; // end capturing group
Pattern p = Pattern.compile(regex);
matchValidPair[i] = p.matcher(toCheck);
}
// do the search, saving found occurrences in list
List<Integer> occurrences = new ArrayList<>();
for (int i = 0 ; i < matchValidPair.length ; i++) {
while (matchValidPair[i].find()) {
occurrences.add(matchValidPair[i].start(0)+1); // +1 if you want index to start at 1
}
}
// sort the list
Collections.sort(occurrences);
occurrencesItr = occurrences.iterator();
}

@Override
public boolean hasNext() {
return occurrencesItr.hasNext();
}

@Override
public Integer next() {
return occurrencesItr.next();
}

@Override
public void remove() {
occurrencesItr.remove();
}

}

我已经检查了标签是否平衡,我可以得到所有的标签位置:

String[] validPair = {"[N]", "[C]" };
OwnIterator itr = new OwnIterator(toCheck, validPair);
while (itr.hasNext()) {
System.out.println(itr.next());
}

但是在获得所有位置后无法弄清楚如何区分每个部分并分配正确的枚举值。

一些想法?也许我的方法有误,或者有人可以找到更好的方法?

最佳答案

下面的一段代码会给你想要的LinkedHashMap,

private Map<String, FontType> getMapFromTags(String toCheck) {
Map<String, FontType> chunksMap = new LinkedHashMap<>();
boolean openTag = false;

while (toCheck.contains(TAG_NEGRITA) || toCheck.contains(TAG_CURSIVA)) {
final int indexOfBold = toCheck.indexOf(TAG_NEGRITA);
final int indexOfItalics = toCheck.indexOf(TAG_CURSIVA);

final int indexToUse = getValidIndexToUse(indexOfBold, indexOfItalics);

final String substring = toCheck.substring(0, indexToUse);
toCheck = toCheck.substring(indexToUse + 3, toCheck.length());

if (!substring.isEmpty()) {
if (!openTag) {
chunksMap.put(substring, FontType.NORMAL);
} else if (indexToUse == indexOfBold) {
chunksMap.put(substring, FontType.BOLD);
} else {
chunksMap.put(substring, FontType.ITALIC);
}
}

openTag = !openTag;
}
// check if there is some NORMAL text at the end
if (!toCheck.isEmpty())
chunksMap.put(toCheck, FontType.NORMAL);

return chunksMap;
}

private int getValidIndexToUse(int indexOfBold, int indexOfItalics) {
if (indexOfBold > -1 && indexOfItalics == -1)
return indexOfBold;
else if (indexOfItalics > -1 && indexOfBold == -1)
return indexOfItalics;
else
return indexOfBold > -1 && indexOfBold < indexOfItalics ? indexOfBold : indexOfItalics;
}

但是当您发现两个或更多必须进行哈希处理的相等字符串时,就会出现问题。

关于Java,通过各种标签拆分一个String并存入一个Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34589739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com